How to change the color of the header for all gridview in yii2?

advertisements

I want to change background color of header for all my GridView headers in yii2. I know the following code does this work but I want change only once in code, all header of the same color.

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

    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        [
            'attribute' => 'user_id',
            'headerOptions' => ['style' => 'background-color:#ccf8fe'],
        ],
    ],
]); ?>

What should I do?


Adding style options to each column seems to be the only way, but I can't guarantee. However, you're not limited to only that. The workaround solution:

1) Add this code somewhere in GridView::widget (for example, above columns => [...]:

 // ...
 'filterModel' => $searchModel,
 'options' => [
    'class' => 'YourCustomTableClass',
 ],
 // ...

2) Add new style rules to it (in css file):

.YourCustomTableClass table thead {
    background-color: #FF0000;
}

CSS will apply background color (red) for header in each column. I have tested this myself to confirm it works.