Custom Filtering (date range) with SSP Class
Custom Filtering (date range) with SSP Class
Hi, I'm pretty new to datatables, had the luck of finding and implementing it on my site the day before you released 1.10 :)
So now I've updated everything to use the new version but there is a feature I'm trying to add which isn't working. That feature is date range filtering. I followed the example from your site (http://datatables.net/examples/plug-ins/range_filtering.html) but it just seems to ignore the custom filter. I've spent ages battling with it, trying to work out where things are going wrong and then it hit me that it could be the way data is being sent/not sent to the database. I'm fetching the data out of a MySQL database using the ssp.class.php file you provided in the 1.10 "examples/server_side/scripts" directory.
My simple test to check if it is working is simply to filter out everything:
$.fn.dataTable.ext.search.push(
function( settings, data, dataIndex )
{
return false;
}
);
So my questions are:
1) Are custom search filters i.e. data range filters supposed to work with the default ssp MySQL method?
2) If not then could you suggest where I should be looking to add this functionality myself? My guess is that I might have to tinker in _fnFilterComplete (jquery.dataTables.js) or the static filter function in ssp.class.php.
This question has an accepted answers - jump to answer
Answers
It was pretty clear that the custom filtering doesn't work with SQL supplied data, at least for me I couldn't get it working. I then thought about tweaking it so that for all other filtering it would use the database filter system and then for date ranges I could just do a client-side hack but that didn't really work and I got worried when I considered how it would run with tens of thousands of records further down the line. I then moved onto trying to do it with a RegEx search but I quickly realised that pretty much everybody on the planet says doing a date range filter with RegEx is a very very bad idea!
In the end I decided to write my own cheaters system. Each time they type into one of the date input text fields it fires a java call to do a "column.search" but I manually craft the search term. I prefix the search with the keyword "Date:". I then tack the values from both the start and end text fields together so a search might actually look like this:
"Date:2014-04-01 02:15:35<>2014-05-02 18:21:56"
Then in the ssp.class.php file I modified the column search code inside the filter function. That then looks for the "Date:" prefix and then performs my date range search. In my test version I have enabled the Any+Time date/time picker and will prevent direct typing into the input fields. I know this won't stop people from sneakily putting their own data in, it's just for ease of not worrying about the date formatting people might type. This way I know if the data is in the wrong format then they are doing something they shouldn't.
Above my DataTable instance I have several filter boxes. Most of these are just plain input text fields. For my dates I have 2 input text fields, a start date and an end date:
Then the final phase was to mod the static filter function in ssp.class.php from (line 164 for me v1.10.0):
To:
I'm not sure why it's ignoring all the code indentation etc in my code posts, I'll update the post if anyone can tell me.
I've been having an issue with this as well.. i've included the search. push function but i can't get the internal function to call.. is this a serverside issue ? if not, i will add a test link..
tannsan, i'll have a look through your code as well.. thanks
Thanks for sharing your solution with us!
Custom filtered on the client-side doesn't work when you have server-side processing enabled, since you are server-side processing the data, not client-side :-). So yes, it needs to be done on the server-side.
I broke that for a little while. Fixed now :-)
Allan
ha yeah I noticed you super quick fixed the forum code indentation when I checked back for the reply today! Still doesn't indent when Previewing though ;)
I also noticed that the reply email is sending the links with the HTML links in plain text format:
Also, when you do "Preview" on this comment box there is no button to return to editing it, just "Save Draft" and "Post Comment". Saving draft and tapping F5 to refresh gets around that for now.
Hope you don't mind me throwing these nega-vibes your way, I think you've done an amazing job with DataTables and the site.
Edit: hmmm maybe indenting does work with previewing, when I first published this comment the indenting was still broke even though the code in my other comment was indented but then when I refreshed the page all of it looked ok. Could just be some gremlins hiding in my cache.
Thanks - I keep forgetting to mention that!
TannSan, thanks for posting your code.
I've been having the same issues and I thought i'd come back and let you know how i went about it.
Rather than send the dates as part of the search string, i sent the date information via ajax using the data option.
On this particular grid, i have 4 additional date range filters. i send the affected column, the type of data and the range values to the script
I then run a function which creates the HTML and adds the handlers. On the key up for each handler. i call the search
I then change the script to loop through the ajax data and matching any columns
It's a different way of doing things but now it's set up, i can have any number of range filters set up and the script handles accordingly.
This code hasn't been properly tested yet but it gives a good idea
Lynd
=