fnFilter problem

fnFilter problem

jamillerjamiller Posts: 27Questions: 0Answers: 0
edited August 2009 in General
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?

Replies

  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin
    Hi jamiller,

    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
  • fliesflies Posts: 35Questions: 0Answers: 0
    edited August 2009
    Had done the same sometime ago so i share a code:


    [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]
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    edited August 2009
    Thanks for the replies! That did the trick but now I have another problem...

    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?
  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin
    It's because you need to clear the filter :-) - it retains the column filter that you were using before.

    Try:

    [code]
    tableToFilter.fnFilter("", colToFilter, false);
    [/code]
    when you want to "filter" by "All".

    Allan
  • fliesflies Posts: 35Questions: 0Answers: 0
    edited August 2009
    You are not resetting internal filter variable:

    [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 ;)
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    that worked!

    Thanks!
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    ahh! Now I have another filtering problem...

    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?
  • allanallan Posts: 61,710Questions: 1Answers: 10,103 Site admin
    I don't think that '&' is actually a special character in regex (bit rusty, so might be wrong) - could it be because it's actually stored as "&" in the HTML?

    Allan
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    Good point. But after checking, it is not & in the html. I'll check to see if it's a regex character. I'm betting it is.
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    hmm. Actually, "$" is not a special character in regex. I'll try manually escaping it...
  • jamillerjamiller Posts: 27Questions: 0Answers: 0
    I mean, the "&" character... not the "$"
This discussion has been closed.