Get ALL filtered rows across ALL pages

Get ALL filtered rows across ALL pages

SmallzooSmallzoo Posts: 7Questions: 0Answers: 0
edited November 2013 in DataTables 1.9
I cant seem to solve this simple problem. I know I can use the following to loop through all the rows :-

[code]
var rows = $("#myTable").dataTable().fnGetNodes();
for (var i = 0; i < rows.length; i++) {
var v1 = $(rows[i]).find("td:eq(1)").text();
}
[/code]

I also know you can get the filtered rows by :-
[code]
var rows = $("#myTable").dataTable()._('tr', {"filter":"applied"});
[/code]

BUT how do I combine BOTH. ie lopp thorugh all the rows in a paginated tabled but ONLY the filtered ones

Thanks

Replies

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Use the `$` API method rather than `_` :-). $ for nodes (more specifically jQuery) and _ for data.

    Allan
  • SmallzooSmallzoo Posts: 7Questions: 0Answers: 0
    Sorry I dont know what you mean.. can you give me a simple example as per my code ?
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    [code]
    var rows = $("#myTable").dataTable().$('tr', {"filter":"applied"});
    [/code]
  • SmallzooSmallzoo Posts: 7Questions: 0Answers: 0
    The above works but only for the first page..as per my first email I want to loop through ALL filtered rows ..
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    That should be doing so. Are you using server-side processing or deferred rendering? Please link to a test case showing the issue, so we have all of the information needed to be able to help assist in resolving the issue.

    Allan
  • SmallzooSmallzoo Posts: 7Questions: 0Answers: 0
    I'll try and give more info

    First I create a simple table
    [code]
    $('#cfulltable').dataTable({
    "aoColumnDefs": [
    { "asSorting": [ "desc", "asc", "asc" ], "aTargets": [ 1,2,3,4,5,6, ] }
    ],
    "bProcessing": true,
    "sPaginate": true,
    "iDisplayLength": 20

    });
    [/code]

    on data ( simplified of course...)

    [code]



    ID
    Name




    5684
    NAME



    [/code]


    I then filter with a textbox for name which works well
    ( textbox is say called "fname" and you click a button to filter which runs the following script..)



    [code]
    function filter(){
    var oTable = $('#cfulltable').dataTable();
    oTable.fnDraw();
    }
    [/code]



    This then runs the filtering routine automatically. I then check to see if the name matches. I'm sure the way I am matching the name is too convoluted but it works..



    [code]
    $.fn.dataTableExt.afnFiltering.push(

    function (oSettings, aData, iDataIndex) {
    var rt1 = false;
    var isName = document.getElementById('fname').value;
    var iaName = aData[1];
    if (isName == '') { rt1 = true; }
    else {
    var inamelen = iaName.length;
    var inamesub = iaName.substring(iaName.indexOf('') + 1;
    var nme = inamesub.indexOf('');
    var iname = inamesub.substring(nms, nme);
    if (iname.substring(0, inamelen).toLowerCase().indexOf(isName) >= 0)
    { rt1 = true; }
    else
    { rt1 = false; }
    }
    );
    [/code]


    This filtering works over all pages great...but as I said when I then try and read the records I can only seem to either loop through the whole table

    [code]
    var rows = $("#cfulltable").dataTable().fnGetNodes();
    for (var i = 0; i < rows.length; i++) {
    var v1 = $(rows[i]).find("td:eq(1)").text();
    }
    [/code]

    or the filtered ones

    [code]
    var rows = $("#cfulltable").dataTable()._('tr', {"filter":"applied"});
    for (var i = 0; i < rows.length; i++) {
    var v1 = $(rows[i]).find("td:eq(1)").text();
    }
    [/code]



    BUT NOT BOTH ?


    Does this make sense ?

    Thanks
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    edited November 2013
    The `_` method isn't going to work there, as I mentioned originally. It is for raw data, not nodes. So the jQuery find isn't going to find very much on line 3 there.

    Try:

    [code]
    $("#cfulltable").dataTable().$('tr', {"filter":"applied"}).each( function () {
    var v1 = $(this).find("td:eq(1)").text();
    ...
    } );
    [/code]

    I presume there is meant to be more code after the find row.

    Allan
This discussion has been closed.