过滤后,yii2 gridview row css不会应用

时间:2015-04-22 16:17:36

标签: gridview yii2

我的gridview上有行css。使用过滤后,它不会被应用。以下是包含GridView

的视图
<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel app\models\SurveysSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Surveys';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="surveys-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a('Create Surveys', ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <p>
        PUBLISHED - Available on website<br/>
        NEW - Just created ready for publishing. Not visible on website.<br/>
        <span class="red">DELETED</span> - Flagged as deleted. Not visible on website.
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'rowOptions' => function ($model, $index, $widget, $grid){
            if($model->published0->description == "DELETED")
                return ['class'=>'red'];            
        },
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            //'survey_id',
            [
                'attribute' => 'name',
                'contentOptions'=>['style'=>'width:250px' ]
            ],
            'description:ntext',
            //'guid',
            [
                'attribute' =>'published_name',
                //'value'=>function ($data) { return $data->published0->description;} ,
                'contentOptions'=>['style'=>'width:100px' ]
            ],
            [
                'attribute' => 'survey_type',
                'contentOptions'=>['style'=>'width:50px' ]
            ],
            [ 
                'attribute' => 'region_name' ,
                //'value'=>function ($data) { return $data->region->name;} ,
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

</div>

我的模特课

<?php

namespace app\models;

use Yii;
use sammaye\audittrail\LoggableBehavior;
use sammaye\audittrail\AuditTrail;

/**
 * This is the model class for table "surveys".
 *
 * @property integer $survey_id
 * @property string $name
 * @property string $description
 * @property string $guid
 * @property integer $published
 * @property string $date_added
 * @property string $year_acquired
 * @property string $year_processed
 * @property string $survey_type
 * @property string $total_length
 * @property integer $region_id
 * @property string $processing_type
 * @property string $center_point
 * @property integer $shrink
 * @property integer $country_id
 *
 * @property CasestudySurvey[] $casestudySurveys
 * @property FactsheetViews[] $factsheetViews
 * @property Factsheets[] $factsheets
 * @property FactsheetsSeismic[] $factsheetsSeismics
 * @property Lines[] $lines
 * @property SurveyViews[] $surveyViews
 * @property Countries $country
 * @property Published $published0
 * @property Regions $region
 */
class Surveys extends \yii\db\ActiveRecord
{   
     const DELETED = 0;
     public $region_name;
     public $published_name;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'surveys';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'survey_type','country_id'], 'required', 
                'isEmpty' => function ($value) {
                    return empty($value);
                }
            ],
            [['description'], 'string'],
            [['published', 'region_id', 'shrink', 'country_id'], 'integer'],
            [['date_added'], 'safe'],
            [['total_length'], 'number'],
            [['name', 'guid'], 'string', 'max' => 255],
            [['year_acquired', 'year_processed'], 'string', 'max' => 14],
            [['survey_type', 'processing_type'], 'string', 'max' => 45],
            [['center_point'], 'string', 'max' => 100]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'survey_id' => 'Survey ID',
            'name' => 'Name',
            'description' => 'Description',
            'guid' => 'Guid',
            'published' => 'Published',
            'date_added' => 'Date Added',
            'year_acquired' => 'Year Acquired',
            'year_processed' => 'Year Processed',
            'survey_type' => 'Type',
            'total_length' => 'Total Length',
            'region_id' => 'Region',
            'processing_type' => 'Processing Type',
            'center_point' => 'Center Point',
            'shrink' => 'Shrink',
            'country_id' => 'Country',
            'region_name' => 'Region',
            'published_name' => 'Published'
        ];
    }

    /*public function afterSave(){
        $prod = new Surveys;
        Yii::error(print_r($this,true));
        $prod = $this->getDbProd();
        $prod->attributes = $this->attributes;
        if($prod->save())
            return parent::afterSave();

        return false;
    }*/

   /* public static function getDb() {
        //Yii::error(print_r($this,true));
        return \Yii::$app->db;
    }*/

    public function behaviors()
    {
        return [
            //'sammaye\audittrail\LoggableBehaviour'
                /*'LoggableBehavior'=> array(
                        'class' => 'sammaye\audittrail\LoggableBehaviour'//'site.vendor.sammaye.audittrail.LoggableBehavior',
                )*/
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCasestudySurveys()
    {
        return $this->hasMany(CasestudySurvey::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFactsheetViews()
    {
        return $this->hasMany(FactsheetViews::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFactsheets()
    {
        return $this->hasMany(Factsheets::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getFactsheetsSeismics()
    {
        return $this->hasMany(FactsheetsSeismic::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getLines()
    {
        return $this->hasMany(Lines::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSurveyViews()
    {
        return $this->hasMany(SurveyViews::className(), ['survey_id' => 'survey_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCountry()
    {
        return $this->hasOne(Countries::className(), ['country_id' => 'country_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPublished0()
    {
        return $this->hasOne(Published::className(), ['published_id' => 'published']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRegion()
    {
        return $this->hasOne(Regions::className(), ['region_id' => 'region_id']);
    }

    public function setDeleted(){
        $this->published = self::DELETED;
    }
}

1 个答案:

答案 0 :(得分:0)

在我将其更改为以下

后,它正在运行
'rowOptions' => function ($model, $index, $widget, $grid){
            if($model->published == $model::DELETED)
                return ['class'=>'red'];
            elseif($model->published == $model::NEW_S)
                return ['class'=>'new'];
        },