Yii框架 - CGridView排序相关列

时间:2011-05-20 03:10:16

标签: php gridview frameworks jquery-ui-sortable yii

提前感谢任何可以提供帮助的人。我一直在寻找答案,但还没有找到答案。我遇到了“解决方案”,这些解决方案从1行开始没有用到重写整个班级。

我有“网格”来显示关系,并且能够使用搜索功能。我无法弄清楚的是排序功能。完成以下更改后,列标题将变为不可点击。

这就是我所拥有的:

关系名称/标签是“公司”,在员工模型中设置。

表:员工 - 列:idCompany &安培; 表:公司 - 专栏:companyNick

admin.php - 查看

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'employee-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
            array(
                    'name'=>'company',   
                    'value'=>'$data->company->companyNick',
            ),
            'lastName',
            'firstName',

ETC...

Employee.php - MODEL

public function search()
    {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            //Company Relation Search
            $criteria->compare('company.companyNick',$this->company,true);  
            $criteria->with='company'; 

            //stock
            $criteria->compare('idEmployee',$this->idEmployee,true);
            $criteria->compare('idAccount',$this->idAccount,true);

ETC...

4 个答案:

答案 0 :(得分:5)

我遇到了同样的问题,最终以这种方式解决了问题:

模型搜索方法:

$sort = new CSort();
$sort->attributes = array(
'assignedTo'=>array(
    'asc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) ASC',       
    'desc'=>'(SELECT surname from people 
            WHERE people.person_id = t.assigned_to) DESC',     
    ),
    '*', // add all of the other columns as sortable   
); 

查看文件:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tasks-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
    'task',
    array(
    'header'=>'Assigned To',
    'value'=> '$data->assignedTo->surname.", ".$data->assignedTo->forename',
        'name'=> 'assignedTo',
        'sortable'=>TRUE,
        ),
    'due_date',
    'status',       
),

));

这样我就从相关表中选择一个字段到order by子句然后按顺序排序,在表达式中创建表连接,在这种情况下它是 - people.person_id = t.assigned_to(其中t是yii)提供的表别名。这可能不是创建order by子句的最有效方法,但它有效!

答案 1 :(得分:2)

这似乎是[yii]的日常问题。将这些东西从搜索功能中删除,并将过滤器属性添加到CGridView列中,如下所示:

        array(
                'name'=>'company',   
                'value'=>'$data->company->companyNick',
                'filter' => CHtml::listData(Company::model()->findAll(),'id','nick'),
        ),

答案 2 :(得分:2)

在Yii网站Wiki页面上如何做到这一点也有很好的解释: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

答案 3 :(得分:1)

我喜欢Mike H的回答。我还想指出,您可以使用with()来执行关系查询,然后将select设置为false以防止实际加载相关模型,而不是输入原始SQL。您还可以通过传递带点名的字符串来使用视图中相关属性的attributeLabel,例如:


// Controller
$gridDataProvider = new CActiveDataProvider('EmrFormPatientTie', array(
    'criteria'=>array(
        'condition'=>'flag_locked=0',
        'with'=>array('patient'=>array(
            'select'=>false, // Perform relational query without loading related models
        )),
    ),
    'pagination'=>(array('pageSize'=>15)),
    'sort'=>array(
        'attributes'=>array(
            'patient.display_id'=>array(
                'asc'=>'patient.display_id',
                'desc'=>'patient.display_id DESC',
            ),
            '*', // Make all other columns sortable, too
        ),
    ),
));

// View
array(
    'name'=>'patient.display_id',
    'value'=>'$data->patient->display_id',
),