Grails gsp: count of auto-number in list - grails

How do I get a count (auto-number) inside of an each loop in a gsp (groovy server page)? I'm doing a project of FAQ's. The following code works just fine, but I'd would like to number each question in the output like:
FAQ question here...
Answer here....
2, FAQ question here...
Answer here....
<g:each in="${gFaqCategory.list()}" var="faqCategory">
<p><b>${faqCategory.categoryType}</b></p>
<g:each in="${Faq.findAllByFaqCategory(faqCategory)}" var="faq">
<li><p>${faq.question}</p></li>
<li><p>${faq.answer} </li></p>
</g:each>
</g:each>

<g:each in="${Faq.findAllByFaqCategory(faqCategory)}" var="faq" status="i">
<li><p>${i+1}. ${faq.question}</p></li>
<li><p>${faq.answer} </li></p>
</g:each>
Use status in g:each. Strongly suggest to go through the doc.

Related

Render a Map of lists as a table in grails

I have a controller which is passing on a hashmap to a view. The map has 3 elements, one of which is as below:
-- A list of lists (A) in which list3 is a list of lists
Now I want to display them on the gsp as follows:
-----------------------------------------------------------------------------
A.list1 | A.list2 | A.list3[0] | A.list4 |
| | A.list3[1] | |
| | A.list3[2] | |
----------------------------------------------------------------------------
I have very little knowledge about generating views and I need nothing more than the tabular format here. Nothing more fancy. I just want the table because the data makes sense only in that format. Thank you.
welcome to the Grails world. Hope you have an awesome experience. Unsure if you are aware but by generating a view i.e. the default scaffolding CRUD elements auto generated and most specifically the list.gsp has most of that logic in place. None the less I reviewed your question over again and can see your stuck on map that has a list which is really simply stuff. So here goes.:
<g:each in="${A}" var="myMap">
<tr>
<td>${myMap.list1}</td>
<td>${myMap.list2}</td>
<td>
<g:each in="${myMap.list3}" status="i" var="myList3">
${myList3} <!-- this should be what you want I have added below -->
<!-- ${myList3?.name} --> <!-- explained further below -->
<!-- ${i} : ${myList3} --> <!--where i is this is the iterator -->
</g:each>
</td>
<td>${myMap.list4}</td>
</tr>
</g:each>
I put in comments myList3?.name simply because if the list is actually a binding of domain objects then you could display the element from the domainclass that is that being returned.
so:
class Country {
String name
static hasMany=[cities:Cities]
}
Class Cities {
String name
static belongsTo=[Country:country]
}
Then if A was ${country} and list3 was cities then .name would actually show the cities.name which is the binded value...
In other examples of raw maps that are not domainClass binded being returned to a gsp you could use the key value definition to parse:
raw map being passed to gsp
private Map menuMap = ['index':'Socket method', 'ajaxpoll':'Ajax poll',
'socketremote':'RemoteForm Websocket', 'scsocket':'NEW: Websocket Client/Server']
navbar gsp
<g:each in="${menuMap }" var="cmenu">
<li <g:if test="${actionName == cmenu.key}">class="active"</g:if>>
<g:link action="${cmenu.key}">
${cmenu.value}
</g:link>
</li>
</g:each>

Counters in Loops in Thymeleaf

Is there a way to do a loop in Thymeleaf without a list?
I'd like to essentially convert this snippet to Thymeleaf:
<jsp:useBean id="now" class="java.util.Date" />
<fmt:formatDate var="year" value="${now}" pattern="yyyy" />
<c:forEach var="i" begin="0" end="99">
<form:option value="${year-i}" />
</c:forEach>
</form:select>
-- Update --
I've decided this is along the lines of how I want to do it, but I'm not sure about the springEL syntax:
<option th:each="i : ${#numbers.sequence( 1, 100)}" th:value="#{ T(java.util.Date).getYear() - $i }">1</option>
In case you are still looking for the correct SpEL syntax,
here's what worked for me:
<option th:each="i : ${#numbers.sequence( 1, 100)}"
th:value="${ (new org.joda.time.DateTime()).getYear() - i }"
th:text="${ (new org.joda.time.DateTime()).getYear() - i }">1</option>
Notice:
added th:text to set the option text.
used Joda-Time instead as java.util.Date wouldn't give me the desired outcome
Read this discussion on java.util.Date and getYear()
You can use the special thymleaf iteration variable inside the each block.
This special variable name is the name of your element variable concatenate with the keyword 'Stat' (ex: elt -> eltStat)
This variable gives you many information related to the iteration.
You can also specify this variable name after your element variable. For example:
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
More information in the official documentation below:
http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#keeping-iteration-status

