2

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

1
  • I hope its .dataTable not .DataTable Commented Apr 23, 2015 at 9:21

1 Answer 1

2

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

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

1 Comment

I build the table from sources JS data. Here you have hardcoded

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.