Yii2: Select2, How to set initValueText in Gridview or Tabularform?

 ̄綄美尐妖づ 提交于 2019-12-12 03:37:31

问题


I need to set initValueText for Select2, which is in loop, like gridview or Tabularform. but I don't know how to set right value for each.

<?= TabularForm::widget([
    'dataProvider' => $dataProvider,
    'form' => $form,
    'actionColumn' => false,
    'checkboxColumn' => false,
    'attributeDefaults' => [
        'type' => TabularForm::INPUT_RAW,
    ],
    'attributes' => [
        'test' => [
            'type' => Form::INPUT_WIDGET,
            'widgetClass' => Select2::className(),
            'options' => [
                'name' => 'test',
                'options' => [
                    'class' => 'test-to-select',
                ],
                'pluginOptions' => [
                    'allowClear' => true,
                    'minimumResultsForSearch' => 'Infinity',
                    'ajax' => [
                        'url' => Url::to(['/test/get-list']),
                        'dataType' => 'json',
                        'data' => new JsExpression('function(term,page) {
                            return {term : term.term};
                        }'),
                        'results' => new JsExpression('function(data,page) {
                            return {results:data.results};
                        }'),
                        'cache' => true
                    ]
                ],
                'initValueText' => 'Selected Text' /// how can I set this in gridview or Tabularform?
            ],

        ],
    ]
]) ?>

Of course this is not working,

'initValueText' => function($model){
    retur $model->textValue;
}

Any help would be appreciated.


回答1:


If for example attribute for city, so try this..

$cityDesc = empty($model->city) ? '' : City::findOne($model->city)->description;

'initValueText' => $cityDesc, // set the initial display text

For init value first assign value to $model attribute, if you should not assign so this attribute can take value.




回答2:


Into tabularform if you want dynamic initValueText, you can use options closure in this manner:

'test' => [
        'type' => Form::INPUT_WIDGET,
        'widgetClass' => Select2::className(),
        'options' => function($model, $key, $index, $widget) {
            $initValueText = empty($model['textValue']) ? '' : $model['textValue'];
            return [
                'name' => 'test',
                'options' => [
                    'class' => 'test-to-select',
                ],
                'initValueText' => $initValueText,
                'pluginOptions' => [
                    'allowClear' => true,
                    'minimumResultsForSearch' => 'Infinity',
                    'ajax' => [
                        'url' => Url::to(['/test/get-list']),
                        'dataType' => 'json',
                        'data' => new JsExpression('function(term,page) {
                            return {term : term.term};
                        }'),
                        'results' => new JsExpression('function(data,page) {
                            return {results:data.results};
                        }'),
                        'cache' => true
                    ]
                ],
            ];
        }
    ],



回答3:


Set the data parameter with the array including the option you want to show. For example for cities:

'options' => [
    'data' => \yii\helpers\ArrayHelper::map(\app\models\City::find()->orderBy('id')->asArray()->all(), 'id', 'name'),
]



回答4:


try to put inside the options instead ouside

'widgetClass' => Select2::className(),
'options' => [
    'initValueText' => 'Selected Text'



回答5:


Don't use isset it return error if more the one filter are use there.

      [
            'attribute' => 'ad_partner_id',
            'value' => function ($model, $key, $index, $widget) {
                return $model->partner->name;
            },
            'filter' => Select2::widget([
                'model' => $searchModel,
                'initValueText' => !empty($searchModel->ad_partner_id) ? $searchModel->partner->name : "",
                'attribute' => 'ad_partner_id',
                'options' => ['placeholder' => Yii::t('app', 'Search  Partner ...')],
                'pluginOptions' => ['allowClear' => true, 'autocomplete' => true,
                    'ajax' => ['url' => Url::base() . '/partner/get-partners',
                        'dataType' => 'json',
                        'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                    ],
                ],
            ]),
        ],


来源:https://stackoverflow.com/questions/31694113/yii2-select2-how-to-set-initvaluetext-in-gridview-or-tabularform

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!