Yii GridView:根据条件突出显示行

时间:2017-07-05 01:30:21

标签: gridview yii yii2

我们学校有时间表申请。我们正在寻求扩展其功能。

目前,当搜索班级代码时,我们会得到该班级的学生列表。我们为我们的数据库创建了一个新模型,其中包含有医疗要求的学生列表。

我们希望在时间表视图中使用Gridview来突出显示在我们的医疗表中有记录的学生。我们只是不确定如何解决这个问题,我们从未根据条件在gridview中强调过。

时间表模型;

public function rules()
    {
        return [
            [['ttLastName', 'ttFirstName', 'ttYearLevel', 'stuId', 'ttClassId'], 'required'],
            [['ttYearLevel'], 'integer'],
            [['ttLastName', 'ttFirstName', 'stId', 'ttClassId'], 'string', 'max' => 255],
            [['stuId'], 'exist', 'skipOnError' => true, 'targetClass' => Student::className(), 'targetAttribute' => ['stuId' => 'stuId']],
        ];
    }
医学模型;

 public function rules()
    {
        return [
            [['amCreatedAt', 'amBackGround', 'amStrategies', 'amCreatedBy'], 'required'],
            [['amCreatedBy', 'stuId'], 'integer'],
            [['amCreatedAt'], 'string', 'max' => 100],
            [['amBackGround', 'amStrategies'], 'string'],
            [['stuId'], 'exist', 'skipOnError' => true, 'targetClass' => Student::className(), 'targetAttribute' => ['stuId' => 'stuId']],
        ];
    }

时间表gridview;

<?= GridView::widget([
        'dataProvider' => $dataProvider,

        //'filterModel' => $searchModel,
        'columns' => [
            //['class' => 'yii\grid\SerialColumn'],

            'ttLastName',
            'ttFirstName',
            'ttYearLevel',
            'bceId',
            'ttClassId',

            // 'ttClassId',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

任何帮助将不胜感激,我们不需要完整的代码只是解释如何处理它。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以自定义GridView的rowOptions属性以添加网格行的类。然后将css添加到该类以获得背景颜色

<?= GridView::widget([
    'dataProvider' => $dataProvider,

    //'filterModel' => $searchModel,
    'rowOptions' => function ($model, $key, $index, $grid) {
        // $model is the current data model being rendered
        // check your condition in the if like `if($model->hasMedicalRecord())` which could be a method of model class which checks for medical records.
        if() { 
             return ['class' => 'highlighted_grid_row'];
        }
        return [];
    }
    'columns' => [
        //['class' => 'yii\grid\SerialColumn'],

        'ttLastName',
        'ttFirstName',
        'ttYearLevel',
        'bceId',
        'ttClassId',

        // 'ttClassId',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>