简体   繁体   English

如何在CGridView Yii中对模型方法应用排序

[英]how to apply sorting on model method in CGridView Yii

I have User mode l which contain a function that calculates the average revenue per user. 我有用户模式 l,其中包含一个计算每个用户的平均收入的函数。 Now i want apply sorting in CGridView on the column which is associated with getAvgRevenue() function. 现在,我想在与getAvgRevenue()函数关联的列上的CGridView中应用排序。 While license is relation in the User model. 而许可是用户模型中的关系。

Here is my code, 这是我的代码,

 public class User{
     $user_id;
     $email;
     public function getAvgRevenue(){
        $count = 0;
        $revenue = 0;
        foreach ($this->license as $license){

            $revenue += $license->price;
            $count++;                
        }

        if($count!= 0){
            $averageRevenue = $revenue/$count;
            return $averageRevenue;
        }
        else{
            return null;
        }
      }

}

In Controller 在控制器中

  $modelUser = new CActiveDataProvider('User', array( 
            'sort' => array(
                    'attributes' => array(
                            'user_id',
                            'email',
                            'averagerevenue'
                    ),
                    'defaultOrder' => array(
                            'user_id' => CSort::SORT_ASC,
                    ),
            ),
     ));

In view 视野中

  <?php $this->widget('zii.widgets.grid.CGridView', array(
     'id' => 'user-grid',
    'dataProvider' => $modelUser,
     'columns' => array(
            array(
                    'header' => 'User ID',
                    'name' => 'user_id',
            ),
            array(
                    'header' => 'Email',
                    'name' => 'email',
            ),
            array(
                'header'=>'Average Revenue',
                'value'=>'$data->averagerevenue',
            ),
        )
    ));

?> ?>

Sorting is applicable on user_id and email but Average Revenue column is not sortable. 排序适用于user_id电子邮件,但“ 平均收入”列不可排序。 how to specify model method in sort() of CActiveDataprovider Please help me to solve the problem. 如何在CActiveDataprovider的sort()中指定模型方法,请帮助我解决问题。

Try this: 尝试这个:

$modelUser = new CActiveDataProvider('User', array( 
        'sort' => array(
                'attributes' => array(
                        'user_id',
                        'email',
                        'avgRevenue' //here's the change for you
                ),
                'defaultOrder' => array(
                        'user_id' => CSort::SORT_ASC,
                ),
        ),
 ));

And your gridview column should be: 并且您的gridview列应为:

array(
            'header'=>'Average Revenue',
            'value'=>'avgRevenue',
        ),

and you can read more info on it over here: 您可以在此处阅读更多信息:

http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/ http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM