How to replace colunm sort behaviour with my own

How to replace colunm sort behaviour with my own

yassenyassen Posts: 10Questions: 0Answers: 0
edited October 2013 in DataTables 1.9
Hi and thanks to the authors for a great piece of software!

I try to solve the following issue:

How can I replace the default behaviour for column sorting with my own? I want to load the table from the server with that field used in an "ORDER BY" clause instead of letting the DataTables object sort the in-memory data. (Why? -- see below.)

Can anyone point me to some direction?
Your help is highly appreciated, thanks!
Yassen

P.S. Motivation:
I have a table that consists of the result of an SQL query with a LIMIT clause. When the user clicks on a column to sort the data, I need to actually perform a new query -- if I don't, the DataTables table will sort the in-memory data, but that is only a limited portion of the whole data and will not represent the true data in the database.

Replies

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    You probably need bSortable. Try a forum search.
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Ahh, thanks, tangerine!
    It seems that {"bSortable": false} for all columns and also adding sort listeners via fnSortListener (will the event still be fired? I'm gonna find out) should do the trick ... no?
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    I'd suggest just using bSort to disable DataTables sorting :-)

    Allan
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Oops! Sorry guys.
    FWIW I just took a look at some of my own code, and I'm disabling initial sort by giving aaSorting an empty array. Works for me.
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    Yup - aaSorting:[] allows you to keep user sorting enabled, but disable the initial sort. Can be quite useful!

    Allan
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Guys, thanks so much!

    I have stuff like currency (e.g. $5.34) and percent data (32.6%), which I need sorted properly (numerically, not alphabetically). Can I keep something like a hidden column only for sorting, and another one with the formatted data to show to the user? Thanks!
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    Or, you could use DataTables 1.10 which has currency support like that built in :-). The pre-beta software is available here: https://github.com/DataTables/DataTables/tree/1_10_wip/media/js . There isn't support for the personage type built in, perhaps I should add that... hmmm. Until then, there dis plug-in: http://datatables.net/plug-ins/sorting#percentage

    Allan
  • yassenyassen Posts: 10Questions: 0Answers: 0
    edited October 2013
    ahhh allan, you saved my day, thank you! I stay with the old version and I am able to fix all sorting issues now when I saw your examples.

    Another Q: How can I apply custom style (e.g. background color) for numeric data that is behind/below certain limits? (Just refer to the proper source of info so that I can educate myself.) Tanks!
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    You could use fnCreatedCell to add a class based on the data in a cell.

    Allan
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Yeah, however, on sorting, I loose the custom style ... I managed to achieve that server-side, but hit the same problem. Any clue on how to retain that style "forever and ever"? Or have DataTables re-do that on sorting?
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    What custom style? How are you using fnCreatedCell - that should rested the style. Please link to a test case showing the issue.

    Allan
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Sure, here it is: http://odesk.natxela.com:8000/testcase

    When you sort the last column, the special bgr colours are lost :(
    Allan, your help is *highly* appreciated, thank you!
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Ahh! I can see now that if I sort on first and second column,custom colors are still there! Didn't notice that until now.

    This means that colours are only temporarily lost because DataTables code applies highlighting styles temporarily to underline the sorted column. How can one suppress that or have the DataTables code skip already coloured stuff?
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    > How can one suppress that or have the DataTables code skip already coloured stuff?

    Use the bSortClasses option and set it to false.

    Allan
  • yassenyassen Posts: 10Questions: 0Answers: 0
    Allan, thanks, it works now! Your support rocks as well as DataTables itself. Have a great day!
    Yassen
This discussion has been closed.