2015-04-22 61 views
0

我在我的gridview上有行css。在使用過濾之後,它不會被應用。下面是方含觀點GridViewyii2 gridview行css在過濾器後沒有得到應用

<?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; 
    } 
} 
+0

貌似有錯字在'$模型 - > published0-> description' ... – user1852788

+0

我沒有看到類型錯誤? 'published0'是關係模型 – shorif2000

+0

是'published0'一個關係嗎?你可以在模型中顯示整個GridView小部件調用和這個關係聲明嗎? – user1852788

回答

0

現在運轉之後,我改變以下

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