yii2cactivedataprovider

function in activeDataProvider output returning HTML in yii2


[
  '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!


Solution

  • 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