Yii2 - 单元格编辑

Yii2 - Cell Edit

如何在 gridview 中编辑单元格 CSS(如背景颜色)? 请注意,我只需要编辑一个单元格而不是整列或整行。 具体来说,网格视图中有一个标记为 'colors' 的列,我希望每个单元格的背景颜色都与那里写的颜色相同。

在 gridView 中,在每一列中,您可以设置 contentOptionsvalue 参数

这是一个示例,其中:

对于第一列,您为该列的所有单元格分配颜色,

在第二列中,您可以根据函数内部评估的颜色值(在此示例中,颜色值由模型提供)为单个单元格分配颜色。然后编写正确的 html 代码并以行格式呈现你设置你想要的颜色

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'your_attribute',
                'label' => 'your_labe',
                'contentOptions' => ['style' => 'background-color: #000000;'], 
            ],
            ....
            ....

            [
                'attribute' => 'your_attribute_cell',
                'label' => 'your_label_cell',
                'format' => 'raw',
                'value' => function ($model) { 
                    return  "<span style='background-color:" . $model->yourColor  "'  >"  . $model->your_attribute_cell. " </span>";
                },
                'contentOptions' => ['style' => ' text-align: center; width: 100px;'],
                'headerOptions' => ['style' => 'text-align: center;'],
            ],
        ],