Thymeleaf - how to condition format date value if exists? - thymeleaf

I have a table that shows dates, but some date values are null or blank. I'd like to format them if they exist and display nothing if there's no value.
I was considering something like:
(date != '' ? ${#calendars.format(date,'dd-MM-yyyy HH:mm')} : '')
Is this possible? How?

Maybe you have to try something like this?
<table>
....
<td>
<span th:if="${date != null}"
th:text="${#calendars.format(date,'dd-MM-yyyy HH:mm')}">
</span>
</td>
....
</table>
If you have not null value in date it will shown another it will be blank table cell.

The shortest way I think is this:
<td th:text="${#date} ? ${#calendars.format(date,'dd-MM-yyyy HH:mm')}" />

Related

Accessing the name of a smarty variables

Hello i need some help with a basic question. This is my code:
{foreach $sArticle.attributes.core->toArray() as $attribute}
<tr class="product--properties-row">
<td class="product--properties-label is--bold">{$sArticle.attributes.name}{$sArticle.attributesName}</td>
<td class="product--properties-label is--bold">{$attribute}</td>
</tr>
{/foreach}
1.Question: How do I enter the attributes name? I mean the column name from the attribute in the database?
2.Question: I only want to loop with the foreach through columns with name="artikelattr_" any idea how this can be done?
Thanks
I adjusted my answer here: https://stackoverflow.com/a/71237355/7201069
How to get the name of the attribute.
To consider only certain attributes that starts with a specific string, just add an if in the loop:
{foreach $sArticle.attributes.core->toArray() as $attributeName => $attribute}
{if $attributeName|strpos:"artikelattr_" === 0}
{$attributeName|var_dump}
{$attribute|var_dump}
{/if}
{/foreach}
You can simply use nearly all PHP functions in Smarty.

Convert thymeleaf to freemarker

Please help, i cann't find in freemarker guide how to convert from thymeleaf this:
lists.isEmpty and for each
<th:block th:if="${#lists.isEmpty(employees)}">
<h3>No employee</h3>
</th:block>
<th:block th:unless="${#lists.isEmpty(employees)}">
<tr th:each="contact,iterStat : ${employees}">
<td th:text="${iterStat.count}"></td>
<td th:text="${contact.name}"></td>
<td th:text="${contact.phone}"></td>
Thanks!
Maybe Something like this? (sketch, not tested)
<#list employees as contact>
<tr>
<td>${contact?index}
<td>${contat.name}</td>
<td>${contact.phone}</td>
</tr>
<#else>
<h3>No employee</h3>
</#list>
Notes
<#list> Will generate a <tr> element for each item in the employees sequence, containing <td>'s for each field.
If the employees sequence is empty it will generate the <h3> element.
See List Directive Doc
It gets the zero based index of the item using the build-in function
?index. See built-ins and loop variables in the help. Freemarker built-ins Doc. If you want one based, you can add one to it.
It's works
<#list employees as contact>
<tr>
<td>${contact?index}
<td>${contat.name}</td>
<td>${contact.phone}</td>
</tr>
<#else>
<h3>No employee</h3>
</#list>

Thymeleaf - if checked do something

I have a simple table and I have checkbox inside that table. If checkbox is selected I want to display other features.
My code:
<table>
<tr th:each="obj,iterationStatus : ${objs}">
<td th:text="${obj.name}"></td>
<td><input type="checkbox" name="firstcheckbox"></td>
<td th:if="isChecked">
<!-- do something -->
</td>
</tr>
</table>
How I do this without using javascript and backbean?
In this case you have two options, the first option you don't want but is not a bad option is using Javascript: creating a function called isChecked.
Otherwise, you can know if a checkbox is checked with thymeleaf in the case you are in a form which calls itself. You could do something like if(firstcheckbox != null) and, of course, your checkbox would have a value <input type="checkbox" name="firstcheckbox" value="value">. Finally, you must add as attribute in your model: model.addAttribute("firstCheckbox", firstCheckbox);

Capybara checkbox and click on link

I have a table like this with many numbers, below particular row from this table.
<tr>
<td>5100<td>
<td> Number description<td>
<td>long number description<td>
<td>
<input class="checkbox" type="checkbox" checked="" value="5100" name="id_rola[]">
<td>
<td>
<a href="javascript:documeny.frolazak_5100.submit();">
<b><img src="temp/img/foto.gif"></b>
</a>
<td>
<tr>
Whats i try to do was for this number check checkbox and next click link, but with this i have a problem. Firstly when i do check("5100") this show me unable to find checkbox "5100", and next when i try click link with find(:xpath, "//a[#href="javascript:document.frolazak_5100.submit();]").click , this show me unable to find xpath.
For any suggestion thx.
EDIT: ANSWERS
Check checkbox with particular value:
find(:css, ".checkbox[value='5100']").set(true)
Click on image in link in row:
To do this i wrote:
all('tr').each do |tr|
next unless tr.has_text?('5100')
#href = tr.all('td a')[0][:href]
end
visit #href
But this solution was be sloowly, so first i puts #href and then i could manipulate directly with params in this href and instead using method all simply visit this href. BTW why i dont think about it earlier :)

Hpple XPath query issue on IOS

I have the following HTML snippet:
<divv id="items">
<trr>
<td>
<p>Cars</p>
<a>Toyota</a>
<a>Opel</a>
<a>Audi</a>
</td>
</tr>
<tr>
<td>
<p>Planes</p>
<a>A320</a>
<a>B787</a>
<a>B767</a>
</td>
</tr>
<div/>
What I want is to create a XPath query so I can retrieve only the Cars.
Currently I am using this: //div[#id='items']/tr/td. But with this I get also the Plane items. I do not know how to test for the 'p' tag.
Anyone can help me ?
Thanks.
//div[#id='items']/tr/td[p='Cars']
The last predicate tests the existence of a <p> child element with Cars text content and thus filters out the <td> with <p>Planes</p>.
If picking the first group is enough, then you can use:
//div[#id='items']/tr/td[1]

Resources