提前感谢任何可以提供帮助的人。我一直在寻找答案,但还没有找到。我遇到过从 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 - 模型
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...
最佳答案
我也遇到过同样的问题,最后是这样解决的:
模型搜索方法:
$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 子句的最有效方法,但它确实有效!
关于php - Yii Framework - CGridView 排序相关列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067063/