Datatables Responsive - sort by column names clicked in ul list
Datatables Responsive - sort by column names clicked in ul list
Hi everyone.
Short introduction to the problem. In my app I have datatables. In some form user set which columns he/she want to see and in which order. Because of that I call all columns by their name by table.column("COLUMN_NAME:name"). I found a way to order columns by name which is table.column("COLUMN_NAME:name"):order('DIRECTION').
Now I'm having problem with ordering table according to clicked column name in ul list in responsive extension. Is there any way to make it possible to order table in such a way?
I found line 764 in dataTables.responsive.js which creates li in ul list. But I don't know how to make clicked li trigger sorting function like clicking th do.
Thank you for your help and any advice.
Krzysztof Maliszewski
This question has accepted answers - jump to:
Answers
The order of the items in the list is defined by the columns - it will display in the same order as the columns that have been hidden.
If you want to display in a different order you would need to define a custom renderer:
responsive.details.renderer
.Allan
This is not what I meant.
What I want to beeing accomplished is sorting whole table according to clicked li title. In other words: there is li with title Name, if I click this li, whole table is sorted by column Name.
Oh I see - thanks for the clarification. You would need to add an event listener to the elements that will then trigger the
order()
method.Allan
So... You suggest to add listener to span.dtr-title which onclick will take its parent (li) data-dtr-index attribute value which is column index and call table.column(COLUMN_INDEX).order('DIRECTION') function and redraw table by draw(), yes?
If it works it'd be great because it'd work in newer versions of extension because of no changes in dataTables.responsive.js.
I'll try today and post an answer in afternoon.
Krzysztof Maliszewski
Yes - that sounds like the perfect approach to me. You will need to make sure the index is an integer (just
*1
) so DataTables sees it as a column data index selector.Allan
Solved my problem so read next post.
Problem was of course in creating .dtr-title after creating "on" event. Delegate must be used:
$(document).on("click", ".dtr-title", function(){
alert(1);
});
Think it works:
I'm not very good and JS so sorry if code is nasty.
CSS for clearer view:
CSS for li sorting icons is:
(remember to change paths to images! these are in my case)
And the last thing to do is adding sorting icons to all ul>li rows like in th. I don't know how to force dataTables script to add class after above sorting and drawing dataTable.
I think I need some draw callback after this kind of ordering. But is drawCallback enought and if 'yes', how to call it?
You would need to use
order
to known when an order event has occurred and then update the sub-title headers accordingly.Allan
One more thing I've forgotten to mention: I use server side data.
As far as I understand what you mean is that I should use order
order
event to know the exact point in time that order has occured to update li with classes sorting_asc or sorting_desc as they should be. But...There is a problem: because I use server side data I need to use
draw()
method to redraw data. Because of that whole table is redrawn and there is no peace of code in original dataTables which would draw order icons in li so I need to do it afterdraw()
(or am I wrong?).I know what to put inside above function which is sth like
How should I create such a function?
Yes, if you use server-side processing (which I didn't know before, sorry), you would use
draw
since your would need to update this on every draw.Allan