Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the setting of datatable after it initialized

I have a datatable with some settings,

 $(tblCategory).dataTable({
            "bPaging": true,
            "sPaginationType": "full_numbers", //
            "bPaginate": false, // shows both next prvious and show 10,20,30 entries
            "sdom": 't',
            "bprocessing": false, //
            "sAjaxSource": "PM_MasterHelper.ashx?m=pmcfl&UserCode="+UserCode,
            "bFilter": false, // search btn avail
                "bDestroy": true
}

Now i would like to alter or add the settings like "bInfo":true, "bPaginate":true, when i click html input button.

Please help me to achieve this.

Try 1: I tried the following way but it doesn't affect the datatable.

 oTable = $("tblCategory").dataTable();
 var oSettings = oTable.fnSettings();
 oSettings.bInfo= true;
oSettings.bPaginate = true;
like image 955
Gomathipriya Avatar asked Feb 12 '16 11:02

Gomathipriya


3 Answers

You need to do a DataTable().destroy() before apply the new options, example:

oTable = $("tblCategory").DataTable();
var oSettings = oTable.fnSettings();
oSettings.bInfo= true;
oSettings.bPaginate = true;
oTable.destroy();
$("tblCategory").DataTable(oSettings);

Also u need to use DataTable() with capital D.

like image 114
Bruno Bollati Avatar answered Oct 11 '22 16:10

Bruno Bollati


You Should reinitialize the datatable:

//Default Options Object
var tableOptions = {
        'bPaginate': true,
        'searching' : true
    };
//The table has already been initialized
var table = $("#ExampleTable");
  tableOptions.bPaginate = true;
  table.DataTable().destroy()
  table.DataTable(tableOptions);
like image 8
Rodrigo Esparza Salazar Avatar answered Oct 16 '22 03:10

Rodrigo Esparza Salazar


According to the documentation:

Simply put, DataTables does not allow initialisation options to be altered at any time other than at initialisation time. Any manipulation of the table after initialisation must be done through the API

The solution is to destroy the table:

table = $('#example').DataTable( {
    paging: false
} );

table.destroy();

table = $('#example').DataTable( {
    searching: false
} );

You can also do:

table = $('#example').DataTable( {
    destroy: true,
    searching: false
});

This has some performance consequences and it will also reset paging.

like image 5
kgiannakakis Avatar answered Oct 16 '22 04:10

kgiannakakis