在DataTables(jQuery)中重新排序后排序不起作用

时间:2015-09-21 15:21:34

标签: jquery datatables

我有一个带有jQuery DataTables的表,并希望允许手动行重新排序。因此我使用的是a jQuery plugin that enables row reordering in DataTables

问题是通过启用行重新排序,我无法再对表进行排序。 rowreordering-plugin的程序员明确表示这是插件的工作方式,请参阅此处的讨论:https://code.google.com/p/jquery-datatables-row-reordering/issues/detail?id=1

现在我想在手动重新排序之后启用排序,并在排序后同样重新排序。如果排序破坏了手动订单并将其“反转”,那就没问题了。

我想这是插件源代码中最相关的部分:

    return this.each(function () {

        var oTable = $(this).dataTable();

        var aaSortingFixed = (oTable.fnSettings().aaSortingFixed == null ? new Array() : oTable.fnSettings().aaSortingFixed);
        aaSortingFixed.push([properties.iIndexColumn, "asc"]);

        oTable.fnSettings().aaSortingFixed = aaSortingFixed;


        for (var i = 0; i < oTable.fnSettings().aoColumns.length; i++) {
            oTable.fnSettings().aoColumns[i].bSortable = false;
            /*for(var j=0; j<aaSortingFixed.length; j++)
            {
            if( i == aaSortingFixed[j][0] )
            oTable.fnSettings().aoColumns[i].bSortable = false;
            }*/
        }
        oTable.fnDraw();

Here is the complete source code

通过在f​​or循环中将aoColumns [i] .bSortable设置为'true',可以在表中启用排序,但如果单击列则没有任何影响。我正在寻找如何解决这个问题的想法。 issue discussion的最后一个条目说了一些有希望的东西,但它似乎对我不起作用。

我正在使用DataTables 1.9.0和jQuery 1.7.1,因为该插件不适用于Datatables 1.10或jQuery 2。

1 个答案:

答案 0 :(得分:5)

使用dataTables 1.10.x和新的“原生”RowReorder插件。将以下文件包含在项目中:

<script src="https://cdn.datatables.net/rowreorder/1.0.0/js/dataTables.rowReorder.min.js"></script>
<link href="https://cdn.datatables.net/rowreorder/1.0.0/css/rowReorder.dataTables.min.css" type="text/css" rel="stylesheet">
$('#example').DataTable( {
    rowReorder: true
});

这里的演示 - &gt;的 http://jsfiddle.net/0f9Ljfjr/