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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题