HOw to get number of rows

HOw to get number of rows

findpritzfindpritz Posts: 12Questions: 0Answers: 0
edited July 2010 in General
Hello all,

I wanted to know how do I go about getting the number of rows in my datatable.
If I use


var rowCount = document.getElementById('#table-id').getElementsByTagName('tr');


then it does not take into considering the rows on the next pages.

Is there a function that I use to get the # of total rows across all pages?

Thanks,
Pritish

Replies

  • allanallan Posts: 63,412Questions: 1Answers: 10,454 Site admin
    oTable.fnSettings().fnRecordsTotal() should do it.
    oTable.fnSettings().fnRecordsDisplay() will give you the number after filtering has occurred.

    Regards,
    Allan
  • findpritzfindpritz Posts: 12Questions: 0Answers: 0
    Thanks for you reply Allan.
    But I'm currently using the following, and it seems to work for me.

    oTable.fnGetData().length;

    Another thing, I wanted to know was if I wanted to pass over some of the table data that was not included in the DOM(in my case, some data from page # 2 of the datatable) in an AJAX call, how would I do the same ?
  • allanallan Posts: 63,412Questions: 1Answers: 10,454 Site admin
    oTable.fnGetData().length will do the job nicely :-)

    You can use fnGetNodes() to get all TR elements which will allow you to parse them and modify them as needed. This plug-in might be of use as well: http://datatables.net/plug-ins/api#fnGetHiddenNodes

    Allan
  • findpritzfindpritz Posts: 12Questions: 0Answers: 0
    Hi Allan,

    Thanks for your prompt reply.

    So, let me frame my question correctly again.

    This is one of the columns in the HTML table that I'm using datatables plug-in for



    ...







    ...





    And, currently, I'm using the following code to get the value of the hidden field for each row that has the checkbox checked.


    var protCaseInstArray = new Array();

    $("#mydivsid").find("input[type='checkbox' name='caseConsent']:checked ~ input[name='protCaseInst']").each(function() {
    protCaseInstArray.push($(this).val());
    });


    It works perfectly fine for rows selected on the first page of my datatable, but does not work if I select rows from the 2nd page onwards.

    What would the changes I need to make?

    Thanks,
    Pritish Devurkar.
  • allanallan Posts: 63,412Questions: 1Answers: 10,454 Site admin
    Try:

    $(oTable.fnGetNodes()).find("input[type='checkbox' name='caseConsent']:checked ...

    Allan
  • findpritzfindpritz Posts: 12Questions: 0Answers: 0
    Thanks you for replying so quickly!

    That works perfectly...and thank you for building such a great plug-in.
  • pedrolopespedrolopes Posts: 5Questions: 0Answers: 0
    Hi Allan!

    I was looking to update a section in my page with the number of rows in the DataTable. However, the table can be reloaded several times (using fnReloadAjax(url)), which is resulting in an incorrect length value...

    I'm setting up the new row value after the reload function and I tried with oTable.fnSettings().fnRecordsTotal(), oTable.fnGetData().length (these return 0) and with oTable.fnSettings()._fnRecordsTotal (this always returns the size of the initial table).

    Any ideas?

    Thanks!
  • allanallan Posts: 63,412Questions: 1Answers: 10,454 Site admin
    fnReloadAjax provides a callback function option. You can pass in a function and have it executed when the new data from the server has been retrieved. From there your counter can be updated accurately.

    Allan
  • pedrolopespedrolopes Posts: 5Questions: 0Answers: 0
    Hi Allan,

    I was looking at the documentation... I'm using something like:

    oTable.fnReloadAjax({},'../browse/${actionBean.hgnc}/enabled', function(){
    $('#size').html(oTable.fnSettings().fnRecordsTotal());
    }, null);

    However this does not reload the data or corrects the value in the #size element... If I just call fnReloadAjax with the data source, the data is loaded but I cannot update the row total...

    Any ideas?

    Thanks in advance!
  • pedrolopespedrolopes Posts: 5Questions: 0Answers: 0
    Ah, I got the problem!

    I was using a streamlined version of the fnAjaxReload plugin which did not include any support for callback operations... I'm doing the update directly in the plugin and it works...

    Problem solved!
  • bdillbdill Posts: 1Questions: 0Answers: 0
    Is there a way to get fnGetNodes() in the current sort order?

    I can loop the visible rows using jQuery when I click on an click in a cell.
    [code]
    function foo(ctrl) {
    // (td) (tr) (tbody)
    $(ctrl).parent().parent().parent().find('tr').each( function(index) {
    // get ID of each row
    $(this).attr('id');
    }
    }
    [/code]
    This works great except that it only iterates through visible rows (current pagination page)

    I can use oTable.fnGetNodes() to get all the rows, but it is the original sort order, and doesn't take into account the possible change in sorting.

    Is there a way to iterate through ALL rows, but take into account the current sorting?
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    you'd probably have to write your own routine. look into the aiDisplay or aiDisplayMaster arrays in the oSettings object to get the current sorting of data.
  • allanallan Posts: 63,412Questions: 1Answers: 10,454 Site admin

    Update for anyone reading this - in 1.10 use page.info() or data() (i.e. table.data().length() to get information about the number of rows in the table.

This discussion has been closed.