Populating inner table inside another table with Thymeleaf - java

I have DTO class:
private LocalDate date;
private List<String> firstShift;
private List<String> secondShift;
private List<String> thirdShift;
With getters, setters and toString for every field.
I'm creating this table for shift schedule calendar:
My Thymeleaf:
<table class = "table table-striped table-hover" id = "schedule_table">
<thead>
<tr>
<th class = "th_schedule_table">Date</th>
<th class = "th_schedule_table">First Shift</th>
<th class = "th_schedule_table">Second Shift</th>
<th class = "th_schedule_table">Third Shift</th>
</tr>
</thead>
<tbody>
<tr th:each = "calendar_node : ${calendarNodeList}">
<td th:text = "${calendar_node.date}"></td>
<td>
<table>
<tbody>
<tr th:each = "employee, i : ${firstShift}">
<td th:text = "${firstShift.[i]}"></td>
</tr>
</tbody>
</table>
</td>
<td>
<table>
<tbody>
<tr th:each = "employee: ${secondShift}">
<td th:text = "${employee}"></td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
</tbody>
</table>
The idea behind this: CalendarNode.date generates row for parent table, for each date. That works fine.
Inside every row, in a cell I have second table that should show list of employees who works on that date in that shift. If I have calendar_node object in one row, I'm using his "firstShift" field to generated rows for second inner table.
The problem is that I'm getting empty table. I checked my back-end and I have two employees for first date (18th July), first shift, one employee for second shift, but none is shown. I tried a lot of different syntax for Thymeleaf, none of it works. I guess I did Thymeleaf wrong?
UPDATE:
Example of data that has been passed to web page via model object:

If firstShift is a field of calendar_node, then you need to actually address it as such in the template:
${calendar_node.firstShift} instead of ${firstShift}

Related

Pull array from table back into controller to create or update new array

So I have a table generated using EL that is requested from a Java class
<table id="medTable" class="goldTable">
<thead>
<tr id="titles">
<td>Resident SSN</td>
<td>Medication ID</td>
<td>Quantity Received</td>
</tr>
</thead>
<c:forEach var="item" items="${allMeds}">
<tr>
<td <!--onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;"-->${item.resSSN}</td>
<td <!--onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;"-->${item.medID}</td>
<td onclick="listenForDoubleClick(this);" onblur="this.contentEditable=false;">${item.quantity}</td>
</tr>
</c:forEach>
</table>
The problem, as you can see is the quantity of this table is editable for correction purposes. Now the issue is I don't know how to receive this new information inside my controller to create a new, updated array...This is what my controller looks like for the "update" action...
} else if (request.getParameter("update") != null) {
String[] residentSSN = request.getParameterValues("resSSN");
//pull from html into multiple arrays
//push back to html, but content uneditable
String[] medicationID = request.getParameterValues("medID");
String[] quantity = request.getParameterValues("quantity");
//int quantityNum = Integer.parseInt(quantity);
//allMeds = new ArrayList<OrderedMeds>(residentSSN, medicationID, quantityNum);*/
url = "/ReceiveMeds.jsp";
}
This is what I've tried and clearly it doesn't work. Please help!

jstl loop through a list in an object

I am building a Spring MVC web app, I have an object called NodeRel which is defined as below:
public class NodeRel {
private String fromNodeId;
private String toNodeId;
private String fromNodeName;
private String toNodeName;
private List<QuotaValueOnTime> fromNodeSend;
private List<QuotaValueOnTime> toNodeSend;
//getters and setters omitted
}
In the server side code, i obtained a list of NodeRels and bind it to the model. In the jsp page, I want to first loop through the List and then inside it, I want to loop though List. My jsp code:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="center">Count</th>
<th>relation</th>
<th colspan='25'>Detail</th>
</tr>
</thead>
<tbody>
<c:forEach var="nodeRel" items="${nodeRelInfo}" varStatus="stc">
<tr>
<td rowspan="3">${stc.count}</td>
<td rowspan="3">${nodeRel.fromNodeName} --> ${nodeRel.toNodeName}</td>
<td>\</td>
<c:forEach var="x" begin="0" end="23" step="1">
<td>${x}</td>
</c:forEach>
</tr>
<tr>
<td>Send_A</td>
<c:forEach var="node" items="${nodeRelInfo.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
</tr>
<tr>
<td>Send_B</td>
<c:forEach var="x" begin="0" end="23" step="1">
<td>${x}</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
</div>
My code doesn't work and I got java.lang.NumberFormatException: For input string: "fromNodeSend" near the second loop:
<c:forEach var="node" items="${nodeRelInfo.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
What is wrong with my code?
Note that the variable ${nodeRelInfo} represents the List and the variable ${nodeRel} represents the each item you work with.
Thus the item you want to loop in the second loop is ${nodeRelInfo.fromNodeSend}. Change the second name of variable looped:
<c:forEach var="node" items="${nodeRel.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
It works on the same logic like Java for-each loop.
for (List nodeRel: nodeRelInfo) {
// bla blaa
for (String node: nodeRel.fromNodeSend()) {
System.out.println(node);
}
}
change your second loop like this because your variable name in parent loop is nodeRel not nodeRelInfo
<c:forEach var="node" items="${nodeRel.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>

