Grails inside a <g:each> who creates rows change class of a <tr> - grails

I have my grails view, and using a api rest I Receive an array of items,that I put in a table,and I want to make the pair rows with tr class="alt">
I want for first for second ,and so on.
I was thinking to do that with a or creating a tag,but donĀ“t know really if there is something useful that can use for similar cases.
The tr that I want to change In the code is as
<table id="customers">
<tr>
<th>Foto</th>
<th>Nombre</th>
<th>Vendedor</th>
<th>Precio</th>
<th>Estado</th>
<th>Ciudad</th>
</tr>
<g:each in="${results}">
**<tr >**
<td><img src="${it.thumbnail}"></td>
<td>${it.title}</td>
<td>${it.nombre_vendedor}</td>
<td>${it.price}</td>
<td>${it.condition}</td>
<td>${it.address.state_name}</td>
</tr>
</g:each>
</table>

<g:each in="${results}" status="ix">
<tr class="${ix % 2 ? 'light' : 'dark'}">
<td>...
</tr>
</g:each>

Related

Last Element in List in Thymeleaf

I have a list in Thymeleaf and I want to use th:with in a div to grab the last element in a list. I've tried:
<div th:if="${not #lists.isEmpty(licence.registrations)}"
th:with="lastRegistation=${licence.registrations[__(#lists.size(licence.registrations) - 1)__]}">
<tr>
<td>Name</td>
<td><span th:text="${lastRegistration.name}"/></td>
</tr>
</div>
However this gives me the error:
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "(#lists.size(licence.registrations) - 1)" (template: "admin/viewLicence.html" - line 103, col 10)
Does anyone know a way I can get the last item in a list with Thymeleaf?
Cheers,
Neil
If you're using preprocessing, you need to surround the expression with ${...}. Like this:
th:with="lastRegistration=${licence.registrations[__${#lists.size(licence.registrations) - 1}__]}"
That being said, there is no reason to use preprocessing in this case. (And I would remove the extra unused tags as well.) This will work:
<tr th:if="${not #lists.isEmpty(licence.registrations)}"
th:with="lastRegistration=${licence.registrations[#lists.size(licence.registrations) - 1]}">
<td>Name</td>
<td th:text="${lastRegistration.name}" />
</tr>
You can also get fancy with collection selection & projection, but I'm not sure this is an appropriate use. Still, it seems to work:
<tr th:if="${not #lists.isEmpty(licence.registrations)}">
<td>Name</td>
<td th:text="${licence.registrations.![name].$[true]}" />
</tr>

How to iterate two objects at a time by using thymeleaf?

I am new to thymeleaf. I am trying to iterate two objects at a time on table. Because i am getting value from two objects.
I tried my way but it not working properly.
<tbody th:each="trn,iterStat : ${avngs}">
<tr th:each="emp,iterStat : ${list}">
<td th:text="${emp.name}">Name</td>
<td th:text="${emp.address}">VPA</td>
<td th:text="${emp.accountno}">Accont No</td>
<td th:text="${trn.daylimit}">Yes</td>
<td th:text="${trn.weeklimit}">Yes</td>
<td th:text="${trn.monthlimit}">Yes</td>
</tr>
</tbody>
Here, emp.name,address,accountno are getting correctly. But trn.daylimit,weeklimit,monthlimit are getting only same to remaining records
What is wrong in my code?
Make some meaning out of this. I have two lists, part1 and part2, I get values of part 2 from the index of part 1. This considers that your part 1 and 2 are supposed to display values in a linear - side by side manner. I got my parts by splitting a string.
<tr th:each= "part1, status : ${part1}">
<td th:text="${status.index + 1}"></td>
<td th:text="${part1}"></td>
<td th:text="${part2.get(status.index)}"></td>
</tr>

dartlang: how do you highlight table rows?

