Filter doesn't seem to actually work..
Filter doesn't seem to actually work..
I've created a fiddle with my code: http://jsfiddle.net/tpv3eakn/17/
This is an attempt to implement a very basic date range filter using the filter() function. I set it to run any time the date fields are changed. The code seems to execute just fine, and it logs a proper true/false to the console. However, the table itself never reflects those changes. What am I doing wrong?
Stipulations: The solution must be specific to the instance, not global. I'd prefer it to be column-agnostic and look for a class on the td instead e.g. class="date-filterable" but I understand if this requires a lot of work.
Answers
In fact, if I could make it column agnostic, then I could make the filter global.
I think you want
search()
. As the documentation forfilter()
states:Allan
I've read and re-read that a dozen times. I don't get it.
When I use the search, I can't do a date range. It just puts the text I specify into the search filter field. This is not what I want.
This example seems to be pretty close to what you are doing. I don't remember JS being very good at handling dates / datetime though compared to other languages.
Nope. That's a global solution. I did stipulate that the solution needs to be instance specific.
There isn't a way of directly providing a plug-in custom filter that operates on a single instance without a little bit of extra logic. This is a shortcoming that needs to be addressed in a future version, but fortunately the extra bit of logic is fairly trivial:
Not ideal I know, but it should work.
Regarding the
filter()
method - the key point is thatfilter()
does not filter the table! It filters the data contains in the API instance (think of it the same asArray.prototype.filter
, whilesearch()
does effect the display of the table and applies search terms.Allan
Hmm. The question then becomes, can I use the .search() in conjunction with having my own search? I.E. If I have three input fields on the page: search, date_start, and date_end, would it be possible to have all of those running at the same time and have them preserve their searching/filtering when I switch pages or sorts?
So far any time I call .search() all it does is replace what I have in my search with the date. I need the search to remain in place and date filter in addition.
Sure - but
search()
uses the built in internal searching that DataTables provides. If you are using custom search plug-ins, then they don't directly relate andsearch()
doesn't effect them.There is only one global search on the table - the search input text box that is visible relates to
search()
(i.e. they are both the global search input for the built in search options).Can you link to the page please?
Allan
Unfortunately the page is on an internal webs. I do have a fiddle though, which I provided in my first post.
The fiddle shows the use of
filter()
though, which as we discussed above does not filter the table, but rather the API result set.For a table search with a range, you would need to use a plug-in like this example: http://datatables.net/examples/plug-ins/range_filtering.html .
Allan
You can swap filter() for search() easy enough in the fiddle. As for the link, once again, that is a global solution. I need an instance specific solution.
I could, but as the documentation for
search()
notes, there is no option for passing in a function to it (or rather, the lack of documentation for a function being passed in means that it isn't supported).You can make a search plug-in table specific by adding:
at the top of the plug-in function. It isn't ideal I know, but at the moment, it is the only way to do range, table specific searching in DataTables. Improving this is something I'm going to work on for 1.11.
Allan