dooo61733 2016-04-18 10:23
浏览 61
已采纳

如何根据数据值Yii CGridView为行着色

I have Users model.

Displaying it with CGridView i want to color the rows depending on $model->is_admin which is an integer either 0 or 1, so either of 2 colors.

Could it be done by simple settings of CGridView ?

Controller

/**
* Lists all models.
*/
public function actionIndex() {
    $model = new Users('search');
    $model->unsetAttributes();  // clear any default values
    if (isset($_GET['Users']))
        $model->attributes = $_GET['Users'];

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

View

<div class="row">
    <div class="col-sm-12 table-responsive">
        <?php
        $this->widget('zii.widgets.grid.CGridView', array(
            'id' => 'users-grid',
            'itemsCssClass' => 'table table-bordered table-hover dataTable',
            'dataProvider' => $model->search(),
            'enablePagination' => false,
            // 'filter'=>$model,
            'columns' => array(
                'username',
                'first_name',
                'last_name',
                'email',
                ),
                array(
                    'class' => 'CButtonColumn',
                ),
            ),
        ));
        ?>
    </div>
</div>
  • 写回答

1条回答 默认 最新

  • douwen5833 2016-04-27 07:25
    关注

    Here is a good looking example which may help you:

    http://www.yiiframework.com/wiki/314/cgridview-use-special-variable-data-in-the-htmloptions-of-a-column-i-e-evaluate-htmloptions-attribute/

    I had no luck implementing it by myself, using only build-in features, so it might be the only option. I did not try this one, but as comments say, it should work.

    EDIT: I made it, much shorter way

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $data_provider,
        'rowCssClassExpression' => '$data["is_admin"] ? "tr_isadmin" : ""',
        'columns' => array(
            array(
                'header' => 'Is admin?',
                'name' => 'is_admin',
            ),
        ),
    );
    

    So, rowCssClassExpression adds a class for tr wrapper based on $data["is_admin"] value.

    Then, simply add css like

    .tr_isadmin { 
        background-color: red; 
    }
    

    Of course, it is just simplified example. You have to adapt it for your purpose.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料