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

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

Given the above, I need to display the stars only, but I get bellow in the produced grid:

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

But I want 3 styled stars. Any help would be greatly appreciated!

M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76

2 Answers2

1

Set format as html in GridView Columns option like below

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

Refer Yii2 Grid Data Columns Format

vishuB
  • 4,173
  • 5
  • 31
  • 49
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;
            },
        ],
Pratik Karmakar
  • 247
  • 1
  • 8