Yii2 - 细胞编辑

时间:2015-09-13 07:23:09

标签: css colors yii2 cell

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

1 个答案:

答案 0 :(得分:1)

在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;'],
            ],
        ],  
相关问题