这是用于完全不同的东西的数据表小提琴的一个分支。
http://jsfiddle.net/72xGx/
此示例以数据表站点上的“范围过滤”示例为起点。它使用复选框来确定是否应应用过滤器,并且默认情况下所有过滤器都处于打开状态。当您选中和取消选中复选框时,表中的数据应相应过滤。毫无疑问,这可以进行一些清理,但我相信您会发现这是实现您既定目标的一种途径。
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();
});
});
html表格结构的一部分
(以便在元素上标注一些css
类)<tr>
<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>