Abh*_*ain 5 php gridview html-encode yii2
ajax 请求调用以下操作,其响应为 JSON:
\Yii::$app->response->format = 'json';
if($userId){
$dataProvider = new ArrayDataProvider([
'allModels' => Templates::getTemplates($userId,'n'),
]);
$response = $this->renderAjax('index', ['dataProvider' => $dataProvider,]);
return ['status'=>true,'data'=>$response,'total'=>count($dataProvider)];
}
Run Code Online (Sandbox Code Playgroud)
在这个action的View中,有一个GridView Widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return '<a href="'.$url.'" title="'.Html::encode($data->template_name).'">'.Html::encode($data->template_name).'</a>';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
Run Code Online (Sandbox Code Playgroud)
但这对模板名称的 html 值进行编码。例如:测试 <a href="test.php">测试</a>
我不需要这种编码。请帮我解决这个问题。
你应该使用 format => raw
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute'=> 'template_name',
'label'=>'Test Name',
'format' => 'raw',
'value' => function($data){
$url = Yii::$app->urlManager->createUrl('templates/get-tests')."&id=".$data->id;
return '<a href="'.$url.'" title="'.Html::encode($data->template_name).'">'.Html::encode($data->template_name).'</a>';
}
],
[
'attribute'=> 'template_date',
'label'=>'Beginning Date'
],
[
'attribute'=> 'template_expire_time',
'label'=>'End Date'
],
'user_id',
],
]); ?>
Run Code Online (Sandbox Code Playgroud)