我有一个DataTable,我在其中存储来自产品形式的数据(每次我提交表单时,产品数据都会添加为数据表中的新行) . 我设法将每个表单数据推送到数组中并将其添加到数据表中,最后我得到整个数组然后发送到另一个控制器,以便持久保存数据 .
问题是我需要动态更改此数组,以便每次用户删除一行时(由于每行上的本地删除按钮),前一个数组将被新数组替换 .
我认为最好的方法是每次删除行时获取整个数据表的数据,并替换上一个表 .
// This is my array of products that should be updated
var products = [];
// my function when submitting the form
form2.on('submit', function (event) {
// this is the array getting the form data for each product (= row)
var product = [];
event.preventDefault();
// Here I fill the datatable with the form data after submit
product.push(autocomplete, price.val(), qty.val(), duration.val());
products.push(produit);
// I add the row to the datatable
tab_presta.row.add( [/* form data + delete button */] );
});
// and here is what happens when I click on a delete button
tab_presta.on("click", "[id^=btn_supp]", function() {
tab_presta.row($(this).closest("tr")).remove().draw();
// Here I try to get the data from the datatable
alert(tab_presta.rows().data());
// and I commented this row below where I push the data to an array
// tab_presta.rows().data().toArray(productDatatable);
console.log(productDatatable);
});
我进入控制台或警报的所有内容都是一个空数组 . 任何的想法 ?提前致谢 .