我正在爲yii2中的學習編寫一個項目。我在查看微件時遇到了一個問題。在動態創建的文件上使用yii2 jui小部件
我有一個輸入描述發票的表單。其中一個輸入用yii2 \ jui中的DataPicker綁定,另一個輸入用yii2 \ jui中的AutoComplete綁定。基本上,代碼如下所示:
$contractors = app\models\Contractor::find()
->select('contractorId as id, contractorShortName as label, contractorShortName as value')
->asArray()
->all();
/.../
<tr>
<td>
<?= DatePicker::widget([
'name' => 'costBillDate[]',
'language' => 'pl',
'dateFormat' => 'yyyy-MM-dd'
]) ?>
</td>
<td><?= AutoComplete::widget(
'clientOptions' =>[
'source' => $contractors,
'autofill' => 'true',
'select' => new JsExpression("function(event, ui) {
$($(this).parent().find('input[name=\"contractorId[]\"]')).val(ui.item.id);
}")
],]) ?>
<?= Html::hiddenInput('contractorId[]', "", []) ?>
</td>
//other inputs
<td class="text-center delete"><div class="glyphicon glyphicon-trash"></div></td>
</tr>
上面的代碼很簡單,基本可行。因此,在表格下添加了一個按鈕,用於複製表格中的行以將更多文檔信息插入數據庫。併爲每一行添加一個腳本來刪除這一行。
//Dynamically creation of a row
$this->registerJs('$("#add-bill").on("click",function(){'
. '$("#costs-bills").append(\''
. '<tr>'
. Html::tag("td", DatePicker::widget([
'name' => 'costBillDate[]',
'language' => 'pl',
'dateFormat' => 'yyyy-MM-dd'
]))
. '<td>'
. AutoComplete::widget([
'clientOptions' =>[
'source' => $contractors,
'autofill' => 'true',
'select' => new JsExpression("function(event, ui) {
$($(this).parent().find('input[name=\"contractorId[]\"]')).val(ui.item.id);
}")
],])
. Html::hiddenInput('contractorId[]', "", [])
. '</td>'
. '<td class="text-center delete"><div class="glyphicon glyphicon-trash"></div></td>'
. '</tr>'
. '\');'
//binding function to created span
. '$(".delete").on("click",function(){
var $killrow = $(this).parent("tr");
$killrow.addClass("danger");
$killrow.fadeOut(2000, function(){
$(this).remove();
});})'
. '})
');
//Binding delete function to span for static row
$this->registerJs('$(".delete").on("click",function(){
var $killrow = $(this).parent("tr");
$killrow.addClass("danger");
$killrow.fadeOut(2000, function(){
$(this).remove();
});})
');
#add-bill
是按鈕的ID。
#costs-bills
是我在表格中的表格的ID。
以上代碼的影響: - 我可以添加行到表; - 我可以從表中刪除選定的行。
問題: 如果我將該行添加到具有動態創建的輸入的表中,該動態創建的行中的DataPicker和AutoComplete不起作用。它們的行爲與普通的textInputs相同。
我不知道該怎麼做。
嗨卡米爾。你能否更清楚你想要達到的目標?最好的問題是這些顯示足夠的但短代碼,明確說明你正在努力解決的問題以及困難在哪裏。 – gorn
如果您可以編輯您的原始問題,最好的辦法是。這樣,你有更好的機會有人回答它。 – gorn