【问题标题】:Render html in yii2 Gridview Widget在 yii2 Gridview Widget 中渲染 html
【发布时间】:2019-08-13 00:02:01
【问题描述】:

这就是我在网格视图上呈现值的方式

但我可以看到文本值,而不是链接。

如何让它呈现 html 而不是文本?

【问题讨论】:

    标签: php gridview yii2


    【解决方案1】:

    在链接栏配置中添加:

    'format' => 'html',
    

    或者如果你想要一些额外的标记

    'format' => 'raw',
    

    如果是raw,请记住对来自外部用户的值进行编码,因为它不是自动完成的。

    【讨论】:

    • 这种“格式”配置记录在哪里?我在任何地方都找不到它。 api doc中只有一个例子,没有解释
    • 提到了here,例子可以在here找到。
    【解决方案2】:

    在 Yii 中这样做的更好方法。

    'value' => function ($data) {
        return Html::a($data->name, [$data->url, 'someData' => $data->someData]);
    }
    

    Yii 文档:https://www.yiiframework.com/doc/api/2.0/yii-helpers-basehtml#a()-detail

    这篇文章有点晚了,但希望对未来有所帮助。

    【讨论】:

      【解决方案3】:
      <?= GridView::widget([
          'dataProvider' => $dataProvider,
          'filterModel' => $searchModel,
          'columns' => [
              ['class' => 'yii\grid\SerialColumn'],
              'name',
              'email:email',
              'timestamp:date',
              [
                  'attribute'=>'Resume',
                  'format' => 'raw',
                  'class' => 'yii\grid\DataColumn', // can be omitted, as it is the default
                  'value' => function ($data) {
                      $url = "www.sample.com/contactform/resumes".$data->resumepath;
                      return Html::a('<i class="glyphicon glyphicon-download-alt"></i>', $url);
                  },
              ],
      
              ['class' => 'yii\grid\ActionColumn'],
          ],
      ]); ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        • 2020-06-26
        相关资源
        最近更新 更多