fully custom filtering

fully custom filtering

cosborncosborn Posts: 2Questions: 0Answers: 0
edited December 2012 in Feature requests
I want to implement fully-custom filtering, but as far as I can tell the only custom filtering options are hamstrung in strange ways:

- I can implicitly change the behavior of per-column filtering by modifying the value that standard filtering sees (ofnSearch), but standard filtering logic still applies to the resulting string
- I can add a fully custom row-based filter (afnFiltering), but it has serious flaws: it's global, so it won't work easily for multi-table pages, and it's in addition to the standard filtering, so if I want to replace the standard filtering I need to disable and replace the entire input element.

What I really want to do is provide my own mapping from search input text to filtering function. That is, I convert the search string to a function which returns true or false when applied to each row, just like the afnFiltering functions but NOT global and INSTEAD of the standard filtering.

I'd be happy to implement this as a plugin, but from reading the code I can't find any suitable extension points. I think what I want is a way to intercept _fnFilterCreateSearch and perhaps also to disabuse _fnFilter of the assumption that longer search strings are more restrictive.

Are there any pointers you can give me?

Thanks!

Replies

  • allanallan Posts: 61,452Questions: 1Answers: 10,055 Site admin
    Agreed - the filtering implementation that DataTables uses is tied into the core at the moment, so the extensions that are available are not as flexible as they perhaps could be. The plan is, in DataTables 1.11 to fully modularise the library, so you will be able to fully replace the filter module with a custom one if you need to. The example I keep thinking of is fuzzy matching - type "Hello" and it would match "Hullo" for example.

    Having said that, custom filtering can currently be done using custom row filters ( http://datatables.net/development/filtering ), and not making use of the built in filtering. Its not quite as efficient (since DataTables can't optimise external code!), but you can use that to do some fairly advanced filtering. However, as you say, its global, so not suitable for all situations.

    Bottom line, I'm afraid DataTables isn't quite as flexible in this regard as I'd like it to be - at the moment you'd need to hack around on the core.

    Allan
  • cosborncosborn Posts: 2Questions: 0Answers: 0
    Thank you very much for the quick response! I ended up getting the behavior I needed by replacing the string input field with my own (thus disabling standard filtering) and implementing filtering as follows:
    - in the filter input's change handler, generate a custom row filter function and add it to the $.fn.dataTableExt.afnFiltering array, then run filtering by calling fnFilter('').
    - in the table's fnDrawCallback, remove the custom row filter function from the afnFiltering array so it doesn't affect future filter options or other tables on the same page

    I'll revisit this when the 1.11 enhancements are available, but until then it appears to get the job done.
  • FredFlintstoneFredFlintstone Posts: 24Questions: 0Answers: 0
    edited April 2013
    "remove the custom row filter function from the afnFiltering array " - can i ask how you did that?

    it's ok. what i found easy was to simply reset all custom filters to their default values before going off to the server to retrieve data again and it works as expected.
This discussion has been closed.