Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform Filter using Multi Select Dropdown in Yii2 GridView

Tags:

php

yii2

dropdown

Here i like to explain my problem clearly,

am trying to perform multi select dropdown filter, before this multiselect filter i have a basic filter.

Am using kartik-v dropdown extension

search.php

<?php
     $status = ArrayHelper::map(Status::find()->all(),'id','status');
     echo $form->field($model, 'status')->widget(Select2::classname(), [
                            'data' => $status,
                            'language' => 'en',
                            'options' => [
                            'placeholder' => 'Select Status..',
                            'multiple' => true
                            ],
                            'pluginOptions' => [
                                'allowClear' => true
                            ],
                    ]);
?>

claimsSearch.php

$query->andFilterWhere([
            'status' => $this->status
        ]);

if i try the above code am getting error as below

Array to string conversion

but here i don't know how to write filter code.

update searchview: search view snapshot

like image 875
Nodemon Avatar asked Feb 15 '16 07:02

Nodemon


2 Answers

Try to delete 'status' from EmployeeSearch rules. You cannot filter such field automatic way. Or you must set up custom filter value for status column, like this (you can dig into this direction):

How can I use a simple Dropdown list in the search box of GridView::widget, Yii2? Try this link

like image 130
Kalai S Avatar answered Nov 18 '22 06:11

Kalai S


You are not calling the model in that widget. You shoudl use like this:

echo $form->field($mySearchModel, 'state_10')->widget(Select2::classname(), [
    'data' => $status,
    'options' => [
        'placeholder' => 'Select Status ...',
        'multiple' => true
    ],
]);

And your select it's probably returning an array. So, your search would be something like:

$query->andFilterWhere([
    'status' => ('in', 'status', $this->status)
]);

See more examples of queries here.

If that solution don't work, i will sugest you to do a var_dump($yourModel->status) in your view, just to check what is returning.

like image 37
Clyff Avatar answered Nov 18 '22 06:11

Clyff