kartik选择2作为yii2网格中的过滤器输入


kartikSelect2 as filter input in yii2grid

我在Yii2项目中遇到了另一个伪问题。我有一个标准的网格视图在我的视图中,定义如下:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'layout' => '{items}{summary}',
        'columns' => [
            [
                'class' => 'yii'grid'SerialColumn',
                'contentOptions' => [
                    'style' => 'vertical-align: middle;'
                ]
            ],
            [
                'attribute' => 'name',
            ],
            [
                'attribute' => 'type',
                'value' => function($model){
                    /* @var $model app'models'Object */
                    return $model->typeNames()[$model->type];
                },
                'filter' => Select2::widget([
                    'name' => 'ObjectSearch[type]',
                    'data' => Object::typeNames(),
                    'theme' => Select2::THEME_BOOTSTRAP,
                    'hideSearch' => true,
                    'options' => [
                        'placeholder' => 'Wybierz typ obiektu...',
                        'value' => isset($_GET['ObjectSearch[type]']) ? $_GET['ObjectSearch[type]'] : null
                    ]
                ]),
            ],
            [
                'attribute' => 'countryId',
                'value' => 'country.name',
                'filter' => Select2::widget([
                   'name' => 'ObjectSearch[countryId]',
                   'data' => Country::forWidgets(),
                   'theme' => Select2::THEME_BOOTSTRAP,
                   'options' => [
                       'placeholder' => 'Wybierz państwo...'
                   ]
                ]),
             ],
  //other columns
        ],
    ]); ?>

我定义了一个searchModel类:

class ObjectSearch extends Object
{
    public function rules()
    {
        return [
            [['type'], 'integer'],
            [['name', 'city', 'countryId'], 'string']
        ];
    }
    //some other functions
    public function search($params)
    {
        $query = Object::find()->where(['userId' => Yii::$app->user->id]);
        $query->joinWith('country');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        $dataProvider->sort->attributes['countryId'] = [
            'asc' => ['countries.name' => 'ASC'],
            'desc' => ['countries.name' => 'DESC']
        ];
        if (!$this->load($params) && $this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere(['like', 'objects.name', $this->name])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['=', 'objects.countryId', $this->countryId])
            ->andFilterWhere(['=', 'type', $this->type]);
        return $dataProvider;
    }
}

排序和搜索很好——我得到了正确的结果。那我的问题是什么?对于标准列,当我在textInput中键入一些文本时,该输入的值在搜索后会保留在其中。但当我在Select2小部件搜索中选择一些值时,搜索后所选的值消失了,我只有一个占位符。

感谢您的帮助,
Kamil

您应该简单地使用initValueText param:

initValueText:string,在Select2小部件中显示的初始值的文本。

例如:

Select2::widget([
    'name' => 'ObjectSearch[type]',
    'data' => Object::typeNames(),
    'initValueText' => $searchModel->type,
    // ... other params
])

您也可以将其用作InputWidget:

Select2::widget([
    'model' => $searchModel,
    'attribute' => 'type',
    'data' => Object::typeNames(),
    // ... other params
])