我有一个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);
});
我在控制台或警报中得到的只是一个空数组。任何想法 ?预先感谢。