Yii框架 - CGridView排序相关列

Log*_*ner 2 php gridview frameworks yii jquery-ui-sortable

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

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

这就是我所拥有的:

关系名称/标签是"公司",在Employee模型中设置.

表:员工 - 列: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...
Run Code Online (Sandbox Code Playgroud)

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...
Run Code Online (Sandbox Code Playgroud)

小智 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   
); 
Run Code Online (Sandbox Code Playgroud)

查看文件:

$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',       
),
Run Code Online (Sandbox Code Playgroud)

));

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