Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving row data after filtering JQuery Datatables

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.

like image 766
proggrock Avatar asked Oct 16 '12 13:10

proggrock


4 Answers

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

like image 169
anagnostatos Avatar answered Nov 12 '22 11:11

anagnostatos


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]);
    };
}
like image 32
Joe Bergevin Avatar answered Nov 12 '22 09:11

Joe Bergevin


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

like image 23
AlecBoutin Avatar answered Nov 12 '22 10:11

AlecBoutin


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;

}
like image 2
nsheaps Avatar answered Nov 12 '22 09:11

nsheaps