javascript - 从表行中删除行和重新排序索引
问题描述
我们有一个功能,我们可以通过下面的函数从表中删除行。
function setDeleteItemButtonBehavior() {
$(document).on('click', '.delete-boxItem', function (e) {
e.preventDefault();
//$(this).removeClass('.delete-boxItem');
//showLoadingDialog();
var type = $(this).data('type');
var oTable = type == "U" ? TableR : TableS;
if (oTable.fnGetData().length > 0) {
//$(this).closest("tr").remove();
var nRow = $(this).closest("tr")[0];
oTable.fnDeleteRow(nRow);
updateReceivedItemLabelWhenRemove();
controlDeleteItem(type, oTable);
var deleteIndex = parseInt($(nRow).find("input[name*='Index']").val());
var tagRemove = parseInt($(nRow).find("input[name*='Tag']").val());
updateTagItemOrder(type, deleteIndex, tagRemove, true);
}
//makeModalContentInvisible();
});
}
由于删除中发生的其他逻辑,如行和标签的重新排序,Internet Explorer 最终崩溃,因为脚本运行时间过长。有人对如何优化此功能有其他建议吗?以下是索引重新排序方法的示例:
function controlDeleteItem(type, oTable) {
var grid = oTable.fnGetNodes();
$.each(grid, function (index, value) {
$(value).find('input[name$=".Index"]').val(index);
$(value).find('input, div, span').each(function () {
if ($(this).attr('id')) {
var boxItems = $(this).attr('id').split("_")[0];
var field= $(this).attr('id').split("__")[1];
$(this).attr('id', boxItems + '_' + index + '__' + field);
}
if ($(this).attr('name')) {
var boxItems = $(this).attr('name').split("[")[0];
var field = $(this).attr('name').split("]")[1];
$(this).attr('name', boxItems + '[' + index + ']' + field);
}
if ($(this).data('valmsg-for')) {
var boxItems = $(this).data('valmsg-for').split("[")[0];
var field = $(this).data('valmsg-for').split("]")[1];
$(this).data('valmsg-for', boxItems + '[' + index + ']' + field);
}
});
$(value).find('button[id^="btnSetWarrantyLostReason_R"]').each(function () {
$(this).attr('id', 'btnSetWarrantyLostReason_R' + index);
});
});
}
function prepareDataTable() {
oTableR = $('table.data-table-r').dataTable({
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"bAutoWidth": false,
"aoColumns": [{ "sClass": "" }, { "sClass": "" }, { "sClass": "" }, { "sClass": "" }, { "sClass": "" },
{ "sClass": "align-text-center align-center" }, { "sClass": "" },
{ "sClass": "align-text-center align-center" }, { "sClass": "align-text-center" }, { "sClass": "align-text-center" }]
});
oTableS = $('table.data-table-s').dataTable({
"bPaginate": false,
"bLengthChange": false,
"bFilter": false,
"bSort": false,
"bInfo": false,
"bAutoWidth": false,
"aoColumns": [{ "sClass": "" }, { "sClass": "" }, { "sClass": "" }, { "sClass": "" }, { "sClass": "" },
{ "sClass": "align-text-center align-center" }, { "sClass": "" },
{ "sClass": "align-text-center align-center" }, { "sClass": "align-text-center" }, { "sClass": "align-text-center" }]
});
}
上面的函数是我的 DataTables 对象的组装。
解决方案
主要问题在于您的 id 构造。
如果你可以改变你的 id 结构。如果你不能,你应该考虑,因为它会为你节省一些神经元。(您不应该将 id 从一个项目转移到另一个项目......)。
以越来越流行的方式命名您的 id 有两个目的:
- 有一个有序/可比较的集合
- 在两个元素之间进行简单的距离计算
为了满足 1. 它实际上是微不足道的:juste make an id with a timestamp
id = Date.now()+'_'+(z++)%1000 //the z part in case you create several items at the same timestamp
显然最好使用 aguid
或 anObjectId
为了满足 2. juste 在你的 DOM 中使用一个数组
myIds = []
myIds.splice(myIds.indexOf(yourId), 1)//delete
myIds.push(yourId)//add
var d = (aId,bId)=> myIds.indexOf(bId) - myIds.indexOf(aId)
attr 名称和 valmsg-for 不应在语义上包含“索引”部分,但如果强制,您也可以这样做
推荐阅读
- c - 我们如何创建一个结构数组,它的最后一个字段是灵活数组?
- python - 使用 Python 的 pytest 框架进行测试时,Pycharm 生成奇怪的代码模板进行测试
- eclipse - 在缺少 testNG 时运行
- sql - 如何按大小对列进行排序
- oracle - 将 varchar2 表传递给过程
- r - 将裁剪的 pdf 图像从 R imagemagick 保存到新的 pdf
- docker - 从没有 xhost 但使用 xauth 的 docker 连接到 X11-server
- mysql - 如何在平面行中返回带有几个连接的多选 sql 查询
- c# - 如何使用asp.net mvc c#向用户发送电子邮件确认邮件
- verilog - Generate-if 语句无法计算为常数值