yii2 BizleyDetailView css class

yii2 BizleyDetailView css class

我有一个非常非常好的

我要补充:

'contentOptions' => ['class' => 'danger'],

基于。但不幸的是它不起作用。我错过了什么,或者它不应该工作?你能给我指出正确的方向吗?非常感谢!

此选项从 2.0.10 开始可用。你需要像这样修改我的class:

namespace app\widgets;

use yii\widgets\DetailView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class MyDetailView extends DetailView
{
    public $template = '<td{contentOptions}>{value}</td>';
    public $headerTemplate = '<th{captionOptions}>{label}</th>';

    public function run()
    {
        $rows = [];
        $headers = [];
        $i = 0;
        foreach ($this->attributes as $attribute) {
            list($row, $header) = $this->renderAttribute($attribute, $i++);
            $rows[] = $row;
            $headers[] = $header;
        }

        $options = $this->options;
        $tag = ArrayHelper::remove($options, 'tag', 'table');
        $topRow = Html::tag('tr', implode("\n", $headers));
        $dataRow = Html::tag('tr', implode("\n", $rows));
        echo Html::tag($tag, $topRow . $dataRow, $options);
    }

    protected function renderAttribute($attribute, $index)
    {
        if (is_string($this->template)) {
            $contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
            $row = strtr($this->template, [
                '{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
                '{contentOptions}' =>  $contentOptions,
            ]);
        } else {
            $row = call_user_func($this->template, $attribute, $index, $this);
        }
        if (is_string($this->headerTemplate)) {
            $captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
            $header = strtr($this->headerTemplate, [
                '{label}' => $attribute['label'],
                '{captionOptions}' => $captionOptions,
            ]);
        } else {
            $header = call_user_func($this->headerTemplate, $attribute, $index, $this);
        }
        return [$row, $header];
    }
}