简体   繁体   English

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

[英]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 我正在使用kartik-v下拉延伸

search.php 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 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: 更新searchview: 搜索视图快照

Try to delete 'status' from EmployeeSearch rules. 尝试从EmployeeSearch规则中删除“状态”。 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? 如何在GridView :: widget,Yii2的搜索框中使用简单的下拉列表? Try this link 试试这个链接

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) ,只是为了检查返回的内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM