how to add list of objects to html table in spring MVC project? - spring

I'm really new to Spring MVC and Hibernate and this is my first project with java ee technologies. I’m stuck at this point for almost 2 days and I searched over internet but no luck. What I only got is how to represent list of objects in html table fetched from database(which I'm not looking for).
I have a one to many relationship in my project with FeedOrderDetails and Feed. Everything works perfectly fine except I can’t save many feeds with feedOrderDetails. Case is I can save only defined number of feed items in the my jsp page.
Here is a picture of my ER diagram.
This is the code in my jsp
<table>
<tr>
<td>Date</td>
<td><form:input path="date" /></td>
</tr>
<tr>
<td>Discount</td>
<td><form:input path="discount" /></td>
</tr>
<tr>
<td>Total</td>
<td><form:input path="total" /></td>
</tr>
<tr>
<td>Unit Price</td>
<td><form:input path="feedOrderDetail[0].unitPrice" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><form:input path="feedOrderDetail[0].quantity" /></td>
</tr>
<tr>
<td>Feed</td>
<td><form:input path="feedOrderDetail[0].feed.id" /></td>
</tr>
<tr>
<td><input type="submit" value="Add Feed"></td>
</tr>
</table>
</form:form>
What I want to achieve is I want to add many feeds to Feed Order and save it all together. Like I want to add feed Id, feed name ,quantity and unit price to html table and save all these details in one button click.
this is what I want to achieve.
I enter packing material ID,quantity and unit price and click add button to add those item to table. like that I want to add those item to a table in html form with path tag included in it.
for a example
first row should contain tag
<td><form:input path="feedOrderDetail[0].feed.id" /></td>
second row
`<td><form:input path="feedOrderDetail[1].feed.id" /></td>`
whenever I add items to table this should happen.

You should write some javascript to add form <input> elements with the names resembling to those produced with <form:input> tags and then submit them altogether. It involves a bit of js coding, once the form elements are properly set, springmvc will do the conversion into a parent+list of children. In this context, elements like <form:input path="feedOrderDetail[0].feed.id" /> will be redundant if there's one <form:input path="id"/>
You can see an example here.

I could solve this problem with javascript. hope it will help to someone else later.
<SCRIPT language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var counts = rowCount - 1;
var cell1 = row.insertCell(0);
var feed = document.createElement("input");
feed.type = "text";
feed.name = "feedOrderDetail[" + counts + "].feed.id";
cell1.appendChild(feed);
var cell2 = row.insertCell(1);
var unitPrice = document.createElement("input");
unitPrice.type = "text";
unitPrice.name = "feedOrderDetail[" + counts + "].unitPrice";
cell2.appendChild(unitPrice);
var cell3 = row.insertCell(2);
var quantity = document.createElement("input");
quantity.type = "text";
quantity.name = "feedOrderDetail[" + counts + "].quantity";
cell3.appendChild(quantity);
}
</SCRIPT>

Related

Html Agility Pack loop through Specific Row

I have a table like this
<table>
<thead>
<tr>
<th>Name</th>
<th>Department</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr id="data1">
</tr>
<tr>
</tr>
</tbody>
And I want to use Html Agility Pack to parse its specific row i.e i want to display row next to row which has id=data1
below is code I am trying ...
//Selecting Document Node....
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(data);
//Selecting Specific Node...
var tableNodes = doc.DocumentNode.SelectNodes("//table");
Your xpath should be like this:
//table/tbody/tr[#id='data1']/following-sibling::tr

How to get between two br tags in xpath?

