Sorting dynamic tables sorts the old value

Sorting dynamic tables sorts the old value

xabi87xabi87 Posts: 3Questions: 0Answers: 0
edited August 2009 in General
Hi,

I'm a massive fan of datatables, but I'm got one problem which is causing me to make unneccessary page refreshes.
My cells when clicked turn into select boxes, when the value is changed the database is updated in the background through AJAX, so no page refresh, but then when I click the column header to sort the column, although the cell is displaying the new value, the sort is being performed on the old value.

For example, I've got a list of priorities:
1, High,
2, High,
3, Medium
4, Low
5, Low

If I then update row 4, the display in the cell becomes "High", but when I sort by clicking on the heading the column looks like this:

1, High,
2, High,
3, Medium
4, High
5, Low

The sort is being performed on the first instance of the table. At the moment I'm resorting to a page refresh after the AJAX update, which defies the point really, if any one can help me out with this I would be very grateful!

Thanks

Replies

  • allanallan Posts: 61,773Questions: 1Answers: 10,112 Site admin
    Hi xabi87,

    What is happening is that while you are updating the DOM, you aren't updating the internal cache that DataTables has of your data (it would be horribly slow to read the data from the DOM on every sort). So what you can do is make use of fnUpdate ( http://datatables.net/api#fnUpdate ) which will update the DOM and the internal information for your change.

    These threads are relevant:
    http://datatables.net/forums/comments.php?DiscussionID=123
    http://datatables.net/forums/comments.php?DiscussionID=114

    Regards,
    Allan
  • xabi87xabi87 Posts: 3Questions: 0Answers: 0
    Thanks very much for the prompt reply Allan, I'll give this a go.

    Xabi
  • xabi87xabi87 Posts: 3Questions: 0Answers: 0
    Worked like a charm, thanks again Allan!
This discussion has been closed.