簡體   English   中英

基於CGridView中的列值動態更改行顏色

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

首先,實際上我沒有使用CGridView,而是使用YiiBoosterTbExtendedGridView 我在標題上使用了CGridView,因為它比較熟悉,但是這兩件事仍然以完全相同的方式工作。

我在TbExtendedGridView(TbJEditableColumn)上啟用內聯編輯。 TbExtendedGridView正在使用jquery的Jeditable來實現此功能。 Jeditable也適用於CGridView

正是由於這樣的問題: 基於CGridView中的Column值更改行顏色

我知道如何更改行顏色。 但是,它仍然不能與內聯編輯功能一起使用。 所以,我的問題是,每當我在列上編輯一些值時,如何更新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',
        )
    )
)

這是我在[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