如何在具有两级 1-N 关系的 CGridView 列中排序和搜索? [英] How to sort and search in a CGridView column with a two-level 1-N relation?

查看:25
本文介绍了如何在具有两级 1-N 关系的 CGridView 列中排序和搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题直接跟在这个其他

我有这个表:组、用户、报告

I have this tables: Groups, Users, Reports

关系:第 1 组 -> N 个用户 ** 和 ** 用户 1 -> N 个报告

我已经有了这个关系在报表模型中:

'user' => array(self::BELONGS_TO, 'Users', 'userId'),

这在用户模型中

'group' => array(self::BELONGS_TO, 'Gruppi', 'groupId'),

在报告模型中我有这个:

In Report Model I have this:

$criteria=new CDbCriteria;
$criteria->with = array ('user');

在报告的管理视图中,我使用了CGridView.

In the admin view of report I'm using a CGridView.

我使用 user.group.name 来显示组名,但是如何搜索(通过 DropDown)和排序!?

I'm using user.group.name for showing group name, but how to search (by DropDown) and sort !?

推荐答案

在管理视图中,我使用的不是 user.group.name

In admin view, instead of user.group.name, i'm using

array (
        'name' => 'groupId',
        'value' => '$data->user->group->name',
        'filter' => Chtml::listData( Groups::model()->findAll() ,'id','name')
    ),

在报表模型中我添加了一个类字段

In Report model I added a class field

public $groupId;

在报告规则中

    array('groupId','safe','on'=>'search'),

在报告搜索中

....
$criteria->with = array ('user.group');
....
$criteria->compare('user.groupId',$this->groupId);
....
[in sort array]
'groupId'=>array(
            'asc' =>'group.name',
                'desc'=>'group.name DESC',
 ),

这样我就可以按组名搜索和排序了!

In this way I can search, and sort, by group name !

希望这篇文章对大家有用

I hope this post can be usefull to someon

这篇关于如何在具有两级 1-N 关系的 CGridView 列中排序和搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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