15

I am using a jQuery dataTable and when the user selects a drop down it searches the data table and filters it and redraws the contents based upon the searched data :

mtTable.columns().each(function() {
    mtTable.column(22).search(searchVal, true, true).draw();
});

Now I am trying to get all of the column values after a search is done, however I cannot find a function to do this. Currently I am using from the api

var myTable = $("#tblResults").DataTable();
var resultsArray = myTable.columns(colIndex).data();

According to the documentation this will return all of the data from within the column unfiltered. I cannot find a function to give me an array of the column values for the filtered data only.

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
foop
  • 513
  • 1
  • 7
  • 16

3 Answers3

42

You can read all about dataTables advanced selector-modifiers here -> http://datatables.net/reference/type/selector-modifier

If you want to get filtered rows only :

table.rows( { search:'applied' } ).data().each(function(value, index) {
    console.log(value, index);
});

To target a specific column, and get filtered values only (your specific request) - here all filtered values from column #2 :

table.column(2, { search:'applied' } ).data().each(function(value, index) {
    console.log(value, index);
});

See demo with both -> http://jsfiddle.net/q0e1bdcz/

To create an array over filtered values for a specific column :

var array = [];
table.column(2,  { search:'applied' } ).data().each(function(value, index) {
    array.push(value);
});
console.log(array);

See demo -> http://jsfiddle.net/q0e1bdcz/1/

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
4

You can also get unique and sorted data if you have more entries:

// Datatable object
var table = $('#example').DataTable();

// Get Unique and Sorted values.  
table.column(3, { search:'applied' } ).data().unique().sort().each(function(value, index) {
    console.log(value, index);
});

Ref: http://www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/examples/api/multi_filter_select.html

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Raja
  • 3,477
  • 12
  • 47
  • 89
0

Below is the solution which I used to get all values of a particular column:

oTable = $('#myDatatable').DataTable({
 //your code for datatable
 })

oTable.on('search.dt', function () {
   oTable.column(10).nodes().each(function (cell) {
        var val = cell.innerHTML;
                console.log(val);
                                });
                            })
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61