maaz
maaz

Reputation: 3664

Datatable : Adding callback function on filter and get filtered data array

I'm using Datatable 1.10.4.

I'm sending the data array to the table to populate the table, Initialization is as follows:

table = $('#dashboard-user-list-table').dataTable({
    "data":window.MyApp.Model.userModel.getUsers(), //sourced JS Array 
    "iDisplayLength": 4,
    ---
    --- 
});

I want to add a onfilter callback function and get the filtered data array and do some stuff.

Even without callback function , is there any way to get a Filtered data array?. (Basically i need to get the array which i passed as a sourced data which is visible on page)

Does the Datatables plugin allow me to do this? If so, I haven't found anything in the documentation that is intuitive.

Can you please suggest me how to do it?

refer JSFIDDLE

Upvotes: 2

Views: 2953

Answers (1)

codeandcloud
codeandcloud

Reputation: 55333

I understand that you want the filtered data as an array when you perform the search.
If so, try this.

var table = $('#dashboard-user-list-table').dataTable({
    ---
    --- 
});
$('##dashboard-user-list-table').on('search.dt', function () {
    var api = table.api();
    //uppercase used for case insensitive search
    var searchTerm = api.search().toUpperCase(); 
    var filteredData = api.data()
        .filter(function (value, index) {
            return value.toString().toUpperCase().indexOf(searchTerm) !== -1;
        }).toArray();
    console.log(filteredData);
});

Working fiddle: http://jsfiddle.net/codeandcloud/a8b3ttf7/

Disclaimer: There might be a simpler way. I am not that much of a datatables.net expert

Upvotes: 2

Related Questions