yii2 GridView 过滤标签 audio? [ 2.0 版本 ]
view中
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['attribute'=>'audio', 'format'=>'html', 'value'=>function($model){
return '<div><audio controls=""><source src="'.WEBURL.$model->audio.'" type="audio/mp3"></audio></div>';
}],
],
]); ?>
但最终显示
<td><div></div></td>
audio标签没有了,如果不用GridView直接写audio标签是没问题的。小伙伴们有没有遇到过?
最佳答案
-
'value' => function($model){ return '<div><audio controls=""><source src="'.WEBURL.$model->audio.'" type="audio/mp3"></audio></div>'; }
这里的
value
换成content
试试。看了下
DataColumn
的实现,如果设置value而非Column
的content属性时,DataColumn会默认调用$this->grid->formatter->format
对内容进行格式化。至于为啥过滤掉你的audio标签,可能是你的写法不符合w3c标准?参考w2c School提供的案例:http://www.w3school.com.cn/html5/html5_audio.asp
共 5 条回复@andyron
content的源码是这样的:/** * Renders a data cell. * @param mixed $model the data model being rendered * @param mixed $key the key associated with the data model * @param integer $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]]. * @return string the rendering result */ 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); }
value的是这样写的:
/** * Returns the data cell value. * @param mixed $model the data model * @param mixed $key the key associated with the data model * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. * @return string the data cell value */ public function getDataCellValue($model, $key, $index) { if ($this->value !== null) { if (is_string($this->value)) { return ArrayHelper::getValue($model, $this->value); } else { return call_user_func($this->value, $model, $key, $index, $this); } } elseif ($this->attribute !== null) { return ArrayHelper::getValue($model, $this->attribute); } return null; }
@andyron 如 @lilongsy 贴出来的代码,value和content的区别不是很大。关键是在
DataColunm
中,如果没有设置content回调,在renderDataCellContent
时,会调用formater对内容进行处理。protected function renderDataCellContent($model, $key, $index) { if ($this->content === null) { return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format); } else { return parent::renderDataCellContent($model, $key, $index); } }
@naivefang 那为什么此处只针对
audio
标签进行过滤?
其他 1 个回答
-
共 8 条回复
@naivefang value的匿名函数应该这样的格式调取:
function ($model, $key, $index, $column)@naivefang 源码里是这样写的
/** * Returns the data cell value. * @param mixed $model the data model * @param mixed $key the key associated with the data model * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]]. * @return string the data cell value */ public function getDataCellValue($model, $key, $index) { if ($this->value !== null) { if (is_string($this->value)) { return ArrayHelper::getValue($model, $this->value); } else { return call_user_func($this->value, $model, $key, $index, $this); } } elseif ($this->attribute !== null) { return ArrayHelper::getValue($model, $this->attribute); } return null; }
@naivefang 嗯,可以~
andyron 上海
注册时间:2015-05-05
最后登录:2021-01-31
在线时长:29小时21分
最后登录:2021-01-31
在线时长:29小时21分
- 粉丝7
- 金钱5115
- 威望0
- 积分5405