Alex
Alex

Reputation: 1040

Datatables: How to disable filtering after init

On my page I'm running one datatables init script which can initialize multiple tables at once. Now let's say I want to change some options, e.g. disable filtering, on one of those tables but not all, what would be the best way to approach this?

Example : http://jsfiddle.net/HEDvf/1165/

$(document).ready(function() {
    $('.dataTable').dataTable({
        "sPaginationType": "full_numbers"
    });
    $('#example2').dataTable({
        "bFilter": false
    });
});

As you can see I have 2 tables in html loaded, I initialize both of them at the same time by selecting the '.datatable' class, which they both have. Then I would like to disable filtering for the 2nd table, to select this table I use the id 'example2' which only the 2nd table has. However changing the setting for this table doesn't work, the popup indicates I can't reinitialize a table, but should I otherwise change an already initialize table's options?

Upvotes: 1

Views: 5003

Answers (1)

Matt Pileggi
Matt Pileggi

Reputation: 7196

You can access the existing DataTable object by calling dataTable with no arguments:

var dt = $('#example2').dataTable();

I don't know which values are capable of being modified after the fact, so it may or may not work changing it then:

dt.DataTable.settings.bFiltered = false;

If you need to do it at initialization time I can think of two options. First, is to filter it out before calling dataTable

$('.dataTable').not('#example2').dataTable(...);
$('#example2').dataTable(.. other options ...);

Alternatively, create a mechanism for setting those options at the html level:

$('.dataTable').each(function(k,ele) {
    ele = $(ele);
    var opts = {
      sPaginationType: ele.data('pagination-type') || 'full_numbers',
      bFilter: ele.data('bfilter') !== 'false'
    }
    // or only set it if it exists
    if (ele.data('bfilter')) {
       opts.bFilter = ele.data('bfilter') === 'true';
    }
    ele.dataTable(opts);
 });

That may be a little overkill but you could make html similar to:

<div class="datatable" data-bfilter="false" data-pagination-type="mypagination_option"></div>

Upvotes: 3

Related Questions