reading searchstring + using internal sort for an array?
reading searchstring + using internal sort for an array?
Hi Allan,
thanks for your helping with my problems in the past! My project moves slowly to V1.0 ;-)
Some (hopefully) final questions left:
Is it possible, to read out the actual search/filter string for a specific column via the API?
The next would be awesome, but i'm afraid it's not possible: i' have select boxes for each column and they be filled with the fnGetColumnData plugin on each Ajaxrequest. Now it would be nice to use your sorting algo to sort the values in the select box (or even before i put it there) in the same manner like the rest of the column. Is this possible?
Best regards
Daniel
thanks for your helping with my problems in the past! My project moves slowly to V1.0 ;-)
Some (hopefully) final questions left:
Is it possible, to read out the actual search/filter string for a specific column via the API?
The next would be awesome, but i'm afraid it's not possible: i' have select boxes for each column and they be filled with the fnGetColumnData plugin on each Ajaxrequest. Now it would be nice to use your sorting algo to sort the values in the select box (or even before i put it there) in the same manner like the rest of the column. Is this possible?
Best regards
Daniel
This discussion has been closed.
Replies
Good to hear things are moving along nicely :-)
1. Get the search string for a column: yes indeed this is possible. First thing to do is grab the settings object for your table using fnSettings() ( http://datatables.net/api#fnSettings ), and then access the "aoPreSearchCols" property of that. This is an array of objects, one entry for each column, and the sSearch property of that object is what you want. So in code:
[code]
var oSettings = oTable.fnSettings();
var sFirstColumnSearch = oSettings.aoPreSearchCols[0].sSearch;
[/code]
2. So I get this right - you want to search by the value of the checkbox (i.e. ), rather than if the checkbox is checked or not? It's certainly possible to do the former - what you need to do is write a custom sorting plug-in which will pick out the term you want to sort on from the HTML string. See here how to develop sorting plug-ins: http://datatables.net/development/sorting
You could even make use of one of the plug-ins already available - for example the hidden title sorting, and modify your HTML: http://datatables.net/plug-ins/sorting#hidden_title
If you want to sort on whether the checkbox is checked or not - this is actually possible, although you need to reference the actual DOM element, rather than the cached one that DataTables has - something like this is discussed in the following post, although it's filtering focused rather than sorting: http://datatables.net/forums/comments.php?DiscussionID=297
Regards,
Allan
for the first. Thanks.
the second point: I had to describe it better. So here is the next try. ;-)
uaaah i see that i wrote select, i meant dropdownfields ()
i fill these with the fnGetColumnData plugin. Now i have unsorted, but unique values in the dropdown. Datatables itself can detect type of a column and pick the needed sorting algo, if i understand it right. A dream come true if its possible to use detection and sorting from datatables for my dropdown or get them already sorted from the plugin/datatables or something like that.
I hope description is better this time.
Best regards
Daniel
Okay, so you have the values for your being read from the column data, and you want the options in the to be sorted? Is that correct?
If that is the case, then it should be a simple matter of when you read the data in to create the select menu, just read it into an array, and then sort() that array :-). From there, you can loop over your array and output the select menu options in the correct order.
How does that sound?
Allan
that would be wonderful, but it's not that easy. :-( I cannot use sort() because the values are not only strings. There are also cols with numbers and cols with mixed nums+chars.
So sort() will fail on any other than strings.
Thats the reason why i ask if its possible to use the internal algo of Datatables to get the same sorting as in the table itself.
If it's not possible i have to build these detection/sorting routines on my own...
Thanks
Daniel
I think there are a couple of options open to you then:
1. Use DataTables to do the sorting for you through the API function fnSort(), and then read the data back for each column after it has been sorted. The downside is that the table will 'flicker' as it goes through each column being sorted.
2. Do a sort() on your array, but make use of the sorting functions that DataTables provides (in the $.fn.dataTableExt.oSort object - they are externally visible). You could even make use of the automatically detected sType.
3. Make use of a natural sorting algorithm such as the one available here for mixing numbers with letters: http://my.opera.com/GreyWyvern/blog/show.dml/1671288
I'd say option 2 is quite attractive as it ties in exactly with what DataTables is doing, and should be plenty fast on initialisation :-)
Regards,
Allan