fnSort and hidden rows?

fnSort and hidden rows?

krille83krille83 Posts: 2Questions: 1Answers: 0

Hi,

I'm using Datatables 1.9 and i have a problem with fnSort. When using the fnSort-function it only sorts visible rows, and not rows hidden with "display: none". Is it possible to make fnSort sort all rows in the table, not just the visible ones?

Thanks!

Answers

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Datatables doesn't know about the "display: none" you have applied. It sorts the data it has in its cache and not the data in the dom. The resulting sort will be correct but you have the row hidden so its still not displayed after the sort.

    Maybe I'm missing something but are you expecting the row with "display: none" to appear?

    Kevin

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    Also, if they are hidden, then what difference does it make if they are included in the sort or not?

    Allan

  • krille83krille83 Posts: 2Questions: 1Answers: 0

    In short, yes, i am expecting the hidden rows to appear. I will try to explain how it should work.

    Lets say that the table has 20 rows, with the first 10 showing and a button in the bottom that says "Show all (10 more)". Clicking the button just does a $(rows).show();

    Now lets say that the default sort is by Date, and user now wants to sort by Price, and the row with the lowest price happends to be the last row (oldest by Date).

    Even if the user hasn't clicked "Show all", and sorts by Price, the last row should be made visible and show at the top. Right now it only sorts among the 10 visible rows.

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Unhiding the desired rows is something you would control as Datatables does not track the HTML state of the rows. I'm not sure how events work in Datatables 1.9 but you would need to have an event that is triggered when sorting occurs so you can unhide the rows.

    Someone more familiar with 1.9 might be able to help with how to setup the event handler.

    Kevin

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Or maybe you can use the fnDrawCallback callback and determine if there are any rows you want to unhide on each table draw.

    Kevin

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I see - regardless of 1.9 (which is no longer supported btw) or 1.10, you should use rows.add() (or fnAddData in 1.9) to add more data. What you are looking to do is not supported by DataTables directly.

    Allan

This discussion has been closed.