简体   繁体   English

jQuery dataTable不对新数据进行排序

[英]jQuery dataTable isn't sorting new data

I am using the jQuery dataTable library, and am having an issue. 我正在使用jQuery dataTable库,并且遇到了问题。 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. 因此,fnDraw()并没有达到预期的效果。

//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. 我从DataTables网站上找到了一个实时示例,他们有一个直接处理此类问题的示例。 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 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. 除了@jacouh的重新初始化表的解决方案外,还尝试在DataTables选项中设置"bStateSave": true This should have DataTables set a cookie to remember a user's sorting preferences. 这应该使DataTables设置一个cookie来记住用户的排序首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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