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

时间:2013-02-22 08:06:23

标签: php ajax yii yii-extensions cgridview

首先,实际上我没有使用CGridView,但我正在使用TbExtendedGridView中的YiiBooster。我在标题上使用了CGridView,因为它更为熟悉,但这两件事仍然以完全相同的方式运作。

我在我的TbExtendedGridView(TbJEditableColumn)上启用内联编辑。 TbExtendedGridView正在使用jquery的Jeditable来实现此功能。这Jeditable also applicable to CGridView

感谢这个SO的问题:Change the Row Color Based on the Column value in CGridView

我知道如何更改行颜色。但是,它仍然没有使用内联编辑功能。所以,我的问题是,每当我在列上编辑某些值时,如何更新rowCssClassExpression

这是[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',
        )
    )
)

这是getCssClass上的[root]/protected/models/Transaction.php代码:

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;
    }

1 个答案:

答案 0 :(得分:0)

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

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