I read the documentation at http://datatables.net/examples/plug-ins/range_filtering.html for filtering by a range, but I don't quite understand how to filter items by a row's class.
I want to have a several buttons that when clicked, filter the dataTable by the classes (string values) of each row. So for example, if you have
<tr class="dont_filter">
<tr class="do_filter">
They would both show up by default. When someone clicks one of the buttons, all rows with "do_filter" are hidden, and dataTables redraws the list so that this change occurs throughout all pages.
Here's a fork of a datatables fiddle that was used for something entirely different.
http://jsfiddle.net/72xGx/
This sample takes the 'range filtering' example on the datatables site as a starting point. It uses checkboxes to determine if a filter should be applied, and all filters are on by default. As you check and un-check the checkboxes, they data in the table should accordingly filter. No doubt this could stand some cleaning up, but I believe you will find it is one route to accomplish your stated goal.
JavaScript
/* Custom filtering function which will filter data in column four between two values */
$.fn.dataTableExt.afnFiltering.push(
function (oSettings, aData, iDataIndex) {
var gA = $('#ckb-gradeA').is(':checked'),
gC = $('#ckb-gradeC').is(':checked'),
gU = $('#ckb-gradeU').is(':checked'),
gX = $('#ckb-gradeX').is(':checked');
var myRowClass = oSettings.aoData[iDataIndex].nTr.className;
if (myRowClass === 'gradeA' || myRowClass === 'gradeA even' || myRowClass === 'gradeA odd') {
return gA === true ? true : false;
} else if (myRowClass === 'gradeC' || myRowClass === 'gradeC even' || myRowClass === 'gradeC odd') {
return gC === true ? true : false;
} else if (myRowClass === 'gradeU' || myRowClass === 'gradeU even' || myRowClass === 'gradeU odd') {
return gU === true ? true : false;
} else if (myRowClass === 'gradeX' || myRowClass === 'gradeX even' || myRowClass === 'gradeX odd') {
return gX === true ? true : false;
} else {
return false;
}
});
$(function () {
/* Initialise datatables */
var oTable = $('#example').dataTable();
/* Add event listeners to the two range filtering inputs */
$('#ckb-gradeA').change(function () {
oTable.fnDraw();
});
$('#ckb-gradeC').change(function () {
oTable.fnDraw();
});
$('#ckb-gradeU').change(function () {
oTable.fnDraw();
});
$('#ckb-gradeX').change(function () {
oTable.fnDraw();
});
});
part of the html table structure
(so as to note some of the css
classes on the <tr>
elements)
<tr class="gradeC">
<td>Trident</td>
<td>Internet Explorer 5.0</td>
<td>Win 95+</td>
<td class="center">5</td>
<td class="center">C</td>
</tr>
<tr class="gradeA">
<td>Trident</td>
<td>Internet Explorer 5.5</td>
<td>Win 95+</td>
<td class="center">5.5</td>
<td class="center">A</td>
</tr>
<tr class="gradeA">
<td>Trident</td>
<td>Internet Explorer 6</td>
<td>Win 98+</td>
<td class="center">6</td>
<td class="center">A</td>
</tr>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With