Kamil Cierczek
Kamil Cierczek

Reputation: 85

kartik\Select2 as filter input in yii2\grid

I've encountered another dummy problem with my Yii2 project. I've got a standard gridView in my view, defined liek this:

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

I've defined a searchModel class:

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;
    }
}

Sorting and searching works fine - I've got correct results. So what's my problem? For standard columns when I type some text in textInput the value of this input stays in it after search. But when I choose some value in Select2 widget search work, but after search the selected value disappear and I've got just a placeholder.

Thanks for your help,
Kamil

Upvotes: 1

Views: 7571

Answers (1)

soju
soju

Reputation: 25312

You should simply use initValueText param :

initValueText: string, the text to displayed in Select2 widget for the initial value.

e.g. :

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

You could also use it as an InputWidget :

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

Upvotes: 4

Related Questions