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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