2017-10-08 60 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; 
      }, 
     ],