How can I tell if the user has performed a search?

How can I tell if the user has performed a search?

hpachecohpacheco Posts: 19Questions: 0Answers: 0
edited March 2011 in General
Hello Alan,

I would like to know if there's a way to check if the user has searched for something, as I have to clear the text in specific columns if the table is being filtered.
I would then use fnDrawCallback to clear the text, as hiding the columns would make the table resize and that would be weird.

Also, is fnDrawCallback the best approach to do so?

Thanks in advance

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hi hpacheco,

    Is it the global filter (the one that DataTables adds automatically) that you are using (rather than a column specific filter)? If so - then yes I'd say fnDrawCallback is the way to go. DataTables provides a flag to indicate if the table has been filtered on a draw. You can do something like:

    [code]
    "fnDrawCallback": function ( oSettings ) {
    if ( oSettings.bFiltered ) {
    alert( 'Table has been filtered' );
    } else {
    alert( 'No filter on this draw' );
    }
    }
    [/code]
    There are other approaches as well - you could scan the internal settings object for the filtering string if you wanted (oSettings.oPreviousSearch.sSearch).

    Regards,
    Allan
  • hpachecohpacheco Posts: 19Questions: 0Answers: 0
    Hi Alan,

    I tried the code you posted - thanks for that btw - and oSettings.bFiltered was always returning true.
    I'm now checking if oSettings.oPreviousSearch.sSearch is equal to an empty string and that seems to work fine.. I don't know if that's a bug in dataTables or in my script, but you may want to have a look.

    Cheers,
    Hugo
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Hah! Damn - thanks for pointing that out. I'll take a look at and address that in the next release. Thinking about providing events which you can bind to in future for this kind of thing as well - hopefully make it easier :-)

    Regards,
    Allan
This discussion has been closed.