8

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

17

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

var trs = $("#tableid").find("tbody>tr");
2
  • If we have applied pagination,then how do I get other page data besides of current page.
    – Hasi
    Apr 6, 2016 at 10:14
  • I was able to find a solution to get all records in paginated pages.
    – Hasi
    Apr 7, 2016 at 4:14
12

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.

4

This is probably the cleanest

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

2

Try:

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

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

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();

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.