Yii2 基本 gridview 只是 header 和值

Yii2 basic gridview just header and values

我想显示连接查询的数据...(控制器):

$znw_a = Znw::find()->withA()->where(['znw.id' => $zg_id])->one();
...
return $this->render('create', [
    ...
    'znw_a' => $znw_a,

...就像一个非常基本的 gridview,没有分页器、摘要等,只有纯粹的 header 和数据。主要思想是将其显示为简单转置的细节视图,以便我从左到右而不是从上到下查看数据,例如简单的 Excel table。

yii有这么简单的widget吗?因为 GridView 不是这样工作的,在我尝试调整我的查询以匹配 Gridview 的标准之前,也许有人可以给我提示,我可以更轻松地实现我想要的。你能给我指出正确的方向吗?非常感谢!

为此扩展 DetailView 并改用您的 class。像(假设基本项目模板):

namespace app\widgets;

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

class MyDetailView extends DetailView
{
    public $template = '<td>{value}</td>';
    public $headerTemplate = '<th>{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)) {
            $row = strtr($this->template, [
                '{value}' => $this->formatter->format($attribute['value'], $attribute['format']),
            ]);
        } else {
            $row = call_user_func($this->template, $attribute, $index, $this);
        }
        if (is_string($this->headerTemplate)) {
            $header = strtr($this->headerTemplate, [
                '{label}' => $attribute['label'],
            ]);
        } else {
            $header = call_user_func($this->headerTemplate, $attribute, $index, $this);
        }
        return [$row, $header];
    }
}

保存到/widgets/MyDetailView.php

一起使用
use app\widgets\MyDetailView;

<?= MyDetailView::widget([
    // ...
]) ?>