简体   繁体   English

基于CGridView中的列值动态更改行颜色

[英]Dynamically Change the Row Color Based on the Column value in CGridView

First, actually I'm not using CGridView, but I'm using TbExtendedGridView from YiiBooster . 首先,实际上我没有使用CGridView,而是使用YiiBoosterTbExtendedGridView I use CGridView on the title because it's more familiar, but still these two things works in quite the same way. 我在标题上使用了CGridView,因为它比较熟悉,但是这两件事仍然以完全相同的方式工作。

I'm enabling inline edit on my TbExtendedGridView (TbJEditableColumn). 我在TbExtendedGridView(TbJEditableColumn)上启用内联编辑。 TbExtendedGridView is using jquery's Jeditable for this functionality. TbExtendedGridView正在使用jquery的Jeditable来实现此功能。 This Jeditable also applicable to CGridView . Jeditable也适用于CGridView

Thanks to this SO's question: Change the Row Color Based on the Column value in CGridView 正是由于这样的问题: 基于CGridView中的Column值更改行颜色

I know how to change the row color. 我知道如何更改行颜色。 But, it's still not working with the inline edit functionality. 但是,它仍然不能与内联编辑功能一起使用。 So, my question is, how can that rowCssClassExpression be updated everytime I edit some value on a column? 所以,我的问题是,每当我在列上编辑一些值时,如何更新rowCssClassExpression

This is my view's code on [root]/protected/views/transaction/admin.php 这是我在[root]/protected/views/transaction/admin.php上的视图代码

<?
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
    'id'=>'transaction-grid',
    'rowCssClassExpression'=>'$data->getCssClass()',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'name'=>'date',
            'value'=> 'date("j M Y", strtotime($data->date))',
            'htmlOptions' => array('style'=>'width:52px')
        ),
        array(
            'name' => 'amount',
            'value' => 'number_format($data->amount, 0, ",", ".")',
            'htmlOptions' => array('class'=>'currency', 'style'=>'width:72px')
        ),
        array(
            'name' => 'category_name',
            'value'=>'$data->category->name',
            'header'=>'Category',
            'sortable' => 'true',
            'htmlOptions' => array('style'=>'width:131px'),
            'class'=>'bootstrap.widgets.TbJEditableColumn',
            'jEditableOptions' => array(
                'type' => 'optgroup',
                'loadurl' => Yii::app()->baseUrl . '/index.php/transaction/getCategory',
                'submitdata' => array('attribute'=>'category'),
                'cssclass' => 'form',
                'width' => '180px',
                'submit' => 'save changes'
            )
        ),
        array(
            'name'=>'desc',
            'value'=>'$data->desc',
            'htmlOptions' => array('class'=>'desccell'),
            'class'=>'bootstrap.widgets.TbJEditableColumn',
            'jEditableOptions' => array(
                'type' => 'text',
                // very important to get the attribute to update on the server!
                'submitdata' => array('attribute'=>'desc'),
                'cssclass' => 'form',
                'width' => '180px',
            )
        ),
        array(
            'htmlOptions' => array('nowrap'=>'nowrap'),
            'class'=>'bootstrap.widgets.TbButtonColumn',
        )
    )
)

And this is my getCssClass code on [root]/protected/models/Transaction.php : 这是我在[root]/protected/models/Transaction.php上的getCssClass代码:

public function getCssClass(){
        $categoryType = Category::model()->findByPk($this->categoryId)->getAttribute("type");
        $categoryName = Category::model()->findByPk($this->categoryId)->getAttribute("name");

        $class = "";

        if($categoryName == "Uncategorized Income"){
            $class = "darkgreen";
        }
        else if($categoryName == "Uncategorized Expense"){
            return "darkred";
        }
        else if($categoryType == "INCOME"){
            return "green ";
        }
        else if($categoryType == "EXPENSE" || $categoryType == "COST OF GOODS"){
            return "red ";
        }
        else if($categoryType == "WITHDRAW" || $categoryType == "DEPOSIT" ){
            return "blue ";
        }
        else{
            return "grey ";
        }

        return $class . " " . $categoryName . " " . $categoryType;
    }

使用'afterAjaxUpdate'属性在任何更新上触发javascript函数。

'afterAjaxUpdate' => ' function(){ //enterJScode }',

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

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