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

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

鉴于上述情况,我只需要显示星星,但我在生成的网格中得到以下信息:

<span class='star'>☆&lt;/span> <span class='star'>☆&lt;/span> <span class='star'>☆&lt;/span>

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

4

2 回答 2

1

GridView Columns 选项中设置formathtml ,如下所示

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

参考Yii2 网格数据列格式

于 2017-10-08T17:41:57.167 回答
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;
            },
        ],
于 2017-10-09T10:53:52.967 回答