I have a table with td like this
<td>
<span> Washington US <br>98101 Times Square</span>
</td>
I can get all the elements in the page, but I need to get those two values separately. If that isn't possible I would like to somehow get 98101 Times Square
I have tried doing something like string(//tr[3]//td[2])/ but all I get is the two text joined together.
You can select the text child nodes in the span element with span/text() so assuming your posted path selects the td containing the span you want //tr[3]//td[2]/span/text().
Here is a sample:
$html = <<<EOD
<html>
<body>
<table>
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3,1</td>
<td>
<span> Washington US <br>98101 Times Square</span>
</td>
</tr>
</body>
</html>
EOD;
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$textNodes = $xpath->query('//tr[3]//td[2]/span/text()');
foreach ($textNodes as $text) {
echo $text->textContent . "\n";
}
Outputs
Washington US
98101 Times Square
Try
td/span/node()[1]
and
td/span/node()[3]
Or
td/span/text()[1]
td/span/text()[2]

PDF Layout with MigraDoc

I am trying to achieve following matrix kind of layout:
TABLE1,1 TABLE1,2
CHART2,1 TABLE2,2
TABLE3 --> occupies whole row
CHART4 --> ocupies whole row
CHART5,1 CHART5,2
................. List goes on...
These components may span over multiple pages. What is the best way to have them side by side and still be able to view them in MigraDoc.
CHART5,1 could be a combination of 4 charts in one cell.
In HTML view I can use following analogy:
<TABLE>
<TR>
<TD>TABLE1,1</TD> <TD>TABLE1,2 </TD>
</TR>
<TR>
<TD>CHART2,1</TD> <TD>TABLE2,2 </TD>
</TR>
<TR>
<TD>TABLE3</TD colspan =2>
</TR>
<TR>
<TD>CHART4</TD colspan =2>
</TR>
<TR>
<TD>CHART5,1</TD> <TD>CHART5,2 </TD>
</TR>
</TABLE>
The MigraDoc equivalent for colspan=2 is MergeRight=1. This is a property of the Cell class.

Scraping page with correct xpath using Mechanize and nokogiri

I am trying to access data contained in a table that is itself contained in a table with class ='L1'.
So basically my html structure is like this:
<table class="L1">
<table>
<tr></tr>
<tr>
<td></td>
<td>data</td>
</tr>
<tr>
<td></td>
<td>data</td>
</tr>
...ect...ect
</table>
</table>
I need to catch the data contained in a all <a> </a> that are in the second contained in <tr> </tr> but only starting with the second <tr> of the table.
So far I came up with that:
html_body = Nokogiri::HTML(body)
links = html_body.css('.L1').xpath("//table/tbody/tr/td[2]/a[1]")
But seems to me that this doesn't express the fact that I want to start only after the second <tr> (second <tr> included?
What would be the right code to do this ?
You can use position() to select the later elements that you want.
html_body = Nokogiri::HTML(body)
links = html_body.css('.L1').xpath("//table/tbody/tr[position()>1]/td[2]/a[1]")
As the comments on that SO answer say, remember XPath counts from 1, so >1 skips the first tr.

Soritng Ascending and descending order after clicking on table header?

I am using Selenium WebDriver with Java.
I have a table where I have to click on header of each column and want to validate whether sorting functionality is working correctly or not. I need to check for both Asc and Desc order.
So I have to take the count of the table first and the get the header text so that I click on each col 1-1 and then need to validate the sorting.
How can achieve my expected result. Below is the HTML source code:
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_gvClinicalTrait" style="border-color:appworkspace;border-collapse:collapse;">
<tr class="gridTitleBar">
<th scope="col">Date Collected</th><th scope="col">Clinical Trait Data</th><th scope="col">Source</th><th scope="col">Value</th>
</tr><tr class="gridBody" align="left">
<td>11/6/2008</td><td style="width:200px;">A1C</td><td style="width:200px;">d</td><td>6.00</td>
</tr><tr class="gridBody" align="left">
<td>9/17/2008</td><td style="width:200px;">BP</td><td style="width:200px;">e)</td><td>104/54</td>
</tr><tr class="gridBody" align="left">
<td>7/12/2008</td><td style="width:200px;">BP</td><td style="width:200px;">g</td><td>124/56</td>
</tr><tr class="gridBody" align="left">
<td>6/21/2008</td><td style="width:200px;">BP</td><td style="width:200px;">t</td><td>110/72</td>
</tr><tr class="gridBody" align="left">
<td>6/14/2008</td><td style="width:200px;">BP</td><td style="width:200px;">n</td><td>120/70</td>
</tr>
</table>
</div>
How can achieve my expected result. Below is the HTML source code:
you need to have list or array with expected result.
You need to create loop like this for every test:
List<string> expectedValues = new List<string>("12","13","14");
int expectedNumber = expectedValues.Count;
int tdNumber = 2;
for (int i=1; i< expectedNumber; i++)
{
string result = driver.Find(by.xpath("//div[#id ='ctl00_ContentPlaceHolder1_gvClinicalTrait']/tr["+ i +"]/td["+tdNumber+"]")).Text;
if (result != expectedValues[i])
Assert.Fail("Wrong value!")
}
I didnt try this code, but even if its wrong it's will be pretty close to code that you need.

Resources