在 yii 中对 CListView 进行排序 [英] Sorting CListView in yii

查看:17
本文介绍了在 yii 中对 CListView 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑:

class User extends CActiveRecord
{
    ...
    public function relations()
    {
        return array(
            ...    
            'articleCount' => array(self::STAT, 'Article', 'userid'),
            ...    
            );
    }
    ...
}

现在,我需要创建一个 $dataProvider = new CActiveDataProvider(...) 来提供一个 CListView 小部件,我想向其中添加 articleCount 到属性 sortableAttributes 以便我可以根据用户是作者的文章数量对用户记录进行排序.

Now, I need to create a $dataProvider = new CActiveDataProvider(...) to feed a CListView widget to which I want add articleCount to the property sortableAttributes so that I can sort User records according to the number of articles the user is the author for.

最方便的方法是什么?其他选择是什么?

What are the most convenient method? what are the other alternatives?

推荐答案

好吧,我花了一段时间,但我终于弄明白了.;)

Well, it took me a while, but I finally got it figured out. ;)

首先,确保您仍然具有您在问题中提到的相同 STAT 关系.

First, make sure you still have the same STAT relation you mention in your question.

然后,在您为 CActiveDataProvider 构建 CDbCriteria 的 search() 方法中,执行如下操作:

Then, in your search() method where you are building your CDbCriteria for the CActiveDataProvider, do something like this:

public function search() {
  $criteria=new CDbCriteria;
  $criteria->select = 't.*, IFNULL( count(article.id), 0) as articleCount';
  $criteria->join = 'LEFT JOIN article ON article.userid = t.id';
  $criteria->group = 't.id';
  // other $criteria->compare conditions

  $sort = new CSort();
  $sort->attributes = array(
    'articleCount'=>array(
      'asc'=>'articleCountASC',
      'desc'=>'articleCountDESC',
    ),
    '*', // add all of the other columns as sortable
  );

  return new CActiveDataProvider(get_class($this), array(
    'criteria'=>$criteria,
    'sort'=>$sort,
    'pagination'=> array(
      'pageSize'=>20,
    ),
  ));
}

然后,在您输出 CGridView 的视图中,这样做:

Then, in your View where you output your CGridView, do this like so:

<?php $this->widget('zii.widgets.grid.CGridView', array(
  'dataProvider'=>$model->search(),
  'columns'=>array(
    'articleCount',
    // other columns here
  )
)); ?>

这有效,我测试了它(虽然我没有使用完全相同的名称,如文章",但基本思想应该有效:)我确实添加了一些额外的功能,因此效果更好(如 IFNULL 魔法)但是大部分功劳归功于此 Yii 论坛帖子:
http://www.yiiframework.com/forum/index.php?/topic/7061-csort-and-selfstat-to-sort-by-count/

This works, I tested it (although I did not use the exact same names, like 'article', but the basic idea should work :) I did add some bonus features so it works better (like the IFNULL magic) but most of the credit goes to this Yii forum post:
http://www.yiiframework.com/forum/index.php?/topic/7061-csort-and-selfstat-to-sort-by-count/

我希望这会有所帮助!不过,他们应该为此添加更好的支持,因此您无需编写这些棘手的 SELECT 语句.似乎应该正常工作",我会在 Yii 错误跟踪器.

I hope this helps! They should add better support for this though, so you don't need to make these tricky SELECT statements. Seems like something that should 'just work', I would file an 'enhancement' request in the Yii bug tracker.

这篇关于在 yii 中对 CListView 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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