在GridView行yii2中禁用交替的背景色

Gib*_*ibs 1 html css gridview yii2

我能够修改gridview的行颜色,但我认为有一个默认属性可以处理交替的行颜色,因为我在样式上指示的是红色,但是当显示时,偶数行为红色,而交替行为白色。白色的行应为绿色。我认为这是由yii出于某些可读性目的制作的。

奇怪的是,字体颜色是根据我提供的类进行的。

<style>
.stateCritical:nth-child(even) {
    color: black;
    background-color: red;
}

.stateCritical:nth-child(odd) {
    color: blue;
    background-color: green;
}

.stateOk {
    color: black;
    background-color: #C0FFBE;
}
</style>

<?= GridView::widget([
                'dataProvider' => $dataProvider,
                'rowOptions' => function($model) {
                    if ($model->last_hard_state == 2){
                        return ['class' => 'stateCritical'];
                    }
                    return ['class' => 'stateOk'];    
                },
    .
    .
    .
    .
    .
?>
Run Code Online (Sandbox Code Playgroud)

如何覆盖默认背景色?

Joe*_*ler 5

一种更简单的方法是覆盖gridView小部件的类。它得到条纹是因为网格的默认类是table table-striped

只需将其添加到您的小部件声明中即可;

GridView::widget([
    'dataProvider' => $dataProvider,
    'tableOptions' => ['class' => 'table table-bordered']
]);
Run Code Online (Sandbox Code Playgroud)