I'm new to dart so this might seem like a silly question but I'd like to apply different CSS to different rows based on column values:
<table id="requests">
<tr>
<td>Title</td>
<td>Status</td>
<td>Date</td>
</tr>
<tr>
<td>Foo</td>
<td>Approved</td>
<td>Date</td>
</tr>
<tr>
<td>Bar</td>
<td>Denied</td>
<td>Date</td>
</tr>
<tr>
<td>Ipsum</td>
<td>None</td>
<td>Date</td>
</tr>
</table>
So far I can do this:
queryAll("tr td").classes.add("foo");
But I'm wondering how to get all rows whose columns have the value "Approved".
Here is a quick dirty way to do it from code:
querySelectorAll('tr').where(
(tr)=> tr.children.any(
(td)=>td.text == 'Approved'
)
)
.forEach((approvedTr)=>approvedTr.classes.add('approved'));
But i really recommend you to use a databinding framework (like Polymer, or AngularDart) to set different styles/classes depending on data. It's far more easy to use.
[A new thread was opened about doing this using Polymer]

tableSorter pager not updating after search

I have tablesorter and pager working together and had no problems so far.
Only thing that I can't manage to do correctly is update the pager after a search.
<table>
<thead>
<tr>
<td>Name</td>
<td>Age</td>
<td>Country</td>
</tr>
</thead>
<tbody>
<tr>
<td>Al</td>
<td>18</td>
<td>Germany</td>
</tr>
<tr>
<td>BAl</td>
<td>19</td>
<td>Italy</td>
</tr>
<tr>
<td>CAl</td>
<td>99</td>
<td>Sweden</td>
</tr>
<tr>
<td>DAl</td>
<td>65</td>
<td>Australia</td>
</tr>
<tr>
<td>EAl</td>
<td>1</td>
<td>Germany</td>
</tr>
</tbody>
I initialize the sorter and the pager to display only 2 rows per page, but after searching for 99 year old person still pager will show 1/3 and not 1/1 which is the correct!
Pager still works, is that visual total page count that is wrong!
Any tips?
I am not sure if this will help or not, but I have it working on mine.
Super important note, all the examples the Official Sorter page has does not use a ID of the table, it just says any table tag. (add the # to the reference)
$("#caseInfoTable").tablesorter({widthFixed: true, widgets: ['zebra']});
$("#caseInfoTable").tablesorterPager({container: $("#pager"), positionFixed: false});
[1]
In the jquery.tablesorter.pager.js file I added these two lines at line 110...
c.pagerPositionSet = false;
fixPosition(table);
Those 2 go right before the line
updatePageDisplay(table);
[1] https://forum.jquery.com/topic/jquery-problem-with-tablesorter-pager-plugin-there-should-be-an-option-to-turn-off-absolute-positioning-of-pager-container/

Grails: GSP variable substitution in g:each tag

I have a loop inside a loop and want to substitute the value of first var into second. Below is the code snippet.
<g:each in="${tables}" status="i" var="table">
<div class="tabletitle">
${table.name}
</div>
<table>
<thead>
<tr>
<g:each in="${${table.name}DisplayColumns}" status="k" var="displayColumn">
<td>${displayColumn}</td>
</g:each>
</tr>
</thead>
<tbody>
....
....
</tbody>
</table>
</g:each>
${table.name} substitution in second g:each tag is not working. Any idea to make it work?
Try this:
<g:each in="${evaluate(table.name+'DisplayColumns')}" status="k" var="displayColumn">
Interesting, I've never used evaluate inside a gsp, as Kelly suggests. But may I suggest a less optimal approach?
You can store ${table.name} inside a variable with <g:set> ( http://grails.org/doc/2.0.x/ref/Tags/set.html )
Do you know that you can pass any object to a GSP? Even maps (you're trying to emulate maps, I don't know why), and use it like:
<g:each in="${displayColumns[table.name]}">
where displayColumns is a Map that contains columns for each table.
Btw, more clean way, is to use special object, that includes this data. Something like TableDetails that have List<String> getColumns() method. So you can use
<g:each in="${tables}" var="table">
${table.name}
<g:each in="${table.columns}" var="column">
${column}
</g:each>
</g:each>

Resources