您使用的currentIndex实际上是多输入容器所在的当前索引。
例如,如果您有5 输入并且您删除1,则当前索引将为4,它不是删除行的索引,因此这意味着currentIndex 不是实际索引该行的数量,但容器中的总行数,如果您期望如果您从5 行总数中删除第三行,它应该返回 3/2(取决于来自 0 或 @ 的起始索引987654330@) 那你就错了。
我无法通过获取元素/行的实际行 ID 来猜测您真正想要完成的任务,尽管您可以这样做,但在您这样做之前您需要了解一些东西。
将以下列表分别视为容器内的行和索引。
| IDX | Element | ID
| 1 | input | email-1
| 2 | input | email-2
| 3 | input | email-3
| 4 | input | email-4
| 5 | input | email-5
如果你删除最后一行,新的索引将和之前一样,逻辑上应该没问题。
| IDX | Element | ID
| 1 | input | email-1
| 2 | input | email-2
| 3 | input | email-3
| 4 | input | email-4
但是如果您删除第一行,并且您希望在删除第一行后其余索引将保留之前的索引,如下所示,
| IDX | Element | ID
| 2 | input | email-2
| 3 | input | email-3
| 4 | input | email-4
| 5 | input | email-5
不行,删除后索引会被重置,如果每次删除第一个元素,总是得到索引1。
因此,如果您仍想获取已删除行的行号,则应使用row 参数和.index() 函数row 参数将具有即将被删除的行的对象已删除。
注意:以下示例均使用基本单列,请相应调整您的脚本
$js = <<<JS
jQuery('#wtabularbotellas').on('beforeDeleteRow',function(e, row,currentIndex) {
//the index of the row removed
let removedIndex= row.index();
//id of the input inside
let inputId = row.find(':input').attr('id');
console.log("Index removed====>"+removedIndex, "Input id of the removed row input====>"+inputId);
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);