引导程序中有这个
<abbr></abbr>
标签,它会自动显示缩写词的弹出窗口。我想将此标签插入到 gridview 中的某个标头,其属性名称为 act
。这是到目前为止我的代码。
[
'attribute'=>'act',
'format'=>'raw',
'label'=>'<abbr title="Area Coordinating Team">ACT</abbr>',
'value'=>function($model){
return '<span class="fa fa-thumbs-up text-green"></span>';
}
],
但是输出字面上显示了整个
<abbr title="Area Coordinating Team">ACT</abbr>
使用:'encodeLabel' => false,
[
'attribute'=>'act',
'format'=>'raw',
'label'=>'<abbr title="Area Coordinating Team">ACT</abbr>',
'encodeLabel' => false,
'value'=>function($model){
return '<span class="fa fa-thumbs-up text-green"></span>';
}
],
如果您想拥有自定义 HTML 和仍然原始的排序功能,您可以创建自己的 DataColumn(假设在公共/组件中),然后在网格视图中设置
dataColumnClass
:
<?= GridView::widget([
...
'dataColumnClass' => 'common\components\HtmlDataColumn',
'columns' => [
'id',
[
'attribute' => 'title',
'htmlHeader' => 'Some Header<span class="glyphicon glyphicon-ok"></span>',
],
...
我的数据栏:
namespace common\components;
use yii\helpers\Html;
/**
* DataColumn that allows HTML in 'header' yet still appends sorting.
*/
class HtmlDataColumn extends \yii\grid\DataColumn
{
public $htmlHeader = null;
/**
* @inheritdoc
*/
protected function renderHeaderCellContent()
{
if ($this->header !== null || $this->label === null && $this->attribute === null) {
return parent::renderHeaderCellContent();
}
if ($this->htmlHeader !== null) {
$label = $this->htmlHeader;
} else {
$label = $this->getHeaderCellLabel();
if ($this->encodeLabel) {
$label = Html::encode($label);
}
}
if ($this->attribute !== null && $this->enableSorting &&
($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
}
return $label;
}
}
希望这有帮助。