PHP,Yii2 GridView对关系值的过滤 [英] PHP, Yii2 GridView filtering on relational value

查看:130
本文介绍了PHP,Yii2 GridView对关系值的过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继此之后:

Yii2 SearchModel在SearchModel中如何工作?

我希望能够过滤关系数据的GridView列.这就是我的意思:

I would like to be able to filter a GridView column of relational data. This is what I mean:

我有两个表,TableATableB.两者都有使用Gii生成的相应模型. TableA对于TableB中的值具有外键,如下所示:

I have two tables, TableA and TableB. Both have corresponding models generated using Gii. TableA has a foreign key to a value in TableB, like this:

TableA
attrA1, attrA2, attrA3, TableB.attrB1

TableB
attrB1, attrB2, attrB3

attrA1和attrB1是它们相应表的主键.

attrA1 and attrB1 are the primary keys of their corresponding tables.

现在,我的Yii2 GridViewattrA2attrA3attrB2.我在attrA2attrA3上有一个有效的过滤器,以便可以搜索列值.对于这两列,我也有一种有效的排序方式-只需单击列标题即可.我也希望能够在attrB2上添加此过滤和排序.

Now, I have a Yii2 GridView of attrA2, attrA3 and attrB2. I have a working filter on attrA2 and attrA3 so that I can search on column values. I also have a working sort for these two columns too - by just clicking on the column header. I would like to be able to add this filtering and sorting on attrB2 too.

我的TableASearch模型如下:

public function search($params){
    $query = TableA::find();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if (!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }

    $this->addCondition($query, 'attrA2');
    $this->addCondition($query, 'attrA2', true);
    $this->addCondition($query, 'attrA3');
    $this->addCondition($query, 'attrA3', true);

    return $dataProvider;
}

在我的TableA模型中,我这样设置相关值

In my TableA model, I set the related value like this

    public $relationalValue;

public function afterFind(){
    $b = TableB::find(['attrB1' => $this->attrB1]);
    $this->relationalValue = $b->relationalValue;
}

尽管这可能不是执行此操作的最佳方法.我想我必须在搜索功能中的某处使用$ relationalValue,但不确定如何使用.同样,我也希望能够按此列进行排序-就像我可以通过单击标题链接对attrA2AttrA3进行排序一样.任何帮助,将不胜感激.谢谢.

Although it is probably not the best way of doing this. I think I have to use $relationalValue somewhere in my search function but I'm not sure how. Similarly, I would like to be able to sort by this column too - just like I can for attrA2 and AttrA3 by clicking on the header link`. Any help would be appreciated. Thanks.

推荐答案

在Yii 2.0中,按列过滤gridview是非常容易的.请将filter属性添加到具有查找值的gridview列中,如下所示:

Filtering a gridview by a column is damn easy in Yii 2.0. Please add the filter attribute to a gridview column having lookup values, as under:

[
        "class" => yii\grid\DataColumn::className(),
        "attribute" => "status_id",
        'filter' => ArrayHelper::map(Status::find()->orderBy('name')->asArray()->all(), 'id', 'name'),
        "value" => function($model){
            if ($rel = $model->getStatus()->one()) {
                return yii\helpers\Html::a($rel->name,["crud/status/view", 'id' => $rel->id,],["data-pjax"=>0]);
            } else {
                return '';
            }
        },
        "format" => "raw",
], 

这篇关于PHP,Yii2 GridView对关系值的过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