using checkboxes to filter
using checkboxes to filter
richardsonja
Posts: 6Questions: 0Answers: 0
I saw a lot of different forum posts asking about using check boxes to filter a data table. between 3 or 4 posts here and stock overflow, I've figured out one way to do it and wanted to share with the community!
The next question I have is can I filterAll on different columns independently. =) For example by Engine and then by Platform. so Gecko and Win 98+.
[code]
$.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
var settings = $.fn.dataTableSettings;
for (var i = 0; i < settings.length; i++) {
settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
}
};
$(document).ready(function () {
//its important to throw in into aTable (or any name you give it)
var aTable = $('#example').dataTable({
"bStateSave": false,
"sPaginationType": "full_numbers",
"oLanguage": { "sSearch": "Filter:" },
"bJQueryUI": true
});
$("input[name='types[]']").click(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='types[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
//add the OR operator
stringofstuff += "|" + $(this).val();
}
}
});
//2nd parameter is the column to filter
aTable.fnFilterAll(stringofstuff, 0, true, false);
});
});
[/code]
then the html
[code]
Gecko
Trident
Presto
[/code]
The next question I have is can I filterAll on different columns independently. =) For example by Engine and then by Platform. so Gecko and Win 98+.
[code]
$.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
var settings = $.fn.dataTableSettings;
for (var i = 0; i < settings.length; i++) {
settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
}
};
$(document).ready(function () {
//its important to throw in into aTable (or any name you give it)
var aTable = $('#example').dataTable({
"bStateSave": false,
"sPaginationType": "full_numbers",
"oLanguage": { "sSearch": "Filter:" },
"bJQueryUI": true
});
$("input[name='types[]']").click(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='types[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
//add the OR operator
stringofstuff += "|" + $(this).val();
}
}
});
//2nd parameter is the column to filter
aTable.fnFilterAll(stringofstuff, 0, true, false);
});
});
[/code]
then the html
[code]
Gecko
Trident
Presto
[/code]
This discussion has been closed.
Replies
I'm not sure if that was the exact issue but when i did that, it started working >_<
You need a bit of custom code to do that, since obviously DataTables doesn't know anything about the checkboxes. You could use fnStateSaveParams and fnStateLoadParams to hook into the DataTables state saving code.
Allan
I do have another question about this. i have a page with three datatables. each one being loaded into different varaibles (oTable, oclientTable, oFullProductTable). however the filter affects all three of them. how do i limit the execution of the fnFilterAll to just a single table?
here is the code:
[code]
$.fn.dataTableExt.oApi.fnFilterAll = function (oSettings, sInput, iColumn, bRegex, bSmart) {
//alert(oSettings + "|" + sInput + "|" + iColumn + "|" + bRegex + "|" + bSmart);
var settings = $.fn.dataTableSettings;
for (var i = 0; i < settings.length; i++) {
settings[i].oInstance.fnFilter(sInput, iColumn, bRegex, bSmart);
}
};
$(document).ready(function () {
var stringofstuff = "";
var firstChecked = true;
$.each($("input[name='status[]']"), function () {
if ($(this).is(":checked")) {
if (firstChecked) {
stringofstuff += $(this).val();
firstChecked = false;
} else {
stringofstuff += "|" + $(this).val();
}
}
});
var oFullProductTable = $('.ProjectList').dataTable({
"bStateSave": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
var oclientTable = $('.ClientList').dataTable({
"bStateSave": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
var oTable = $('.ListOfWatchListProjects').dataTable({
"bStateSave": false,
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
oTable.fnFilterAll(stringofstuff, 3, true, false);
});
[/code]