2015-06-24 13:53:17 5029次浏览 2条回答 0 悬赏 10 金钱

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标签是没问题的。小伙伴们有没有遇到过?

最佳答案

  • naivefang 发布于 2015-06-24 15:07 举报
    '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 条回复
    回复于 2015-06-24 15:41 回复

    换成content可以了,能解释一下valuecontent的区别吗?

    回复于 2015-06-24 15:42 回复


    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;
    }
    
    回复于 2015-06-24 15:47 回复

    贴出来的代码,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);
            }
        }
    
    回复于 2015-06-24 16:19 回复

    那为什么此处只针对audio标签进行过滤?

    回复于 2015-06-24 16:25 回复

    看了下format的实现yii\helper\BaseHtmlPurifier,绕得比较远,没仔细看,猜想可能是你的标签嵌套有问题。找个前端问下?

    , , 觉得很赞
  • 回答于 2015-06-24 15:00 举报
    8 条回复
    回复于 2015-06-24 15:08 回复

    配置哪里有问题,能详细说下么?

    回复于 2015-06-24 15:32 回复

    value的匿名函数应该这样的格式调取:
    function ($model, $key, $index, $column)

    回复于 2015-06-24 15:36 回复

    他回调里不接收$key什么的应该问题不大吧? 因为他知道是取的$model->audio,只要正确接收$model应该就可以了吧?

    回复于 2015-06-24 15:39 回复

    源码里是这样写的

    /**
     * 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;
    }
    
    回复于 2015-06-24 15:42 回复

    知道,我的意思是,如果匿名函数里面不需要使用call_user_func返回的后面参数,那么,我觉得closure可以按照他那样写。

    回复于 2015-06-24 15:44 回复

    嗯,可以~

    回复于 2015-06-24 15:45 回复

    应该不是这个问题,这种$model->audio是可以取得到的

    回复于 2015-06-24 15:46 回复

    嗯,我还以为是回调的参数不对,楼下已经解决。:-)

您需要登录后才可以回答。登录 | 立即注册
andyron
副总裁

andyron 上海

注册时间:2015-05-05
最后登录:2021-01-31
在线时长:29小时21分
  • 粉丝7
  • 金钱5115
  • 威望0
  • 积分5405

热门问题