17

Seems like it should be easy but...

Does anyone know how to return the current rows from a filtered dataTable? The oTable.fnGetNodes() method returns all rows, where I just want the filtered (visible, but including paginated) ones

// filter on division
var oTable = $('#summary-table').dataTable();
oTable.fnFilter(division_text, 2, true);

// Get the nodes from the table  
var nNodes = oTable.fnGetNodes(); // <-- still retrieves original list of rows

I checked: Retrieving visible data from Datatables but not much help there.

9 Answers 9

20

As of Datatables 1.10, there is a built-in way to get the filtered or unfiltered rows after a search.

var table = $('#example').DataTable();
table.rows( {search:'applied'} ).nodes();
table.rows( {search:'removed'} ).nodes();

There are other options for getting only the current page or all pages as well as the order. More details here: http://datatables.net/reference/type/selector-modifier

Sign up to request clarification or add additional context in comments.

Comments

16

The easiest way to do this is actually built right in to the DataTables API:

_('tr', {"filter": "applied"})

Used in a Function:

function get_filtered_datatable() {
    var filteredrows = $("#mydatatable").dataTable()._('tr', {"filter": "applied"});

    for ( var i = 0; i < filteredrows.length; i++ ) {
        debug.console(filteredrows[i]);
    };
}

1 Comment

FYI - this seems to not work with the new API e.g. $("#mydatatable").DataTable()._('tr',...); gives TypeError: $(...).DataTable(...)._ is not a function
3

If you're trying to get the actual tr DOM elements instead of the data, the solution is similar to the underscore solutions provided above, but you use the $ method instead.

function getFilteredDatatable() {
    return $("table.dataTable").dataTable().$('tr', { "filter": "applied" });
}

More information is available on the API documentation page. http://datatables.net/api

2 Comments

How do I get the DOM element like the fnGetNodes() while applying filter using $('tr', { "filter": "applied" })?
The object returned from .$() is a jQuery object. You can you the .get() method to turn it into an array, which is what is returned from fnGetNodes(). link
2

Better late than never but I was struggling with this myself. Here's what I came up with

$.fn.dataTableExt.oApi.fnGetVisibleData = function(){
    displayed = [];
    currentlyDisplayed = this.fnSettings().aiDisplay; //gets displayed rows by their int identifier
    for (index in currentlyDisplayed){
        displayed.push( this.fnGetData( currentlyDisplayed[index] ));
    }
    return displayed;

}

Comments

1

Figured out the answer, if anyone ever needs this:

First, using this datatables extension to get all the hidden rows:

$.fn.dataTableExt.oApi.fnGetHiddenTrNodes = function (oSettings, arg1, arg2) {

/* Note the use of a DataTables 'private' function thought the 'oApi' object */

var anNodes = this.oApi._fnGetTrNodes(oSettings);
var anDisplay = $('tbody tr', oSettings.nTable);

/* Remove nodes which are being displayed */
for (var i = 0; i < anDisplay.length; i++) {
    var iIndex = jQuery.inArray(anDisplay[i], anNodes);

    if (iIndex != -1) {
        anNodes.splice(iIndex, 1);
    }
}

/* Fire back the array to the caller */
return anNodes;
}

Then filter out the hidden nodes to get only visible nodes:

 var rows = oTable.fnGetNodes(); // get all nodes            
 var rows_hidden = oTable.fnGetHiddenTrNodes(); // get all hidden nodes

 var result = [], found;

 // remove hidden nodes from all nodes
 for (var i = 0; i < rows.length; i++) {
  found = false;
    for (var j = 0; j < rows_hidden.length; j++) {
      if (rows[i] == rows_hidden[j]) {
        found = true;
          break;
                }
            }
            if (!found) {
                result.push(rows[i]); 
            }
    }

3 Comments

-1 This shows only visible rows. Paginated rows are not included.
Way too much code. DataTables actually has this built in to their API by using the underscore function.
See _('tr', {"filter": "applied"}) below.
1

Thanks AlecBoutin, that's the easiest way.
I am trying to search into multiple tables arranged in tabs, and I want to display the table where a result is found. Quite easy with your solution

    // make the global search input search into all tables (thanks to a class selector)
    $('#oversearch').on( 'keyup', function () {
        $('.table').DataTable().search( this.value ).draw();

        var row = $('.table').DataTable().$('tr', { "filter": "applied" });
        console.log(row.parents("div")[1]);
    });

you can then navigate into whatever parent you need with the parents() jquery. (here I'm choosing the 2nd div parent encountered)

Comments

0

You can get the visible rows list changing the fnGetHiddenTrNodes function as follows.

 $.fn.dataTableExt.oApi.fnGetVisibleTrNodes = function (oSettings, arg1, arg2) {

            /* Note the use of a DataTables 'private' function thought the 'oApi' object */

            var anNodes = this.oApi._fnGetTrNodes(oSettings);
            var anDisplay = $('tbody tr', oSettings.nTable);
            var visibleNodes = [];

            for (var i = 0; i < anDisplay.length; i++) {
                var iIndex = jQuery.inArray(anDisplay[i], anNodes);

                if (iIndex != -1) {
                    visibleNodes.push(anDisplay[i]);
                }
            }

            /* Fire back the array to the caller */
            return visibleNodes;
        }

Comments

0

Having the table setup this worked for me having a checkbox with the id for each row and another check box that will select all rows either when a filtered has been applied(just filtered rows) or not (all rows)

$(document).ready(function() {
    var myDataTableHandle = $('#example').dataTable({"bPaginate": false});

   $('#selectAllCheckBox').click(function() {
       if($(this).is(':checked')){ 
          var filteredRows  =   myDataTableHandle._('tr', {"filter":"applied"});
          alert( filteredRows.length +' nodes were returned' );     
          $(myDataTableHandle.fnGetNodes()).find($('input[name=idCheckBox]')).each(function () {
              $(this).prop('checked', true);
           });
        else{ 
          $('input[name=idCheckBox]:checked').prop('checked', false);
         } 
      });
});

Comments

0

I used to Datatable in version 1.10.x

var table = $('#example').DataTable();
var filtered_data = table.rows( {search:'applied'} ).data();
var removed_data = table.rows( {search:'removed'} ).data();

Check example Codepen

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.