如何使用Yii2 GridView中的多选下拉列表执行过滤

时间:2020-12-25 18:27:23

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

我正在使用kartik-v下拉延伸

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: 如何使用Yii2 GridView中的多选下拉列表执行过滤

3 个解决方案

#1


4  

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):

尝试从EmployeeSearch规则中删除“状态”。你不能过滤这种字段的自动方式。或者你必须为状态列设置自定义过滤器值,就像这样(你可以深入研究这个方向):

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

如何在GridView :: widget,Yii2的搜索框中使用简单的下拉列表?试试这个链接

#2


0  

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.

如果该解决方案不起作用,我将在您的视图中提示您执行var_dump($ yourModel-> status),以检查返回的内容。

#3


0  

$this->status is array?

$ this-> status是数组?

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

#1


4  

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):

尝试从EmployeeSearch规则中删除“状态”。你不能过滤这种字段的自动方式。或者你必须为状态列设置自定义过滤器值,就像这样(你可以深入研究这个方向):

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

如何在GridView :: widget,Yii2的搜索框中使用简单的下拉列表?试试这个链接

#2


0  

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.

如果该解决方案不起作用,我将在您的视图中提示您执行var_dump($ yourModel-> status),以检查返回的内容。

#3


0  

$this->status is array?

$ this-> status是数组?

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