php - Yii Framework - CGridView 排序相关列

标签 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 - 模型

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/

相关文章:

php - MYSQL/PHP 选择相同的值

android - 带有节标题的 GridView

asp.net - 在 GridView 中设置下拉列表的选定值

mysql - 编写用于测试 SQL 存储过程的单元测试框架

java - Tomcat/Web 服务器与 Java NIO/网络框架

来自字符串的 PHP 变量值

php - 使用 PHP 将 JSON 保存到 mySQL

php - 为每个用户 ID 在 mysql 中获取最新的日期时间

html - Gridview 标题格式

css - 你知道像 Clever CSS 这样的 css 框架吗?