6

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.

1 Answer 1

18

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/

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Nicola, that is exactly what I don't have, how do I create a filter function for datatables?
@AdamSurfari i updated my answer, basically now if the checkbox is checked and the fifth column is > 1.5 i show the row, if it's unchecked and it's <= 1.5 i show the row
Hi Nicola, I now have the problem that all my tables get this filter applied. How do I apply it to just one table?
@AdamSurfari: if ( oSettings.nTable != document.getElementById( 'your_table_id' );{ return true;}
I took Nicola's fiddle one step further and allowed to filter by distinct values of a column jsfiddle.net/vol7ron/z7wJ5 additionally someone posted this in the dataTables forum: datatables.net/forums/discussion/3118/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.