【发布时间】:2020-10-30 12:54:57
【问题描述】:
我正在尝试使用 Pjax(Yii2 中的内置 Ajax 组件)在 TabularForm 中动态添加多个 Select2 小部件。由于某种原因,Select2 输入在视图顶部的错误位置呈现(请参阅下面的 gif1)。据我了解,这个问题与 Select2 小部件特别相关,因为如果我使用一些默认组件,例如,一切都可以正常工作。 Html::a(参见下面的 gif2)。
gif1:https://i.imgur.com/YMh5dNb.gif
gif2:https://i.imgur.com/sJkTDkO.gif
如何使用 Select2 小部件摆脱这种奇怪的行为?谢谢提前!
控制器:
class ProfileController extends Controller
{
// ...
public function actionCreate()
{
if (Yii::$app->request->isAjax) {
// some logic here ...
return $this->renderAjax('object/create', [
// ...
]);
}
}
// ...
}
查看:
// ...
use kartik\select2\Select2;
use kartik\select2\Select2Asset;
Select2Asset::register($this);
// a bunch of html code
<?php Pjax::begin(['id' => 'product-add']); ?>
$form1 = ActiveForm::begin();
$attribs = [
'name' => [
'type' => TabularForm::INPUT_RAW,
'value' => function($productModel) {
return Select2::widget([
'name' => 'state_10',
'data' => ['1' => '1', '2' => '2'],
'pjaxContainerId' => 'product-add',
'options' => [
'placeholder' => $productModel->tmpId,
'multiple' => true
],
]);
//return Html::a('product ' . $productModel->tmpId); <- works fine if I use this piece of code
},
],
// ...
Html::a("Add", ['profile/create'], ['class' => 'btn btn-primary'])
// ...
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
// ...
【问题讨论】:
标签: php ajax yii2 jquery-select2 pjax