duanpai9945 2014-01-25 13:44
浏览 13

Yii - GridView中的评级百分比

So, I finally have a progress bar in my GridView, thanks to the BootProgressColumn. However, I have a rating system, and I'd like to show the percentage inside the progress bar. I can set it the hard way in the columns array, but that'll be for all the rows.

$rawData = Item::model()->findAllByAttributes(array('special_id' => $specialId));
$dataProvider = new CArrayDataProvider($rawData, array());

The GridView:

'dataProvider' => $dataProvider,
'columns' => array(
 array(
  'class' => 'application.components.BootProgressColumn',
  'name' => 'Rating',
  'animated' => true,
  'striped' => true,
  'percent' => '44',
 ),
)

Now, how do I get the sum of all columns where each respective $data->id matches rows in the Rating model?

$countRatings = count(Rating::model()->findAllByAttributes(array('item_id' => $data->id));

But what to do for sum? And how to divide sum by count? That all having done, how to get it in each column? Or would it be best to just create a table by myself?

  • 写回答

1条回答 默认 最新

  • duansengcha9114 2014-01-26 09:02
    关注

    If I understand your question well, you want to get the average rates for each item, here is what you can do:

    in the Item model create the relation avgRating:

    public function relations()
    {
        return array(
            'avgRating' => array(self::STAT, 'Item', 'item_id', 'group' => 'item_id', 'select' => 'ROUND(AVG(rate),1)'),
        );
    }
    

    and then you can call this for each item to get the rating in your table:

    'dataProvider' => $dataProvider,
       'columns' => array(
        array(
          'class' => 'application.components.BootProgressColumn',
          'name' => 'Rating',
          'animated' => true,
          'striped' => true,
          'percent' => '$data->avgRating',
        ),
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。