gpt4 book ai didi

gridview - Yii2:contentOptions(GridView)中的动态样式

转载 作者:行者123 更新时间:2023-12-01 09:55:25 25 4
gpt4 key购买 nike

在 Yii2 中是否有比以下更优雅的动态样式方法:

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

[
'attribute' => 'fecha',
'contentOptions' => ['style' => 'color: '.$dataProvider->models[0]->fechaVerif->color],
],

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

除了使用匿名函数?我试过类似的东西:

'contentOptions' => ['style' => 'color: '. fechaVerif.color]

但显然没用。

最佳答案

查看 Yii2 中的 Column::renderDataCell 实现:

public function renderDataCell($model, $key, $index)
{
if ($this->contentOptions instanceof Closure) {
$options = call_user_func($this->contentOptions, $model, $key, $index, $this);
} else {
$options = $this->contentOptions;
}
return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
}

因此,做你想做的唯一方法是:

            [
'attribute' => 'fecha',
'contentOptions' => function($model)
{
return ['style' => 'color:' . $model->fechaVerif->color];
}
],

关于gridview - Yii2:contentOptions(GridView)中的动态样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com