user4493177
user4493177

Reputation: 790

Add custom variable to jquery datatables and read in search filter

I am using the jQuery DataTables plugin. I am trying to assign a parameter to my table, and use that parameter later in a custom search. I however cant find out how to store and get that parameter, and in my custom search i dont know how to get the table object. Here is my code to give a more clear idea of what i am trying to do:

jQuery.fn.dataTable.ext.search.push(
    function( settings, data, dataIndex ) {
        var myParam = ....;
        console.log('i said: '+ myParam)
       return true;
    }
);

var table = jQuery('#myTable').DataTable();
table.myParam = 'hello';
table.draw();

Upvotes: 1

Views: 1522

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58860

  • Use global variable:

    var myParam = '';
    
    jQuery.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {   
           console.log('i said: '+ myParam)
           return true;
        }
    );
    
    var table = jQuery('#myTable').DataTable();
    myParam = 'hello';
    table.draw();
    
  • Use data- property:

    jQuery.fn.dataTable.ext.search.push(
        function( settings, data, dataIndex ) {
           var table = new jQuery.fn.dataTable.Api( settings ); 
           var myParam = jQuery(table.table().node()).data('myParam');
    
           console.log('i said: '+ myParam)
           return true;
        }
    );
    
    var table = jQuery('#myTable').DataTable();
    jQuery(table.table().node()).data('myParam', 'hello');
    table.draw();
    

Upvotes: 4

Related Questions