HREF + TEXT with Jsoup

I've the following HTML Page:
</div><div id="page_content_list01" class="grid_12">
<h2><strong class="floatleft">TEXT1</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Attachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example.com">TEXT2</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT3</strong></h2><br>
<table>
<tbody>
<tr>
<th class="no_width">
<p class="floatleft">Atachments:</p>
</th>
<td class="link_azure">
<a target="_blank" href="http://www.example2.com">TEXT4</a><br/>
</td>
</tr>
</tbody>
</table><h2><strong class="floatleft">TEXT5</strong></h2><br>
<table>
<tbody>
<tr>
Actually I'm doing:
Elements rows = document.select("div#page_content_list01");
Now I to select "TEXT" and link. I wanna to make clickable link, so I'm using:
for (Element eleme : rows) {
Elements elements = eleme.select("a");
for (Element elem : elementi) {
String url = elem.attr("href");
String title = elem.text();
}
}
and I'm getting:
url = "http://www.example.com";
title = "TEXT2";
and it's ok, but in this way I can't read "TEXT1" and "TEXT3".
Can someone help me please?
I think you need to work on the selecors. First, your primary selector
Elements rows = document.select("div#page_content_list01");
will return with a list of ONE element only, since you actually select the div, not the tables or table rows. I would instead do this to get all relevant info:
Elements tables = document.select("div#page_content_list01>table");
for (Element table : tables){
Element h2 = table.previousElementSibling();
String titleStr = h2.text();
Element a = table.select("a").first();
String linkStr = a.attr("href");
}
Note that the Text in the h2 elements is on the same level as the table, not inside a common div. This is why I use the previous sibling notation. Also note that I wrote this out of my head and it is untested. You should get the idea though.

How to get an old ID and insert into a new text field

I am working on a scenario, where I create a new user. Since, I will be creating tons of new users for regression, I am assigning a time-stamp to distinguish each user id. For instance, ABC_2015020615215, ABC_2015020615222, etc.
The code for creating and sending a user with the time-stamp:
public static String now = "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
// user
public static String user_id = PropertyLoader.loadProperty("USER_ID") + now;
Next scenario: I need to grab the last user added, and insert into a new text field, on a different page. (remaining on the same driver session).
This code creates a new user:
//enter user id in the text field and add a new user
driver.findElement(By.name(PvtConstants.ADD_USER_ID_TEXT_FIELD)).sendKeys(user_id);
This is a new text field, where I need to insert the user that was added in the previous step. I am not able to figure that out. Thanks in advance for the help!
driver.findElement(By.id(PvtConstants.READ_USER_ADVANCED_FILTER_USER_SEARCH_FIELD));
HTML:
<table class="table smallInput dataTable" id="dataTableAdvertisers" ">
<thead>
<tr role="row" style="height: 0px;">
<th class="sorting_asc" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
<th class="sorting" tabindex="0" "></th>
</tr>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
<tr class="odd">
<td class="">
Advertiser_20150204130817</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td>
</tr><tr class="even">
<td class="">
Advertiser_20150204130923</td>
<td class="">---</td>
<td class="">---</td>
<td class="">---</td><td class="">---</td>
</tr><tr class="odd">
</tbody>
</table>
I will use a Xpath and with passing the user_id as parameter.
Note: this xpath matches the any a tag with the text supplied by the user_id variable which in your case is the lastly instantiated variable with timestamp.
By byXpath = By.xpath("//a[.='" + user_id + "']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byXpath));
String newUser = myDynamicElement.getText().trim();
//send newUser this to the search field

Values of <td></td> is not returned using HtmlUnit

I am having a table structure in a web page as below
In this i am trying to get all the values in the tr using the HtmlUnit code as below.
<tr class="odd" id="rawmeta-123">
<td class="rowid">1</td>
<td>Arun</td>
<td class="date">2006-04-13</td>
<td></td>
<td class="date"></td>
<td></td>
<td></td>
</tr>
List byXPath = page2.getByXPath("//tr[#class='odd']/td/text()");
It returns the first three and fifth td values. But the values for the for the other are not returned.(
<td></td>). How to get this value as null using HtmlUnit?
I highly appreciate your comments.
Are you looking for something like this : ?
HtmlTableRow tr = (HtmlTableRow) page.getByXPath("//tr[#class='odd']/");
for (HtmlTableCell td: tr.getCells() ) {
if (td.getTextContent().isEmpty()) {
// this is null
}
}

Categories

Resources