How to add rows in the middle of a table using jQuery?

I have a table in which there are customer names, as well as the products that they purchased with their prices. Thus, for each client there are several records. This table is simple in table 3 columns: name, product and price.

What I want to do:

Put all the records belonging to one customer together (I did this), and immediately after these lines add one new additional line, which will simply show the total value of all products purchased by each customer. This row will contain an empty cell in the name and column of the product. And it will have a final value in the price column.

EDIT

This is a simple simple table without any class or identifiers. grouped by customer table generated by php. So I have a table like this

<table> <tr> <td> name1 </td> <td> product1 </td> <td> 100 </td> </tr> <tr> <td> name1 </td> <td> product2 </td> <td> 200 </td> </tr> <tr> <td> name2 </td> <td> product3 </td> <td> 50 </td> </tr> <tr> <td> name2 </td> <td> product1 </td> <td> 100 </td> </tr> </table> 

And I want to convert this to:

 <table> <tr> <td> name1 </td> <td> product1 </td> <td> 100 </td> </tr> <tr> <td> name1 </td> <td> product2 </td> <td> 200 </td> </tr> <!-- New row --> <tr> <td> </td> <td> </td> <td> 300 </td> </tr> <tr> <td> name2 </td> <td> product3 </td> <td> 50 </td> </tr> <tr> <td> name2 </td> <td> product1 </td> <td> 100 </td> </tr> <!-- New row --> <tr> <td> </td> <td> </td> <td> 150 </td> </tr> </table> 
+7
jquery html-table row
source share
2 answers

Using jQuery, you can select all the rows in which the username is contained in one of its descendant elements. I assume the usernames are unique.

 $("tr:contains('" + username + "')") 

Now, get the last

 $("tr:contains('" + username + "')").last() 

And use the after function to insert a new line.

+8
source share
 var prize = 1 * 1; var newrow = $('<tr><td></td><td></td><td>' + prize + '</td></tr>'); $('#rowid').after(newrow); 

or

 newrow.insertAfter('#rowid'); 

where #rowid is the identifier from the line you want to add to the new line.

+15
source share

All Articles