开发者

Identifying all rows within a table <tbody> element using jQuery

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&g开发者_JAVA百科t;
      <td>Urgent Data</td>
      <td>Urgent Data</td>
    </tr>
 </tbody>

jQuery code:

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


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

var trs = $("#tableid").find("tbody>tr");


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.


This is probably the cleanest

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


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.


Try:

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

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