This is a GSP problem/niggle I've faced before in JSP and I'm looking for the cleanest possible solution.
Essentially, I'm using a for loop (<g:each> in GSP) to iterate through a list of items and output a HTML node for each item:
<g:each status="i" var="item" in="items">
<span class="item">${item}</span>
</g:each>
All <span> nodes contain a CSS class of item, but I want the first node to also contain a selected class. Thus, I update the code to:
<g:each status="i" var="item" in="items">
<g:if test="${i == 0}">
<span class="item selected">${item}</span>
</g:if>
<g:else>
<span class="item">${item}</span>
</g:else>
</g:each>
This seems like a complicated approach however as I'm duplicating a lot of code. Another solution is to use a custom tag library and pass the current index into it:
<g:each status="i" var="item" in="items">
<span class="item <g:getItemClass index='${i}' />">${item}</span>
</g:each>
The tag library will return selected when index is equal to 0, otherwise it won't return anything at all. Again, this adds complexity.
Other possible solutions:
Use the index in your CSS class name (very messy)
Set a class name var (). Not better than a custom tag imo.
Use scriptlets (no way)
Any other approaches to this that are clean and simple?
Thanks!
Usually it's just a:
<g:each status="i" var="item" in="items">
<span class="item ${i == 0 ? 'selected' : ''}">${item}</span>
</g:each>
Related
I need to get rel="" into this html. This is part of AEM, so I have an xml file doing this:
content.xml
<rel
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldDescription="HTML attribute to apply to the component."
fieldLabel="Rel"
name="./rel"/>
I've tried just duplicating how id is handled, along with a million other things...
button.html
<button
data-sly-use.button="com.adobe.cq.wcm.core.components.models.Button"
data-sly-element="${button.buttonLink.valid ? 'a' : 'button'}"
type="${button.buttonLink.valid ? '' : 'button'}"
id="${button.id}"
rel="${button.rel}" <--THIS DOES NOT WORK
class=""
data-sly-attribute="${button.buttonLink.htmlAttributes}"
aria-label="${button.accessibilityLabel}"
data-cmp-clickable="${button.data ? true : false}"
data-cmp-data-layer="${button.data.json}">
<span data-sly-test="${button.text}" class="">${button.text}</span>
</button>
You can use the properties object with a HTL context attribute.
<button rel=${properties.rel # context='attribute} </button>
this was the answer
rel=${properties.rel}
I have a basic SpringBoot 2.0.5.RELEASE app. Using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have this template, where I want to disable a select object based on a condition
<form id="menuFormId" class="form-style-9" action="#" th:action="#{/menu/save}" th:object="${menu}" method="post">
<ul>
<li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
<span th:text="${e.global}? '*' : ${e.fieldName}" ><b>The field name</b></span> : <span th:text="${e.message}" class="red">
<font color="red">The error message</font>
</span>
</li>
</ul>
<ul class="tab_form">
<li>
<select id="selectMenuId" th:field="*{resto}" th:classappend="${menu.id == null} ? disabled='disabled'">
<option value="0">PLEASE SELECT A MENU</option>
</select>
...
But I got this error:
Could not parse as expression: "${menu.id == null} ? disabled='disabled'"
Uhhhh, there are 2 issues:
1.) disabled is a attribute and not a class. Therefore, use the following snippet:
th:disabled="${menu.id == null}"
2.) You can't define a class disabled='disabled'.
The second issue isn't important. There is no need for such a class definition.
Lets assume below sudo code, As you professionals can see I want to use Ternary kind of condition with ng-if to apply different HTML title attribute.
I tried it as below but it did not work
<td>
<span ng-if="isOk == true ? title ="Is Ok" : title ="Is Not Ok"></span>
</td>
I know I can achieve what I want applying ng-if in <td> level using below code
<td ng-if="isOk == true">
<span title ="Is Ok"></span>
</td>
<td ng-if="isOk != true">
<span title ="Is Not Ok"></span>
</td>
But I want to know weather I can use less code like Ternary check and achieve what I want with ng-if?
I thank you professionals in advace
As it was specified in this thread: if else statement in AngularJS templates, you can use ternary condition this way:
<span title="{{isOk ? 'Is Ok' : 'Is Not Ok'}}"></span>
Is it possible to add multiple attributes based on *ngIf?
My pseudo Code:
<span *ngIf="msg.active" *ngIf="msg.error" >Hallo</span>
And my output should be like this:
If msg.error == false and msg.active== true then it should be like this:
<span>Hallo</span>
If msg.error == true then it should be like this:
<span class="error" myTag="false" myTag2="false" >Hallo</span>
If msg.active == false then the span tag should be empty!
Does anybody have an idea?
i think you should try using nested ternary operator.
<span *ngIf="msg.active==true && msg.error==false? true: msg.active"> hallo <span>
try your condition using ternary operator. hope this will work
for myTag you have to use separate *ngIf
altogether in a single line it is possible as shown here,
DEMO : https://plnkr.co/edit/eKDhkuf3JO8CIKfz7Eqz?p=preview
<span *ngIf="msg.active || msg.error"
[class.error]="msg.error">
{{(msg.active==false)?'':'hello'}}
</span>
I have a sql statement that pulls infomration about tagnum's for individual pidm's. Every pidm can have multiple tagnum's so I am dropping the information into an arraylist. I want to display the contents of this arraylist in a dropdown box on a html page.
Here is the code for the arraylist:
<table style="border:transparent" style="width:100%">
<tr>
<td style ="width: 300px;">
<select style="width:150px;"tabindex="5" name="Tag">
<option></option>
<%} rscheck.close();
ResultSet rsTagCheck = stmt.executeQuery("SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");
while (rsTagCheck.next()){
ArrayList<String> myTag = new ArrayList<String>();
myTag.add(rsTagCheck.getString("XKRPRMT_TAG"));
%>
<option><%= myTag.get(0) %></option>
</select>
</td>
I can get the first element to show in the drop down box, but anything after that show an outofbounds exception. I want to know how to display ALL of the information in the arraylist.
#Pointy I did that and all I got was this:
It put the first one in there, but the rest would not populate!!
There's no reason to create the array list at all.
while (rsTagCheck.next()) {
%>
<option><%= rsTagCheck.getString("XKRPRMT_TAG") %></option>
<%
}
edit — of course in practice you should be careful about what those strings might contain. If the strings come from some sort of user input, you shouldn't be just dumping them unwashed into the HTML. That's a whole other subject however.
Don't use scriptlets, use jstl tags and in this case
<c:forEach var="myTag" items="${rsTagCheck}">
and
<c:out value="${myTag.getString('XKRPRMT_TAG')}" />
Actually looking again at your code, I would not put the db query in a scriptlet! DB access should not be done here, pass the resultsets to jsp from servlet, and loop through data using jstl.