我在每行中有编辑按钮,在点击行中的数据传递给模态,用户可以在其中更改,然后在保存在modal中它应该更新该行。
我正在使用 fnUpdate ,但不是更新(替换)目标行,而是替换了不同的行......
var uId = $(this).closest('.modal-content').find('input[name=uId]').val();
var fNameUpdated = $(this).closest('.modal-content').find('input[name=fName]').val();
var lNameUpdated = $(this).closest('.modal-content').find('input[name=lName]').val();
var uTypeUpdated = $(this).closest('.modal-content').find('.dropdown-toggle').attr('title');
var uNameUpdated = $(this).closest('.modal-content').find('input[name=uName]').val();
var uPasswordUpdated = $(this).closest('.modal-content').find('input[name=password]').val();
var buttons = 'buttons';
// updating row using fnUpdate from datatable
var oTable = $('#dataTables-example').dataTable();
oTable.fnUpdate( [uId, fNameUpdated, lNameUpdated, uTypeUpdated, uNameUpdated, uPasswordUpdated, buttons], trIndex );
先前已收到trIndex :
var trIndex;
$('#dataTables-example').dataTable().find('tbody').on('click', 'tr', function () {
trIndex = this.rowIndex;
});
编辑第1行之前的表:
+-----+------------+-----------+---------+----------+------+---------------+
| ID | First Name | Last Name | Type | Username | Pass | Action |
+-----+------------+-----------+---------+----------+------+---------------+
| 1 | Mark | Knopfler | admin | Mark | pass | Edit / Delete |
| 2 | Peter | Pan | admin | Peter | pass | Edit / Delete |
| 3 | Bob | Dylan | regUser | Bob | pass | Edit / Delete |
| 4 | Harry | Angel | regUser | Harry | pass | Edit / Delete |
| ... | ... | ... | ... | ... | ... | ... |
+-----+------------+-----------+---------+----------+------+---------------+
编辑第1行后的表:
+-----+------------+-----------+---------+----------+------+---------------+
| ID | First Name | Last Name | Type | Username | Pass | Action |
+-----+------------+-----------+---------+----------+------+---------------+
| 1 | Mark | Knopfler | admin | Mark | pass | Edit / Delete |
| 1 | Harrison | Ford | regUser | Harrison | pass | Edit / Delete |
| 3 | Bob | Dylan | regUser | Bob | pass | Edit / Delete |
| 4 | Harry | Angel | regUser | Harry | pass | Edit / Delete |
| ... | ... | ... | ... | ... | ... | ... |
+-----+------------+-----------+---------+----------+------+---------------+
有人可以提出建议吗。
编辑:
1)可能有一种更有效的数据收集方式
2)字段 Pass 仅用于测试目的
答案 0 :(得分:2)
找到解决方案:
显然接收 trIndex 的方法是错误的,应该更正以下内容:
$('#dataTables-example tbody').on('click', 'td', function () {
var tr = $(this).closest('tr');
rowIndex = tr.index();
});
以上似乎工作正常。