9

I am trying to retrieve all rows from within a <tbody> section of a table, but am unsure of the syntax for doing so. I've included a dummy table extract below and my latest attempt for achieving the task with jQuery!

Table extract:

<tbody> <tr> <th id="emergency" colspan="2">Emergency</th> </tr> <tr> <td>Emergency data</td> <td>Emergency data</td> </tr> <tr> <td>Emergency data</td> <td>Emergency data</td> </tr> </tbody> <tbody> <tr> <th id="urgent" colspan="2">Urgent</th> </tr> <tr> <td>Urgent Data</td> <td>Urgent Data</td> </tr> <tr> <td>Urgent Data</td> <td>Urgent Data</td> </tr> </tbody> 

jQuery code:

var emergencyRows = $table.find('#emergency').children().get(); 

6 Answers 6

18

You can use the below if you know the table id.

var trs = $("#tableid").find("tbody>tr"); 
Sign up to request clarification or add additional context in comments.

2 Comments

If we have applied pagination,then how do I get other page data besides of current page.
I was able to find a solution to get all records in paginated pages.
13

My suggestions is to place the ID attributes on the tbody rather than the first row of each one.

HTML

<table> <tbody id="emergency"> <tr> <th colspan="2">Emergency</th> </tr> <tr> <td>Emergency data</td> <td>Emergency data</td> </tr> <tr> <td>Emergency data</td> <td>Emergency data</td> </tr> </tbody> <tbody id="urgent"> <tr> <th colspan="2">Urgent</th> </tr> <tr> <td>Urgent Data</td> <td>Urgent Data</td> </tr> <tr> <td>Urgent Data</td> <td>Urgent Data</td> </tr> </tbody> </table> 

jQuery

var emergencyRows = $("tbody#emergency").find("tr:gt(0)"); var urgentRows = $("tbody#urgent").find("tr:gt(0)"); 

The jQuery snippet will get all the respective rows with the exception of the first rows.

Comments

4

This is probably the cleanest

$('#emergency').closest('tbody').children(); //only returns first child element, so the <tr>'s 

Comments

3

From your example, it seems like you might want "all rows except for the one containing #emergency". If that's the case, you can use the following:

$('#emergency').closest('tr').siblings();

Note that #emergency need not be a <tr /> or <th /> or anything in particular. It could be any element within a table cell.

Comments

2

Try:

var emergencyRows = $("th#emergency").parent().parent().find("tr:gt(0)"); 

which should get you all rows that aren't the header row.

Comments

1

I was able to find a solution to get all records in paginated pages.You also can try out this.

var userList = $("#user-grid").dataTable().fnGetNodes();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.