【问题标题】:Yii2: Select2, How to set initValueText in Gridview or Tabularform?Yii2:Select2,如何在 Gridview 或 Tabularform 中设置 initValueText?
【发布时间】:2015-07-29 07:37:33
【问题描述】:

我需要为 Select2 设置 initValueText,它在循环中,如 gridview 或 Tabularform。但我不知道如何为每个设置正确的值。

<?= 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?
            ],

        ],
    ]
]) ?>

这当然行不通,

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

任何帮助将不胜感激。

【问题讨论】:

    标签: yii2


    【解决方案1】:

    如果你想要动态的initValueText变成表格形式,你可以通过这种方式使用options闭包:

    '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
                        ]
                    ],
                ];
            }
        ],
    

    【讨论】:

      【解决方案2】:

      如果例如城市的属性,那么试试这个..

      $cityDesc = empty($model->city) ? '' : City::findOne($model->city)->description;
      
      'initValueText' => $cityDesc, // set the initial display text
      

      对于初始值,首先为 $model 属性赋值,如果你不应该赋值,那么这个属性可以取值。

      【讨论】:

        【解决方案3】:

        使用包含您要显示的选项的数组设置数据参数。例如对于城市:

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

        【讨论】:

          【解决方案4】:

          试着把选项放在外面

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

          【讨论】:

            【解决方案5】:

            不要使用 isset,如果使用多个过滤器会返回错误。

                  [
                        '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}; }'),
                                ],
                            ],
                        ]),
                    ],
            

            【讨论】:

              猜你喜欢
              • 2017-11-10
              • 2018-05-11
              • 1970-01-01
              • 2017-10-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多