Filtering numberic columns

Filtering numberic columns

visvis Posts: 4Questions: 0Answers: 0
edited November 2009 in General
Hi,

first - all compliments to DataTables author - this is great product.

Now the question. :)

We're using filtering of columns in our DataTables. But when we enter number in numeric column it is filtered as string.
e.g. If the numbers as 1, 5, 10, 15 and we enter 5 we would like only row with value 5 to match and not 5 and 15. Are we doing something wrong or can we fix this somehow using filter API ?

It would be great if our "general filter/search" input box would do the same, but if it is not possible that is OK too.

Thank you!

PS: Do you think it would be possible somehow to add "> 5" to return rows that match values that are greater than 5. Of course, we would implement something like that, we don't expect Allan to do that :)

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi vis,

    If you want to match just '5' you need to use regular expressions ( which fnFilter will allow http://datatables.net/api#fnFilter ). The key piece of knowledge here is that DataTables tries to be "smart" with filtering, and uses a regular expression of it's own on the global search, so this can mess things up a little sometimes if you aren't careful. It will concatenate the data from the row together (space separated) and try to then match on the words. You can have a play with the filtering / regex demo which will illustrate some of this: http://datatables.net/examples/api/regex.html

    To do a >5 filter, you need to use a plug-in filter - http://datatables.net/development/filtering . Which of the two filtering plug-in types you use, is really down to how you want to program it :-) - I think both will work. There is a range filter plug-in demo which you might find interesting to build off: http://datatables.net/examples/api/range_filtering.html

    Regards,
    Allan
  • visvis Posts: 4Questions: 0Answers: 0
    Hi Allan,

    Thank you a lot... :) We will try that.

    Best regards,
    ViS
This discussion has been closed.