Yii:反转 CGridView 显示顺序 [英] Yii: Reverse CGridView display order

查看:28
本文介绍了Yii:反转 CGridView 显示顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何反转 CGridView 的顺序(默认显示最新的)?

How can I reverse the order of CGridView (showing newest on top by default)?

我目前有 Gii 生成的代码:

I currently have the Gii generated code:

public function actionAdmin() {
    $model = new Post('search');
    $model->unsetAttributes();
    if (isset($_GET['Post']))
        $model->attributes = $_GET['Post'];

    $this->render('admin', array(
        'model' => $model,
    ));

请帮忙.谢谢.

推荐答案

如果您将顺序设置为查询的一部分,您将无法使用列排序器.您需要更新创建数据提供者的 sort 属性,在这种情况下,它可能在提到的 @xiaohan2012 函数中.它看起来像:

If you set the order as part of the query you won't be able to use the column sorters. You need to update the sort property where the data provider gets created, which in this case is probably in the function @xiaohan2012 mentioned. It would look something like:

    return new CActiveDataProvider('Post', array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'update_time DESC',
        ),
    ));

或者要创建完全自定义的排序,您需要创建一个新的排序对象,例如:

OR to create a totally custom sort, you would need to create a new sort object something like:

    $sort = new CSort();
    $sort->defaultOrder = 'update_time DESC';
    $sort->attributes = array(
        'post_name'=>array(
            'asc'=>'post_name asc',
            'desc'=>'post_name desc',
        ),
        'update_time'=>array(
            'asc'=>'update_time desc',
            'desc'=>'update_time asc',
        ),
        [... additional columns]
    );

在这种情况下,您的数据提供者将类似于:

in which case, your data provider would look something like:

    return new CActiveDataProvider('Post', array(
        'criteria'=>$criteria,
        'sort'=>$sort,
    ));

这篇关于Yii:反转 CGridView 显示顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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