fnFilter problem
fnFilter problem
So I have a table and in one of the columns a value can either be "Active" or "Inactive." Above the table is a dropdown that the user can select so they see only Inactive or Active items. Well the word "Inactive" contains the word "Active" and therefore does not filter correctly when the user selects "Active."
Doesn't seem like a huge problem, but I can't solve it. Any ideas?
Doesn't seem like a huge problem, but I can't solve it. Any ideas?
This discussion has been closed.
Replies
You can make use of DataTables regular expression handling for this. You'll need to make use of fnFilter (which you might already be doing for the select menu), and tell it not to escape regular expression characters and also which column to filter on: http://datatables.net/api#fnFilter .
Then you can append a hat ("^") to the start of your filter text (so you get "^Active" or "^Inactive") which will provide you with your unique search (as long as you use a specific column number - DataTables dose some "funky" stuff with regular expressions for the global search which can make this kind of thing difficult to do). You can play around with regular expressions in DataTables with this demo: http://datatables.net/1.5-beta/examples/api/regex.html
Regards,
Allan
[code]
$("select#show_act_inact").change(function () {
var val = $("select#show_act_inact option:selected").attr('value');
var regex = (val == "" ? "": "^"+val+"$");
oTable.fnFilter (regex,5,false);
});
[/code]
[code]
Show all
active
inactive
[/code]
My dropdown consists of three options, "All", "Active", and "Inactive."
When the user selects either Active and Inactive, if they then select All, it will not redraw the table... and I cannot figure out why.
My code:
[code]
var obj, tableToFilter, colToFilter;
function tblFilter(obj, tableToFilter, colToFilter) {
var val = $(obj).val();
if(val == "All") {
tableToFilter.fnDraw();
} else {
tableToFilter.fnFilter("^" + val + "$", colToFilter, false);
}
}
[/code]
Any ideas?
Try:
[code]
tableToFilter.fnFilter("", colToFilter, false);
[/code]
when you want to "filter" by "All".
Allan
[code]
var obj, tableToFilter, colToFilter;
function tblFilter(obj, tableToFilter, colToFilter) {
var val = $(obj).val();
if(val == "All") {
tableToFilter.fnFilter("(.*)",colToFilter, false);
tableToFilter.fnDraw();
} else {
tableToFilter.fnFilter("^" + val + "$", colToFilter, false);
}
}
[/code]
This will work
edit: Allan was faster ;)
Thanks!
I'm dynamically adding options to my selects based on data in the table. And sometimes said data can include the "&" character. Well when I'm trying to filter that row, it comes back with no results.
I'm assuming this is another regex solutions. Any ideas?
Allan