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
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();
});
});
7 Comments
Astronaut
Hi Nicola, that is exactly what I don't have, how do I create a filter function for datatables?
Nicola Peluchetti
@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
Astronaut
Hi Nicola, I now have the problem that all my tables get this filter applied. How do I apply it to just one table?
vol7ron
@AdamSurfari:
if ( oSettings.nTable != document.getElementById( 'your_table_id' );{ return true;}vol7ron
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/…
|