Equivalent of fnGetData() for filtered/searched rows

Equivalent of fnGetData() for filtered/searched rows

DaevinDaevin Posts: 5Questions: 3Answers: 0
edited May 2016 in Free community support

Before you write this off as a duplicate of many other questions (including: https://datatables.net/forums/discussion/10321/fngetdata-of-visible-rows-only), I need to point out that:

$(...).dataTable().fnGetData();

and

    $(...).dataTable()._filter('tr', { "filter":"applied" });

are NOT equivalent (beyond just showing un-/filtered data).

Using the Chrome console window to display results, the fn method returns an object with only the data Objects, length, and proto 'fields' whereas the _ method returns the data Objects, as well as $, ajax, cell, [...] definitions.

I'm trying to post results to an API, and using JSON.stringify(results) includes all of the unwanted properties of the _ method results :(. Am I stuck doing what wazza did in his ad-hock solution before Allan showed the _ method?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,914Questions: 1Answers: 10,149 Site admin
    Answer ✓

    Use rows().data() for this - its far more flexible than the legacy fnGetData or _ methods.

    What you would use is:

    var table = $(...).DataTable(); // note the capital `D`
    
    table.rows( { search: 'applied' } ).data().toArray();
    

    Note also the use of toArray() to convert the DataTables API instance to an array. That way JSON.stingify() or whatever serialisation you use will note pick up all the DataTables API methods etc.

    Allan

  • DaevinDaevin Posts: 5Questions: 3Answers: 0

    I was unaware that those fn and _ methods methods were legacy, that's how I was basing my searches :(.

    Work perfectly, thanks!

This discussion has been closed.