How to get the checked Checkbox values from GSP to java script

Hi I am new to grails and GSP
I have a code like
<g:each var="i" in="${typeList}">
<g:if test="${i != null}">
<tr>
<td><input type="checkbox" name="categoryType" id="categoryTypeCB" class="categoryTypeCB" value="${i}"> ${i}</td>
</tr>
</g:if>
</g:each>
How to get the values of checked check boxes in java script
Try to use jQuery. Since Grails 2.0 it's provided by default, you just have to add in your gsp template at the end of head tag with following line:
<r:require module="jquery" />
Or if you do not use resources plugin, include jQuery with following line:
<g:javascript library='jquery' />
And then in a javascript block go with:
<g:javascript>
var checkedCheckboxes = $('.categoryTypeCB:checked');
$.each(checkedCheckboxes, function(index, checkbox) {
var theValue = checkbox.value;
});
</g:javascript>
The each funciton is a loop so you need to handle somehow 'theValue' each iteration. The checkbox argument contains the input element itself if you need it.
BTW. You shouldn't assign the same id for many inputs. It's incorrect. Id has to be unique for each HTML element among document tree.

Struts 2 iterator tag status index is not working

<s:iterator value="podTemplate.subTypeTemplates" status="subTemplate">
<s:textfield id='subType_type_#subTemplate.index' key="subType" label="Name"/>
</s:iterator>
#subTemplate.index is not getting replaced by index. However if I am doing
<s:property value="#subTemplate.index"> is working
That's because id attribute of textfield is of string type and string-type attributes are not interpreted as OGNL by default. As Steven said you have to force the interpretation by using %{} in your case subType_type_%{#subTemplate.index}.
My iterator looks like this:
<s:iterator id="list" value="optionList" status="rowStatus" >
I tried:
<td>
<input class="textInput required"type="text" name="optionList[${rowStatus.index}].option_id"/>
</td>
and it's the only one that worked.
All the following ones failed:
name="optionList[%{#rowStatus.index}].option_id"
name="%{optionList[#rowStatus.index].option_id}"
even
name="%{#rowStatus.index}"

Struts2 Customizing error message

I am using struts2 in my application and trying to diplay error message using "s:actionerror/". It diaplays fine but a dot(.) also appears with the error message which looks ugly and is displayed like list.
Is there any way to cuatomize the error message in struts2.
Thanks in advance.
Brian Yarger's answer is the most complete solution. The easiest solution on the other hand is to just use CSS and modify the li element.
JSP:
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionErrors/>
</div>
</s:if>
CSS:
li .errors { list-style: none; }
Another solution is to override the template for the default actionError output.
You can find the default templates in the struts2 core jar. If you pull out template.simple/actionerror.ftl, you can customize that. You can either come up with your own template and reference it in the s:actionerror tag with the template attribute, or you can keep the same name and put it in /template/simple and it will be used as the default.
Most of the templates are in freemarker, although there are still some of them in velocity depending on your struts2 version. Both are pretty easy to work with templating engines.
I guess the kozmic's solution should be:
JSP:
<s:if test="hasActionErrors()">
<div class="errors">
<s:actionErrors/>
</div>
</s:if>
CSS:
div .errors li { list-style: none; }
It didn't work for me with his CSS code.
Hi I found the solution for getting rid of the dot.
<table align="center" width="70%" class="stats">
<tr>
<s:if test="hasActionErrors()">
<s:iterator value="actionErrors">
<tr>
<td class="error">
<img alt="error message" src="./images/cross.gif" width="10" height="10"/> <s:property escape="false" />
</td>
</tr>
</s:iterator>
</s:if>
Try this out.
I know this is an old question, but I want to share my simple solution. This just prints out each error message without any extra generated html markup. Then you can wrap the <s:property value="%{error}"/> in some custom html if you want.
<s:if test="hasActionErrors()">
<s:iterator var="error" value="%{actionErrors}">
<s:property value="%{error}"/>
</s:iterator>
</s:if>

Resources