How store all data in javascript array after column search?

How store all data in javascript array after column search?

gameon67gameon67 Posts: 2Questions: 1Answers: 0

I have tables exactly as described in https://datatables.net/examples/api/multi_filter.html. My code structure is exactly the same (please see the full code and demo using the link).

$(document).ready(function() {
    // Setup - add a text input to each footer cell
    $('#example tfoot th').each( function () {
        var title = $(this).text();
        $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
    } );

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

    // Apply the search
    table.columns().every( function () {
        var that = this;

        $( 'input', this.footer() ).on( 'keyup change clear', function () {
            if ( that.search() !== this.value ) {
                that
                    .search( this.value )
                    .draw();
            }
        } );
    } );
} );

Now I want to return all records (rows) in the table after performing the search operation, either through console.log() or save the data in javascript array.

For example after I search Accountant in Position column, I want to get the whole data stored in javascript array

enter image description here

[[Airi Satou,Accountant,Tokyo,33,2008/11/28,$162,700],
[Garrett Winters,Accountant,Tokyo,63,2011/07/25,$170,750]]

I have tried something like console.log( table.row( this ).data() ); but didn't work. How to achieve this?

Answers

  • colincolin Posts: 8,150Questions: 0Answers: 1,364

    You need to use selector-modifier with rows(), so something like table.rows({search: 'applied'}).data().toArray()

    Colin

  • gameon67gameon67 Posts: 2Questions: 1Answers: 0

    Where should I put it? after .draw() ? I'm new in datatable, it will take me sometime to fully understand the architecture

  • colincolin Posts: 8,150Questions: 0Answers: 1,364

    It doesn't matter, the search will be applied before the draw() - so just place it wherever you want to get the filtered data. See example here.

    Colin

Sign In or Register to comment.