prog1011
prog1011

Reputation: 3495

How to get <tr> count in HTML table - using Jquery?

I am using Jquery Datatables to achieve search and sort functionality on HTML table <table>.

I have more then 100 rows in <table> and I used iDisplayLength=6 attribute to display on 6 records at the same time and enabled paging functionality for more records.

The problem is: I want to count that how many <tr> in <table> using jquery.

I used follwing code for that but it gives count 6 <tr> always because I used DisplayLength=6. But I want actual count of <tr>

JavaScript

$(document).ready(function () {
  $("#ContentPlaceHolder1_grdRX").dataTable({
         "iDisplayLength": 6,
         "bLengthChange": false,
         "bFilter": true,
         "bInfo": false,
         "bAutoWidth": false 
     });
 });

function getCount() {
    alert($('#ContentPlaceHolder1_grdRX tr').length);
}

How can I get a count of all the rows?

Upvotes: 0

Views: 227

Answers (1)

Vicxx
Vicxx

Reputation: 474

It looks like DataTables is removing the rows that aren't on the current page from the DOM, so you aren't going to be able to count them with a jQuery selector. You'll have to use the DataTables API, specifically the fnGetHiddenNodes function:

var table = $('##ContentPlaceHolder1_grdRX').dataTable();
             $('#button').click( function () {
             var hidden = table.fnGetHiddenNodes();
             alert( hidden.length +' nodes were returned' ); } );

Upvotes: 2

Related Questions