简体   繁体   English

jquery DataTables行重新排序:订单在删除后恢复

[英]jquery DataTables row reordering: order reverts back after drop

I am trying to use jQuery DataTables ( http://datatables.net/ ) with the Row Ordering plugin ( http://code.google.com/p/jquery-datatables-row-reordering/wiki/Index ). 我正在尝试使用jQuery DataTables( http://datatables.net/ )和Row Ordering插件( http://code.google.com/p/jquery-datatables-row-reordering/wiki/Index )。 Originally, the re-ordering of rows looked like it worked, however there was a javascript error "Error: Syntax error, unrecognized expression: #". 最初,行的重新排序看起来像是有效的,但是有一个javascript错误“错误:语法错误,无法识别的表达式:#”。 So I implemented the solution outlined here: http://datatables.net/forums/discussion/19011/drag-and-drop-row-reordering-issue Giving the tr elements unique ids. 所以我实现了这里概述的解决方案: http//datatables.net/forums/discussion/19011/drag-and-drop-row-reordering-issue给tr元素赋予唯一ID。 Now there are no javascript errors. 现在没有javascript错误。 However, the row re-ordering doesn't work at all now. 但是,行重新排序现在根本不起作用。 I drag a row, but when I drop it, the table just reverts back to its previous state. 我拖了一行,但当我放下它时,表只是恢复到之前的状态。

Here's the full HTML file with javascript: http://pastebin.com/2P9hJ7n2 这是使用javascript的完整HTML文件: http//pastebin.com/2P9hJ7n2

Has anyone else encountered this problem? 还有其他人遇到过这个问题吗? If so, how have you solved it? 如果是这样,你是如何解决的?

I tried digging around in the row reordering javascript and it looks like the problem is grabbing the current and previous position of the row: 我尝试在重新排序javascript的行中挖掘,看起来问题是抓住行的当前和先前位置:

// fyi: properties.iIndexColumn is 0
var iCurrentPosition = oTable.fnGetData(tr, properties.iIndexColumn);
//...
oTable.fnGetData(trPrevious[0], properties.iIndexColumn);

Whatever it's expecting to get from fnGetData has changed. 无论它期望从fnGetData获得什么都改变了。 I've iterated over what oTable.fnGetData(tr, i) returns for a couple of values of i and it seems to be the cells of the row. 我已经迭代了oTable.fnGetData(tr,i)为i的几个值返回的内容,它似乎是行的单元格。

My guess is the implementation of DataTables has changed since this plugin was written. 我的猜测是,自从这个插件编写以来,DataTables的实现已经发生了变化。 I'm just wondering if it's possoble to fix this easily or not. 我只是想知道是否可以轻松解决这个问题。

To prevent revert back after drop use "update: false" attribute. 要防止在删除后恢复使用“update:false”属性。

var table = $('#categories').DataTable( {

    "ajax": "someurl",

    "rowReorder": {
        selector: 'tr',
        update: false,
    },

});

As you can read in the wiki-link you provided, 正如您可以在您提供的wiki链接中阅读,

  • Each TR element must have id attribute. 每个TR元素必须具有id属性。
  • One column in the table should be indexing column. 表中的一列应该是索引列。 This column will be used for determining position of the row in the table. 此列将用于确定表中行的位置。 By default this is first column in the table. 默认情况下,这是表中的第一列。 You can see structure of the table on the live example page. 您可以在实例示例页面上看到该表的结构。

The " unrecognized expression: # " was related to the first demand; 无法识别的表达:# ”与第一个需求有关; that you are not able to move the rows around is related to the second. 您无法移动行与第二个相关。 You simply lack an indexing column . 你只是缺少一个索引列 As you have figured out with the missing <tr> #id, you can easily create that column programmatically : 正如您已经找到缺少的<tr> #id,您可以通过编程方式轻松创建该列:

$("#mySuperTable thead tr").prepend('<th>#</td>');    
var count = $("#mySuperTable tbody tr").length-1;
$("#mySuperTable tbody tr").each(function(i, tr) {
    $(tr).attr('id', 'id'+i);
    $(tr).prepend('<td>'+parseInt(i+1)+'</td>');    
    if (i==count) {
       $("#mySuperTable").dataTable({
           //...initialization options
       }).rowReordering();
    }     
});  

Now RowReordering works with your table -> http://jsfiddle.net/gy8s3hoa/ 现在RowReordering与你的表一起工作 - > http://jsfiddle.net/gy8s3hoa/

Notice the demo above are running dataTables 1.10.x. 请注意,上面的演示正在运行dataTables 1.10.x. The issue has nothing to do with dataTables versions or some changes in dataTables internals, it is simply about how the RowReordering plugin is created. 该问题与dataTables版本或dataTables内部的一些更改无关,它只是关于如何创建RowReordering插件。 It is not very elegant, if you ask me. 如果你问我,它不是很优雅。 The plugin should create the id 's and the index column it is needing (and make it hidden) by itself. 插件应该自己创建它需要的id和它所需的索引列(并使其隐藏)。

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

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