Astronaut
Astronaut

Reputation: 7051

datatables create filter checkbox

Does anyone have examples on how to create a Datatablest filter checkbox? I want to display only rows that have a value above X or below Y being controlled by a checkbox.

Upvotes: 6

Views: 20366

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76890

You would have to write your own custom filtering function but after that the code would be vary simple

$(document).ready(function() {
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
        var checked = $('#checkbox').is(':checked');

        if (checked && aData[4] > 1.5) {
            return true;
        }
        if (!checked && aData[4] <= 1.5) {
            return true;
        }
        return false;
    });
    var oTable = $('#example').dataTable();
    $('#checkbox').on("click", function(e) {
        oTable.fnDraw();
    });

});​

fiddle http://jsfiddle.net/nicolapeluchetti/WVYNX/2/

Upvotes: 18

Related Questions