Change filter value from code

Change filter value from code

pawelpawel Posts: 14Questions: 0Answers: 0
edited October 2010 in General
Hi everybody,

I changed the input text box functionallity to filter only after press the enter key and I've been trying to clear the value of the text box after this. I did it with something like this code:

[code]

$('#example input').bind('keyup',function(e){
if (e.keyCode == 13)
{
oTable.fnFilter(this.value);
this.value = "";
}
});

[/code]

But this code doesn't work...If I use another filter in the table then the input text filter will have the previous value unless I first press enter with a empty value. Has Anybody got something like this?

Cheers,
Pawel.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    When you say "If I use another filter in the table", do you mean that you have two filtering input elements (by altering sDom)? If so, then your code will need to clear the input for both filters at the same time (assuming I understand the problem correctly). Something like: $('#example input').val(''); should do it.

    Allan
  • pawelpawel Posts: 14Questions: 0Answers: 0
    Hi Allan,

    Thanks for replying soon. I'm gonna try explain better my problem.

    In addition to the input text filter I'm using two columns filters. My Idea is if the user press enter with some value that wants to search in the table then he will get the results and will be able to select differents pages in the table, filtered with the search value. In this situation if the user wants to filter the table with one of the columns filters then the search value will be cleared and will not affect to the table filter so he will not have to clear the input text, filter like this and then use another filter.

    Do you understand better what I mean?

    Cheers,
    Pawel.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Yes - thanks for the explanation. I think my basic sentiment from before still stands - you'll simply need to clear the values in your event handler for each of the inputs.

    Allan
  • pawelpawel Posts: 14Questions: 0Answers: 0
    That's right, I cleared the values in the event handler and it works fine.

    Thanks for your time.

    Cheers,
    Pawel.
  • pawelpawel Posts: 14Questions: 0Answers: 0
    edited October 2010
    Hi Allan,

    I'm sorry but yesterday I didn't try so much the code and I've just realized I'm still having a problem. I don´t know what I have to use to set the value of a filter from code, I mean, yesterday I tried with this code:

    [code]
    $("#example_filter input").bind('keypress', function(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
    e.preventDefault();
    oTable.fnFilter(this.value);
    $("#example_filter input").val('');
    }
    });
    [/code]

    This code clears the value of the text box but the filter still has the previous value, what am I doing wrong?

    Cheers,
    Pawel.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Try: $("#example input").val('');

    The sector you have will only match one element. Bare in mind that you can't have duplicate IDs in an HTML document, so DataTables only applies an ID to the first instance of a control (example_filter for example).

    Allan
  • pawelpawel Posts: 14Questions: 0Answers: 0
    Hi Allan,

    Thanks for replying. I tried with that and it works fine, cheers !

    Regards,
    Pawel.
This discussion has been closed.