Yii2 基本 gridview 只是标题和值 [英] Yii2 basic gridview just header and values
问题描述
我想显示连接查询的数据...(控制器):
I would like to show data of a joined query... (Controller):
$znw_a = Znw::find()->withA()->where(['znw.id' => $zg_id])->one();
...
return $this->render('create', [
...
'znw_a' => $znw_a,
...就像一个非常基本的gridview,没有pager、summary等,只有带有数据的纯标题.主要想法是将它显示为好像它是一个简单转置的详细视图,以便我从左到右而不是从上到下查看数据,例如一个简单的 Excel 表格.
...like a very basic gridview, without pager, summary, etc, only the pure header with the data. The main idea is to show it like if it was a detailview simply transposed, so that I see data from left to right instead of top to bottom, so like a simple Excel table for example.
yii 里有这么简单的小部件吗?因为 GridView 不是这样工作的,在我尝试调整我的查询以匹配 Gridview 的标准之前,也许有人可以给我一个提示,我可以更轻松地实现我想要的.你能指出我正确的方向吗?非常感谢!
Is there such a simple widget like this in yii? Because GridView is not working like this, and before I'm trying to adjust my query in order to match the criteria of Gridview, maybe someone can give me a tip and I can achieve what I want easier. Can you please point me to the right direction? Many thanks!
推荐答案
为此扩展 DetailView 并改用您的类.类似(假设基本项目模板):
Extend DetailView for this one and use your class instead. Something like (assuming basic project template):
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([
// ...
]) ?>
这篇关于Yii2 基本 gridview 只是标题和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!