views:

53

answers:

2

Hello all,

I'm using the jQuery datatables plug-in for my HTML table.

Is there a way to get the row count of the # of rows in my table across pages...
For example, if I have 70 rows in my table, and lets say 50 of them get displayed on the 1st page, and 20 on the 2nd page..is there a way to get the count of 70?

I've tried all the suggestions included on this link

http://stackoverflow.com/questions/1149958/jquery-count-number-of-rows-in-a-table

This includes

var rowCount = $('#myTable tr').length;
var rowCount = $('#myTable tr').size();
var rowCount = $('#myTable >tbody >tr').length;
var rowCount = $("#myTable").attr('rows').length;

But all the above suggestions seem to return the # of rows on the existing page(in this case, 50 and not 70).

Thanks,
Pritish.

A: 

To get information between page loads with javascript alone you are going to need to use either cookies or URL parameters of some kind. The following is a very easy to use cookie plugin in for jQuery and is probably your easiest solution.

http://plugins.jquery.com/project/cookie

HurnsMobile
+1  A: 

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 fnGetData function:

$(document).ready(function() {

    // Initialize your table
    var oTable = $('#myTable').dataTable();

    // Get the length
    alert(oTable.fnGetData().length);
} );
Matt Peterson
Thanks, this seems to work.Sorry, but wanted to know something else about datatables too.I'm extracting some of the data from the datatables, and passing them in an AJAX call.How do I make sure that I pass data from all rows and not only those on the first page ?Thanks,Pritish.
Pritish
@Pritish Check out the docs (http://www.datatables.net/api) for the fnGetData function. With no arguments (as in my example), it is actually returning a 2D array with all of the data from the table in it.
Matt Peterson
@Matt - Yeah, took a look at it.alert(oTable.fnGetData());prints out the HTML entire table data.How would I access individual HTML elements(specially, by name) from it though?
Pritish