Yii2:contentOptions(GridView)中的动态样式
发布时间:2020-12-13 21:53:16 所属栏目:PHP教程 来源:网络整理
导读:在Yii2中有更优雅的动态样式方法: ?= GridView::widget([ 'dataProvider' = $dataProvider,'filterModel' = $searchModel,'columns' = [ ['class' = 'yiigridSerialColumn'],[ 'attribute' = 'fecha','contentOptions' = ['style' = 'color: '.$dataProvi
|
在Yii2中有更优雅的动态样式方法:
<?= GridView::widget([
'dataProvider' => $dataProvider,'filterModel' => $searchModel,'columns' => [
['class' => 'yiigridSerialColumn'],[
'attribute' => 'fecha','contentOptions' => ['style' => 'color: '.$dataProvider->models[0]->fechaVerif->color],],['class' => 'yiigridActionColumn'],]); ?>
一边使用匿名功能?我尝试过类似的东西: '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,$index,$this);
} else {
$options = $this->contentOptions;
}
return Html::tag('td',$this->renderDataCellContent($model,$index),$options);
}
因此,做你想做的事的唯一方法是: [
'attribute' => 'fecha','contentOptions' => function($model)
{
return ['style' => 'color:' . $model->fechaVerif->color];
}
],
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
