在 yii 中对 CListView 进行排序

编程入门 行业动态 更新时间:2024-10-13 12:20:15
本文介绍了在 yii 中对 CListView 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

请考虑:

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/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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 19:06:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中对   yii   CListView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!