简体   繁体   中英

jQuery dataTable isn't sorting new data

I am using the jQuery dataTable library, and am having an issue. I have some event handlers that allow for table cell values to change. After such a change occurs, I then click on the column header for the column with the newly changed data. It resorts all the rows, but the row with the new data is still sorted according to it's original value when I clicked on that column the first time. Below are the two functions I am using.

So, fnDraw() is not having the desired effect.

//called once, after page load completes
function drawTable()
{
    $('#mytable').dataTable({"bFilter": false,
                                       "bSort": true,
                                       "bInfo": false,
                                       "bPaginate": false,
                                       "bDestroy": true});
}

//called each time a table cell value is changed
function initTable()
{ 
    var oTable = $('#mytable').dataTable();
    oTable.fnDraw();
    //fnDraw is not having the desired effect
}

I found a live example from the DataTables website, they have an example that directly deals with this sort of problem. Plus, they show how to do live sorting for select lists and inputs with both text and numbers.

http://www.datatables.net/examples/plug-ins/dom_sort.html

If there is a better answer, I would be glad to take a look. Currently, I feel their example is the way to go.

Have you tested also:

//called each time a table cell value is changed
function initTable()
{ 
    var oTable =  $('#mytable').dataTable({"bFilter": false,
                                   "bSort": true,
                                   "bInfo": false,
                                   "bPaginate": false,
                                   "bDestroy": true});
    oTable.fnDraw();
    //fnDraw is not having the desired effect
}

?

In addition to @jacouh's solution of re-initializing the table, try setting "bStateSave": true in the DataTables options as well. This should have DataTables set a cookie to remember a user's sorting preferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM