在数据表表和普通 DOM 表之间移动(添加/删除)tr 行
Move (add/delete) tr rows between datatables table and plain DOM table
关于在两个数据表之间移动行,可以找到很多答案。(数据表在表之间移动行)。但是,在一个数据表和一个普通的 DOM 表之间执行此操作已被证明是相当困难的。
我有一个数据表:
var colcount = $("#uitschrdiv").children("thead > tr:first").children().length;
dtable = $("#uitschrdiv").dataTable({
"oLanguage": {
"sUrl": "/css/datatables/nl.txt"
},
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [colcount - 1] }
],
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "Alles"]],
"sPaginationType": "full_numbers",
"bStateSave": true,
"iCookieDuration": 60 * 30 /* 30min */
});
我有一个名为 #inschrdiv
的普通 DOM 表
它们中的每一个在最后一个td
都有一个按钮,用于将行移动到另一个表。如何在他们两个之间切换tr
?
在我将其中一个表切换到数据表之前,这是将移动 TR 的 jQuery
$(".uitschrbut").live("click", function () {
if ($(this).hasClass("inactivebtn")) {
//werkt niet
return false;
} else {
$(this).removeClass("uitschrbut").addClass("inschrbut").val("inschrijven");
$("#uitschrdiv").append($(this).parent().parent());
checkInEnUitschrMax();
}
});
$(".inschrbut").live("click", function () {
if ($(this).hasClass("inactivebtn")) {
//werkt niet
return false;
} else {
$(this).addClass("uitschrbut").removeClass("inschrbut").val("uitschrijven");
$("#inschrdiv").append($(this).parent().parent());
checkInEnUitschrMax();
}
});
这根本不适用于数据表。
这个解决方案花了我一段时间才明白,Datatables 只能在删除/添加时与 DOM 对象一起使用,并且不处理 jQuery 对象。
这就是我最终得到的:
$(".uitschrbut").live("click", function () {
if ($(this).hasClass("inactivebtn")) {
//werkt niet
return false;
} else {
$(this).removeClass("uitschrbut").addClass("inschrbut").val("inschrijven");
var jrow = $(this).parents("tr");
jrow.detach(); //you need to detach this because if the datatable has a filter applied in search, it will stay in this table until the filter fits the data in this row. It would only then be moved to the datatable.
dtable.fnAddTr(jrow[0]); //used plugin, see below, jrow[0] gets the DOM of jrow
checkInEnUitschrMax();
}
});
$(".inschrbut").live("click", function () {
if ($(this).hasClass("inactivebtn")) {
//werkt niet
return false;
} else {
$(this).addClass("uitschrbut").removeClass("inschrbut").val("uitschrijven");
var row = $(this).parent().parent()[0]; //the [0] gets the native DOM elem
$("#inschrdiv tbody").append(row); //append the DOM element to the other table
dtable.fnDeleteRow(row); //delete this row from the datatable
checkInEnUitschrMax();
}
});
您将需要插件fnAddTr
可以在这里找到:http://datatables.net/plug-ins/api#fnAddTr
如果你想一次添加多行,你可以使用这个插件,它允许你一次将整个 DOM 表添加到一个数据表中:http://datatables.net/forums/discussion/comment/31377#Comment_31377
相关文章:
- 根据Select值添加/删除表行
- 添加/删除类淡入淡出不起作用
- p5.js声音库:如何添加/删除p5.第5页中的短语().零件()
- 在django表单集中添加/删除表单的Javascript
- 添加/删除子元素
- 添加/删除/更改输入文本的部分值
- jQuery - 添加删除类 - 设置输入值
- 向具有唯一Id的表添加/删除行
- DateTimePicker:函数从minDate maxDate添加删除日期
- 由 JavaScript 控制的 CSS3 单向转换在按顺序添加/删除类时不起作用
- 添加/删除类在 Chrome 中对我不起作用
- JQuery 测试元素是可见的还是隐藏的,然后添加删除类
- Jquery切换和添加/删除
- 克隆字段上的“添加-删除”按钮
- (X-Editable)与(Select2标签)不工作,可以't填充当前标签并跟踪添加/删除的标签
- 添加/删除要与Jquery进行比较的产品
- 添加/删除类并使用Cookie记住所选内容
- 如何使用延迟添加/删除类
- 如何根据数组内容添加/删除数组中的元素
- 单击单选按钮,添加/删除表单元素的类