嗨我正在使用fnDeleteRow函数删除表中的行,但问题是它只删除了第一次正确的行,而不是开始删除下一行而不是我按下删除按钮的行.
这是我的代码
$(document).ready(function() {
$(document).delegate('.but', 'click', function() {
var id = this.id; //getting the ID of the pressed button
var name = this.name; //getting the name of the pressed button
/****AJAX Function*********/
$.post('/products/delete', {id:id}, function(data) {
if(data == 1){ //In case if data is deleted
var oTable = $('#table_id').dataTable(); // JQuery dataTable function to delete the row from the table
oTable.fnDeleteRow( parseInt(name, 10) );// JQuery dataTable function to delete the row from the table
// oTable.fnDraw(true);
}
else
alert(data);
});
});
});
Run Code Online (Sandbox Code Playgroud)
this.name 给出与按下的按钮关联的行的行号,因为我不知道从dataTable动态计算rowIndex的任何方法.
当我计算服务器端的行号并将该数字与按钮关联时,所以当我删除行数据表时,在客户端重新排列其rowIndex并且服务器不知道任何事情.
任何有助于解决此问题的帮助.
试试这个:
$(document).ready(function() {
$(document).delegate('.but', 'click', function() {
var id = this.id; //getting the ID of the pressed button
var row = $(this).closest("tr").get(0);
/****AJAX Function*********/
$.post('/products/delete', {id:id}, function(data) {
if(data == 1){ //In case if data is deleted
var oTable = $('#table_id').dataTable(); // JQuery dataTable function to delete the row from the table
oTable.fnDeleteRow(oTable.fnGetPosition(row));// JQuery dataTable function to delete the row from the table
// oTable.fnDraw(true);
}
else
alert(data);
});
});
});
Run Code Online (Sandbox Code Playgroud)