DataTable 使用 for 循环添加新行
DataTable add new rows with for loop
大家好
我对 DataTable 及其 API 关于添加新行有疑问。我正在使用带 jQuery 和 ES5 的 DataTable(是的,我需要这种组合)。
我有一个带有功能的按钮,我称之为 'addNewRow'(是的,它是
明显的)。如果我单击此按钮显示带有输入的模式,我可以在其中放置一些数据,如姓名、薪水等,并将其实现到我的数据表中,但是否有任何可能的方法使其更动态?因为每次当我想创建一个新列并向 HTML modals.
添加新输入时,我必须在 row.add() 的 js 文件中添加新行代码
这是一个例子:
$buttonWrapper.on('click', '.addButton', 函数 (){
$table = $('table').DataTable();
$table.row.add([
$modal.find('input').eq(0).val(),
$modal.find('input').eq(1).val(),
$modal.find('input').eq(2).val()
])。画();
});
我想要这样的循环:
$buttonWrapper.on('click', '.addButton', 函数 (){
$table = $('table').DataTable();
$theadTr = $('table thead tr');
for(var i = 0; i < $theadTr.length; i++){
$table.row.add([
$modal.find('input').eq(i).val()
])。画();
}
});
我知道每次 row.add() 都会添加它,因为它取决于长度。我也尝试在 row.add() 函数中实现作为一种方法,但它不起作用(可能在免费版本中不存在类似的东西)。我也试过 add.rows() 但它也不起作用。
感谢您的任何建议。
希望对您有所帮助。
$buttonWrapper.on('click', '.addButton', function (){
var newRow = [];
$table = $('table').DataTable();
$theadTr = $('table thead tr');
for(var i = 0; i < $theadTr.length; i++){
newRow.push($modal.find('input').eq(i).val());
}
$table.row.add(newRow).draw();
});
大家好
我对 DataTable 及其 API 关于添加新行有疑问。我正在使用带 jQuery 和 ES5 的 DataTable(是的,我需要这种组合)。
我有一个带有功能的按钮,我称之为 'addNewRow'(是的,它是 明显的)。如果我单击此按钮显示带有输入的模式,我可以在其中放置一些数据,如姓名、薪水等,并将其实现到我的数据表中,但是否有任何可能的方法使其更动态?因为每次当我想创建一个新列并向 HTML modals.
添加新输入时,我必须在 row.add() 的 js 文件中添加新行代码这是一个例子:
$buttonWrapper.on('click', '.addButton', 函数 (){ $table = $('table').DataTable(); $table.row.add([ $modal.find('input').eq(0).val(), $modal.find('input').eq(1).val(), $modal.find('input').eq(2).val() ])。画(); });
我想要这样的循环:
$buttonWrapper.on('click', '.addButton', 函数 (){ $table = $('table').DataTable(); $theadTr = $('table thead tr'); for(var i = 0; i < $theadTr.length; i++){ $table.row.add([ $modal.find('input').eq(i).val() ])。画(); } });
我知道每次 row.add() 都会添加它,因为它取决于长度。我也尝试在 row.add() 函数中实现作为一种方法,但它不起作用(可能在免费版本中不存在类似的东西)。我也试过 add.rows() 但它也不起作用。 感谢您的任何建议。
希望对您有所帮助。
$buttonWrapper.on('click', '.addButton', function (){
var newRow = [];
$table = $('table').DataTable();
$theadTr = $('table thead tr');
for(var i = 0; i < $theadTr.length; i++){
newRow.push($modal.find('input').eq(i).val());
}
$table.row.add(newRow).draw();
});