Parallel loops in Thymeleaf - thymeleaf

This is my code, it does not work. I know I can not put the loops that, but how should they be to get the logic done
<tr th:each="max:${top3max}", th:each="min:${top3min}">
<td th:text="${max.getName()}"></td>
<td th:text="${min.getName()}"></td>
</tr>

As long as both Lists are the same size, you can loop through one and use the status variable to access the other. Like this:
<tr th:each="max, i: ${top3max}">
<td th:text="${max.getName()}"></td>
<td th:text="${top3min[i.index]}"></td>
</tr>
If you want something more like a traditional for loop, this will work (as long as top3max is a List -- you'll have to use .length instead of .size() if you're dealing with an array.
<tr th:each="i: ${#numbers.sequence(0, top3max.size() - 1)}">
<td th:text="${top3max[i]}"></td>
<td th:text="${top3min[i]}"></td>
</tr>

Related

Import table from html into google sheets

I have the following table element from a website.
Using this formula it only extracts the 1st td ie class=TTRow_left
I want to extract both class=TTRow_left and class=TTRow_right in a google sheet
Formula:
IMPORTHTML("https://www.bsesme.com/","table",6)
Html:
<table width="305" border="0" cellspacing="0" cellpadding="0">
<tbody><tr>
<td class="TTRow_left" style="height:22px;" width="230px">No. of Companies Listed on SME till Date</td>
<td class="TTRow_right" style="height:22px;" id="AL">386</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;" width="230px">Mkt Cap of Cos. Listed on SME till Date (Rs.Cr.)</td>
<td class="TTRow_right" style="height:22px;" id="MCL">58,225.56</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;" width="230px">Total Amount of Money Raised till Date (Rs. Cr.)</td>
<td class="TTRow_right" style="height:22px;" id="Td13">4,132.16</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;" width="230px">No. of Companies Migrated to Main Board</td>
<td class="TTRow_right" style="height:22px;" id="MB">150</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;" width="230px">No. of Companies Listed as of Date </td>
<td class="TTRow_right" style="height:22px;" id="CL"> 236</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;">No. of Companies Suspended</td>
<td class="TTRow_right" style="height:22px;" id="CS">32</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;">No. of Companies Eligible for Trading</td>
<td class="TTRow_right" style="height:22px;" id="CET">201</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;">No. of Companies Traded</td>
<td class="TTRow_right" style="height:22px;" id="CT">110</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;">Advances/ Declines/ Unchanged</td>
<td class="TTRow_right" style="height:22px;" id="Adv">73/ 32/ 5</td>
</tr>
<tr>
<td class="TTRow_left" style="height:22px;">Mkt Cap of BSE SME Listed Cos. (Rs.Cr.)</td>
<td class="TTRow_right" style="height:22px;" id="Dec">15,095.93</td>
</tr>
<!--<tr>
<td class="TTRow_left" style="height:22px;" width="230px">No. of SME companies migrated to main board</td>
<td class="TTRow_right" style="height:22px;" >3</td>
</tr>-->
</tbody></table>
</td>
</tr>
</tbody></table>```
There is a way, You could extract that data with Google Apps Script - i.e. writing a function that reads the values (those are returned by a separated request).
You need to make a request to this url - which is the one that loads the data:
https://www.bsesme.com/markets/MarketStat.aspx?&292022849
Values are:
bse$#$237|32|202|104|58|37|9|15,110.69|12|3,364.25|150|387|58,387.68|4,144.97
And then, extract the data.
I check the page's source code and that page is using javascript for read the data and rearrange it on the main page (i.e. https://www.bsesme.com/).
Tip: Check the main page's source code and check a function called function GetNotices(str) - that function looks like has the logic for rearrange the data.
You will have to check deeper in order to figure out how you can extract this data on your spreadsheet.
In this case IMPORTHTML would be able to return table data as long as it's not JavaScript generated, I tried checking the web page you are trying to scrap data from and it seems that the exact content that is missing is generated through JavaScript as it's not shown when disabled from the browser:
As you can see when JavaScript is disabled the content in the page is not displayed however the Table content TTRow_left is hard coded that's why the function is able to get this information from the web page:
td class="TTRow_left" style="height:22px;" width="230px">No. of Companies Listed on SME till Date'
You will notice that TTRow_right is not displayed therefore the function won't be able to scrap data from it.

How to display blank data in Thymeleaf in th:text

I am trying to use Thymeleaf if and unless but it is not working. What I am trying to do is I want to check if the statusNm variable has a value of 'Approved' then it will display the content otherwise it will leave it blank.
But I am not able to achieve it. Please suggest me a solution.
<th>Approved Date</th>
<td th:if="${obj.statusNm}=='Approved'" th:text="${obj.modifyDt}"></td>
<td th:unless="${obj.statusNm}" th:text=""></td>
<th>Approved By</th>
<td th:if="${obj.statusNm}=='Approved'" th:text="${obj.modifier}"></td>
<td th:unless="${obj.statusNm}" th:text=""></td>
As the alternative, you may look at Conditional expressions and evaluate your condition directly in th:text attribute ...
<th>Approved Date</th>
<td th:text="${obj.statusNm=='Approved'} ? ${obj.modifyDt}"></td>
<th>Approved By</th>
<td th:text="${obj.statusNm=='Approved'} ? ${obj.modifier}"></td>
The equals check must be inside the curly brackets like this:
<th>Approved Date</th>
<td th:if="${obj.statusNm=='Approved'}" th:text="${obj.modifyDt}"></td>
<td th:unless="${obj.statusNm=='Approved'}" th:text=""></td>

Is commenting faster than th:remove="all-but-first"?

There's an example in the Thymeleaf docs that I'm curious about.
Is commenting out a block using Thymeleaf-style commenting faster than using th:remove="all-but-first"?
Example:
<table>
<tr th:each="user : ${users}">
<td th:text="${user.name}">Jamie Dimon</td>
</tr>
<!--/* Hidden from evaluation -->
<tr>
<td>Jeff Bezos</td>
</tr>
<tr>
<td>Warren Buffett</td>
</tr>
<!--*/-->
</table>
vs.
<table th:remove="all-but-first">
<tr th:each="user : ${users}">
<td th:text="${user.name}">Jamie Dimon</td>
</tr>
<tr>
<td>Jeff Bezos</td>
</tr>
<tr>
<td>Warren Buffett</td>
</tr>
</table>
In both cases, prototyping would show the same HTML, but I am wondering whether the low precedence of th:remove would make it less desirable since it would be removing the tags after evaluating the th:each.

How to iterate through a list to find a match in thymeleaf?

How can I rewrite the script below with out specifying the index[x]? and allow thymeleaf to iterate the whole list to see if it find a match of test.host == server.host.
<tr th:if="${server.host == test[1].host}">
<td th:text ="${test[1].Status}"></td>
<td th:text="${test[1].host}"></td>
<td th:text="${test[1].version}"></td>
</tr>
It's not working the way I wanted it to work. It worked when I used index[x] to specify a host. I wanted it to iterate through the whole list. If test.host matches server.host. I don't want to specify an index[x]
I think you can iterate using th:each on your collection/ array
<th:block th:each="item: ${test}">///item is a var name, test is your array
<tr th:if="${server.host == item.host}">
<td th:text="${item.Status}"></td>
<td th:text="${item.host}"></td>
<td th:text="${item.version}"></td>
</tr>
</th:block>

Getting data in <tr> with thymeleaf

This is my code fragment:
<th:block th:each="d : ${items}">
<!--<tr>-->
<td th:text="${d.productname}"></td>
<td th:text="${d.quantity}"></td>
<td th:text="${d.price}"></td>
<!--</tr>-->
</th:block>
items is an ArrayList.
I can get data if the tr doesn't exist (as you can see I commented it in the code).
If I put them back, my th:text gets null values.
I also tried tr th:each but it didn't help.
How can I solve this problem? Thank you.
I am using thymeleaf-3.0.0 ,It worked for me.
<table>
<tr th:each="d : ${items}" >
<td th:text="${d.productname}"></td>
<td th:text="${d.quantity}"></td>
<td th:text="${d.price}"></td>
</tr>
</table>

Resources