Datatable column sort

Datatable column sort

miketwistmiketwist Posts: 2Questions: 0Answers: 0
edited September 2009 in General
Hello,
My question is about sorting. For example :
I'm showing 10 to 20 of 50 entries of a datatable.
Is it possible to sort a column only for these 10 entries, not for all entries ?
Also stay on the current page after the sort and not go back and display the first entries.

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Hi miketwist,

    1. I'm afraid that this isn't possible with the functions that are built into DataTables. However, it would be quite possible to do this by using the development options that DataTables presents. You know the rows which are on display, and the data in those rows, therefore you could reorder them. You'll need to use some of the internals of DataTables data storage for this, so shout if you need any pointers.

    2. This is discussed in this thread (and a couple of others): http://datatables.net/forums/comments.php?DiscussionID=573

    Regards,
    Allan
  • miketwistmiketwist Posts: 2Questions: 0Answers: 0
    Hi Allan,
    First thanks for your answer, I'll follow your method to sort only the displayed entries.
    So how can I know which and how many rows are on display and sort them ?

    Cheers
  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Hi miketwist,

    All the information about each table is contained in an "internal" settings object (you can use it externally, but it's not documented...) - which you can get access to using fnSettings() (or it is passed into a lot of functions which DataTables uses). Then you can use:

    fnRecordsTotal() - total number of records
    fnRecordsDisplay() - records in the 'display' (i.e. after filtering)
    fnDisplayEnd() - End point of the current 'page'
    _iDisplayStart - Start point of the current 'page'
    _iDisplayLength - Length of records for the current 'page'.

    To see what records should be displayed and other information about the records on display. Then you make use of aiDisplay - which is an array of integers, pointing at aoData which is where the TR and data information is.

    For example - the following will get the first TR node which is on display, regardless of sorting and filtering:

    oSettings.aoData[ oSettings.aiDisplay[ oSettings._iDisplayStart ] ].nTr

    So you can use:

    oSettings.aoData[ oSettings.aiDisplay[ oSettings._iDisplayStart ] ]
    to
    oSettings.aoData[ oSettings.aiDisplay[ oSettings._iDisplayStart + oSettings.fnDisplayEnd() ] ]

    to sort the data which is currently on display. It might be worth doing your sort in fnDrawCallback().

    Hope this helps!

    Regards,
    Allan
This discussion has been closed.