I am creating a project in eclipse where I want to display values input into a form in a table. I want the table to only display the rows which have values, but cannot work out how to do this. So far I have just added multiple rows and am using display:none in order to not display them, but I do not cannot think of a way to get the th:text="${name1}" to display.
Here is an example of the way I am doing it so far:
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name1}"></td>
<td class="tableCell" th:text="${description1}"></td>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name2}"></td>
<td class="tableCell" th:text="${description2}"></td>
</tr>
<tr id="tableRow" style="display:none">
<td class="tableCell" th:text="${name3}"></td>
<td class="tableCell" th:text="${description3}"></td>
</tr>
</table>
I am relatively new to coding and so any help would be appreciated.
you need to test values before consuming it
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" th:if="${name1 != null or description1 != null}">
<td class="tableCell" th:text="${name1}"></td>
<td class="tableCell" th:text="${description1}"></td>
</tr>
<tr id="tableRow" th:if="${name2 != null or description2 != null}">
<td class="tableCell" th:text="${name2}"></td>
<td class="tableCell" th:text="${description2}"></td>
</tr>
<tr id="tableRow" th:if="${name3 != null or description3 != null}">
<td class="tableCell" th:text="${name3}"></td>
<td class="tableCell" th:text="${description3}"></td>
</tr>
</table>
I found a good way to do it using th:each:
<table id="table">
<tr id="tableRow">
<th class="tableHeader">Name</th>
<th class="tableHeader">Description</th>
</tr>
<tr id="tableRow" th:each="inputMap : ${InputMap}">
<td class="tableCell" th:text="${inputMap.value.name}"></td>
<td class="tableCell" th:text="${inputMap.value.description}"></td>
</tr>
</table>
The ${InputMap} comes from my controller:
#GetMapping("/tablePage")
public void getTableData(Model model) {
inputRepository.findAll().forEach(inputObject -> {
inputMap.put(inputObject.id, inputObject);
});
if (inputMap != null){
inputMap.forEach((id, i) -> {
model.addAttribute(id + "name", id + i.getName());
model.addAttribute(id + "description", id + i.getDescription());
});
}
model.addAttribute("InputMap", inputMap);
}
Related
I have a JSP page that's attached to a list of objects I need to loop through using a JSTL loop. Basically, I want it to display different information about the book and a submit input view (send to another jsp page to see more details about the book).
My question is how can I know which button was clicked and how can I recover the corresponding book.
<table class="table table-striped">
<thead>
<tr>
<th scope="col">N°</th>
<th scope="col">Title</th>
<th scope="col">ISBN</th>
<th scope="col">Author</th>
<th scope="col">Year</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"></th>
<c:set var="count" value="${1}" scope="request"/>
<c:forEach items= "${requestScope.booksList}" var="b">
<tr>
<td>${count }</td>
<td> ${b.getTitle() } </td>
<td> ${b.getISBN() } </td>
<td> ${b.getAuthor() } </td>
<td> ${b.getYear() } </td>
<td><input type="submit" value="View" name="View${count }"></td>
<td><input type="submit" value="Mark" name="Mark"></td>
</tr>
<c:set var="count" value="${count + 1}" scope="request" />
</c:forEach>
</tr>
</tbody>
</table>
I have a table of reviews
<table class="table">
<tbody>
<tr th:each="review : ${reviewsForMovie}">
<td th:utext="${review.text}"></td>
</tr>
</tbody>
</table>
But I dont want to show review if review.isApproved == false
How do i do that?
<table class="table">
<tbody>
<tr th:each="review : ${reviewsForMovie}">
<td th:hidden="${!review.isApproved}" th:utext="${review.text}"></td>
</tr>
</tbody>
</table>
I want to click a text related input with preceding-sibling node my HTML is the following:
<FORM id="formid" onsubmit="" method=post name="formid" action=>
<TABLE width="100%">
<TR class=foo-even>
<TD rowSpan=3><INPUT onclick="" value=1 type=radio name="formid">upbutton1</TD>
<TD>Pearl</TD>
<TD rowSpan=3></TD>
</TR>
<TR class=foo-even>
<TD>ravenclawn</TD>
</TR>
<TR class=foo-even>
<TD>ravenclawn</TD>
</TR>
</TBODY>
</TABLE>
</TD></TR>
<TR>
<TD> </TD>
</TR>
</TBODY></TABLE>
<TABLE width="100%" >
<TBODY>
<TR>
<TD class="someclass"></TD>
</TR>
<TR>
<TD>
<TABLE class=foo width="100%" border="1px">
<TBODY>
<TR class=foo-header>
<TD></TD>
<TD><BR></TD>
<TD colSpan=3></TD>
<TD></TD>
</TR>
<TR class=foo-odd>
<TD><INPUT value=0 type=radio name="formid.samename">buttonclick1</TD>
<TD>1234</TD>
<TD>blue </TD>
<TD colSpan=2>apple2</TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"></TD>
</TR>
<TR class=foo-even>
<TD><INPUT value=1 type=radio name="formid.samename">buttonclick2</TD>
<TD>1235 </TD>
<TD>blue </TD>
<TD colSpan=2>apple3</TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"> </TD>
</TR>
<TR class=foo-odd>
<TD><INPUT value=2 type=radio name="formid.samename">buttonclick3</TD>
<TD>1235</TD>
<TD>sometext </TD>
<TD>Pearl</TD>
<TD></TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"> </TD>
</TR>
<TR class=foo-even>
<TD><INPUT value=3 type=radio name="formid.samename">buttonclick4</TD>
<TD>1236 </TD>
<TD>blue </TD>
<TD colSpan=2>apple4</TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"> </TD>
</TR>
<TR class=foo-odd>
<TD><INPUT value=4 type=radio name="formid.samename">buttonclick5</TD>
<TD>1236 </TD>
<TD>sometext </TD>
<TD>ravenclawn</TD>
<TD></TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"> </TD>
</TR>
<TR class=foo-even>
<TD><INPUT value=5 type=radio name="formid.samename">buttonclick6</TD>
<TD>1237 </TD>
<TD>blue </TD>
<TD colSpan=2>apple6</TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"> </TD>
</TR>
<TR class=foo-odd>
<TD><INPUT value=6 type=radio name="formid.samename">buttonclick7</TD>
<TD>1237 </TD>
<TD>sometext </TD>
<TD>ravenclawn</TD>
<TD></TD>
<TD> <INPUT value=0 type=hidden name="samenameagain"></tD>
</DIV>
My target is to click on second Pearl text related input field
Selenium code is the following:
driver.findElement(By.xpath(".//td[contains(text(),'Pearl')][1]/preceding-sibling::td")).click();
Problem:
Above code always working with first table that contains Pearl text
Observations:
That case when im modify first Pearl text to something else the code working fine
That case when im working with another text ect:"ravenclawn" its working fine without any modifications
Thanks for any advice
You just include index for table also.
For 1st table
By.xpath("//table[1]//td[contains(text(),'Pearl')]/preceding-sibling::td")
For 2nd table
By.xpath("//table[2]//td[contains(text(),'Pearl')]/preceding-sibling::td")
I have table where I want to iterate over rows (tr) in root tbody and not rows in it child
I try this but with no success:
Elements tableRows = table.select("tr");
this get tr also in it child so I have there duplicates
Elements tableRows = table.select("tbody > td:first-child");
this doesnt work too
here is my table:
<table border="0" cellspacing="0" cellpadding="2" width="100%" align="center">
<tbody>
<tr>
<td class="head">Rank</td>
<td class="Head"> </td>
<td class="Lhead">Club</td>
<td class="head">Nation</td>
<td class="head">Rank points</td>
<td class="head">Five best results (by points)</td>
</tr>
<tr>
<td class="playerB">60. (1.)</td>
<td class="Lplayer">Imrich Blasko</td>
<td class="Lplayer">THC Blue Dragon Kosice</td>
<td class="player"><img src="flags/SVK.gif" alt="SVK" /></td>
<td class="playerB">3 398</td>
<td class="Lplayer">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="Lplayer">World Championships 2013</td>
<td class="Rplayer">817</td>
</tr>
<tr>
<td class="Lplayer">Czech Open 2012</td>
<td class="Rplayer">674</td>
</tr>
<tr>
<td class="Lplayer">Czech Open 2013</td>
<td class="Rplayer">668</td>
</tr>
<tr>
<td class="Lplayer">Slovak Championships 2013</td>
<td class="Rplayer">621</td>
</tr>
<tr>
<td class="Lplayer">Bratislava Cup 2013</td>
<td class="Rplayer">617</td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td colspan="6">
<hr color="#008080" width="100%" /></td>
</tr>
<tr>
<td class="playerB">159. (2.)</td>
<td class="Lplayer">Tibor Valachovic ml.</td>
<td class="Lplayer">SHK Skalica</td>
<td class="player"><img src="flags/SVK.gif" alt="SVK" /></td>
<td class="playerB">2 847</td>
<td class="Lplayer">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="Lplayer">Czech Open 2013</td>
<td class="Rplayer">639</td>
</tr>
<tr>
<td class="Lplayer">Brno 2013</td>
<td class="Rplayer">594</td>
</tr>
<tr>
<td class="Lplayer">Brno 2014</td>
<td class="Rplayer">583</td>
</tr>
<tr>
<td class="Lplayer">Slovak Championships 2013</td>
<td class="Rplayer">569</td>
</tr>
<tr>
<td class="Lplayer">Czech Open 2011</td>
<td class="Rplayer">461</td>
</tr>
</tbody>
</table></td>
</tr>
<tr>
<td colspan="6">
<hr color="#008080" width="100%" /></td>
</tr>
</tbody>
</table>
Try:
Elements tableRows = table.select(":root > tbody > tr");
I have a table as follows
<table id="vainTbl6" class="dtable" cellpadding="3" cellspacing="1" border="0">
<thead>
<tr><th>check</td><th>Morphosal</th><th>goat </th><th>the other</th></tr>
</thead>
<tbody id="tbdy">
<tr class="gradeX">
<td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%">-Approve the femuneration Gommiqee purpirt</td>
<td>pontpose</td></tr>
<tr class="gradeX">
<td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%">-Declare a final truce</td>
<td>More</td></tr>
<tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%">-Amend the articles of asscotonation</td>
<td>Four</td></tr>
<tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%">-Re-elect Bandanna la banana for warden</td>
<td>Floor</td></tr>
</tbody>
</table>
and the first column is a checkbox. I need to fetch all the second column cells where the checkbox in the first column is set to true
I tried using this, but to no avail.
function extractRowCell(divNode){
alert(divNode.id);
$('#tbdy tr td').each(function() {
alert('hello');
var aRowData = this.cells
alert(aRowData[1].firstChild.value);
return aRowData;
});
}
The call is as follows:
<a id="la" href='#' onclick='extractRowCell(this.parentNode)' style="position:absolute; top:280px; left:350px;">Votes & Concerns</a>
The alert in the function triggers though with the correct value.
TIA
To iterate over the td's simply do the following.
$('#tbdy tr td').each(function() {
alert( $(this).text() );
});
You can do this
<table><tbody id="tbdy">
<tr class="gradeX">
<td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%" class="item">-Approve the femuneration Gommiqee purpirt</td>
<td>pontpose</td></tr>
<tr class="gradeX">
<td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%" class="item">-Declare a final truce</td>
<td>More</td></tr>
<tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%" class="item">-Amend the articles of asscotonation</td>
<td>Four</td></tr>
<tr class="gradeX"><td><input type="checkbox" class = "chcktbl" /></td>
<td style="width:55%" class="item">-Re-elect Bandanna la banana for warden</td>
<td>Floor</td></tr>
</tbody>
</table>
<button>Get</button>
<script>
$("button").click(function(){
var ret="";
$("#tbdy tr td:first").each(function()
{
if($("input:checked").length!=0)
{
ret=$("input:checked").parent().parent().find("td.item").text();
}
});
alert(ret);
});
</script>