函数在activeDataProvider输出中返回yii2中的HTML

时间:2017-10-08 17:30:14

标签: yii2 cactivedataprovider

[
  'label' => 'stars',
  'value' => function($model){

    $stars = $model->score;
    $result = "";
    for($i=1; $i<=$stars;$i++){ 
        $result .= "<span class='star'>☆</span>";
    }
    return $result;
  },
],

鉴于上述情况,我只需要显示星星,但我会在生成的网格中显示出来:

<span class='star'>☆</span> <span class='star'>☆</span> <span class='star'>☆</span>

但我想要3个风格的明星。 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

GridView 列选项中将format设置为 html ,如下所示

[
  'label' => 'stars',
  'value' => function($model){
      $stars = $model->score;
      $result = "";
      for($i=1; $i<=$stars;$i++){ 
          $result .= "<span class='star'>☆</span>";
      }
      return $result;
  },
  'format' => 'html'
],

参考Yii2 Grid Data Columns Format

答案 1 :(得分:1)

         [
            'label' => 'stars',
            'format'=>'html', // Use this Line
            'value' => function($model){

                $stars = $model->score;
                $result = "";
                for($i=1; $i<=$stars;$i++){
                    $result .= '<i class="fa fa-star-half-o" aria-hidden="true"></i>';
                }
                return $result;
            },
        ],