2017-10-08 66 views
0
[ 
    'label' => 'stars', 
    'value' => function($model){ 

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

返回HTML鉴于上述情况,我只需要显示的星星,但我在生产网格得到波纹管:功能activeDataProvider输出yii2

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

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

回答

1

设置formatHTMLGridView的列选项像下面

[ 
    '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
  [ 
      '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; 
      }, 
     ],