yii2-multiple-input 我无法获取当前行
yii2-multiple-input I can't get current row
我正在使用 yii2-multiple-input,在使用 javascript 事件 beforeDeleteRow
删除项目时一切正常,如下所示。
jQuery('#wtabularbotellas').on('beforeDeleteRow',function(e, row, currentIndex) {
// This throw the number of rows instead how do I get the current Index?
alert(currentIndex);
});
但我无法捕获行的 ID 号。我尝试使用 row
对象和 currentIndex
变量来做到这一点,但没有结果,因为后者仅 returns 行数而不是我正在寻找的索引。
您使用的 currentIndex
实际上是多输入容器所在的当前索引。
例如,如果您有 5
个输入并删除 1
,则当前索引将为 4
,它不是已删除行的索引,因此这意味着currentIndex
不是该行的实际索引,而是容器中的总行数,如果您希望从总 5
行中删除第 3 行,则它应该 return 3/2(取决于0
或1
的起始索引)那么你错了。
我无法猜测您通过获取 element/row 的实际行 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);
我正在使用 yii2-multiple-input,在使用 javascript 事件 beforeDeleteRow
删除项目时一切正常,如下所示。
jQuery('#wtabularbotellas').on('beforeDeleteRow',function(e, row, currentIndex) {
// This throw the number of rows instead how do I get the current Index?
alert(currentIndex);
});
但我无法捕获行的 ID 号。我尝试使用 row
对象和 currentIndex
变量来做到这一点,但没有结果,因为后者仅 returns 行数而不是我正在寻找的索引。
您使用的 currentIndex
实际上是多输入容器所在的当前索引。
例如,如果您有 5
个输入并删除 1
,则当前索引将为 4
,它不是已删除行的索引,因此这意味着currentIndex
不是该行的实际索引,而是容器中的总行数,如果您希望从总 5
行中删除第 3 行,则它应该 return 3/2(取决于0
或1
的起始索引)那么你错了。
我无法猜测您通过获取 element/row 的实际行 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);