Nodemon
Nodemon

Reputation: 1046

How to perform Filter using Multi Select Dropdown in Yii2 GridView

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

Upvotes: 16

Views: 6915

Answers (3)

Kalaiselvan Mahendiran
Kalaiselvan Mahendiran

Reputation: 996

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

Upvotes: 4

t10508hn
t10508hn

Reputation: 805

$this->status is array?

So, you can use

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

Upvotes: 0

Clyff
Clyff

Reputation: 4076

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.

Upvotes: 1

Related Questions