Yii2动态呈现GridView [英] Yii2 Dynamically render GridView
问题描述
我有一个表/模型研讨会(培训)和一个表/模型类别。
他们通过seminar.category_id字段连接,例如我可以使用研讨会 - >分类 - >名称。现在我想为每个类别渲染一个GridView-widget,包含相应的研讨会。
1)我应该使用多个GridViews还是有更好的方法
(我不需要过滤等)
2)如果答案是1)是GridView,我该如何实现它?
- 将代码放入一个小部件和通过foreach调用
- 将代码放在视图中并通过foreach调用
- 将代码放在视图中并通过ListView调用 li>
- ...
对不起,如有需要,我可以发布更多详细信息,但这是一个概念性问题。
更新 - 解决方案:
赫斯姆的回答指出了我正确的方向。因为我必须修改答案,所以我会在这里发布我的解决方案。
注意:我使用ActiveDataProvider
SeminarController / actionIndex :
$ categories = Category :: find() - 化合物其中([ '状态'=>类别:: STATUS_ACTIVE]) - >所有();
$ dataProviders = [];
foreach($ categories as $ category){
$ dataProviders [] = new ActiveDataProvider([
'query'=> $ category-> getSeminars (),
]);
return $ this-> render('index',compact('dataProviders'));
在显示视图的过程中挣扎 - 不要使用:
<?php foreach(...){$ this-> render()} ...?>
查看/研讨会/ index.php:
<?php
foreach($ dataProviders as $ dataProvider){
?>
<?=
$ this-> render('_ categoryRow',[
'dataProvider'=> $ dataProvider,
]);
?>
<?php}?>
/view/seminar/_categoryRow.php:
<?= GridView :: widget([
'dataProvider'=> $ dataProvider,
...
其他提示:
-
从$ dataProvider获取当前类别名称(name是category的列)
$ dataProvider - > query-> primaryModel-> name
-
从$ dataProvider获取研讨会的当前计数
$ dataProvider-> getCount()
多个数据提供者与多个小部件
考虑我们在我们的行动中有这样的代码:
$ categories = Category :: find() - > All();
$ dataProviders = [];
foreach($ categories as $ category){
$ dataProviders [] = new ArrayDataProvider([
'allModels'=> $ c ategory->研讨会,
'sort'=> ['attributes'=> ['id'],],//这里你可以选择你的自定义属性进行排序
'pagination'=> ['pageSize'=> 100]
]);
return $ this-> render('test',compact('dataProviders'));
通过在视图中循环 $ dataProviders
数组,我们可以显示所有类别与他们的儿童研讨会:
foreach($ dataProviders as $ dataProvider){
echo GridView :: widget([$ b $'dataProvider'=> $ dataProvider,
'columns'=> [
['class'=>'yii \grid\SerialColumn'],
'foo',
'bar',
'created_at:relativeTime',
'updated_at:relativeTime',
],
]);
}
希望它可以提供帮助。
I have a table/model "seminar" (Trainings) and a table/model "category". They are connected through the field "seminar.category_id" so e.g. that I can use seminar->category->name. That all works.
Now I want to render a GridView-widget for each category, containing the corresponding seminars.
1) Should I use multiple GridViews or is there a better way (I need no filtering, etc.)
2) If answer of 1) is yes GridView, how should I implement it?
- put the code in a widget and call through foreach
- put the code in a view and call through foreach
- put the code in a view and call through ListView
- ...
Sorry for the short question, if necessary I can post more details, but it's more a conceptual question.
UPDATE - solution:
The answer of Hesam pointed me in the right direction. Since I have to modified the answer I will post my solution here.
Note: I used ActiveDataProvider
SeminarController/actionIndex:
$categories = Category::find()->where(['Status'=>Category::STATUS_ACTIVE])->All();
$dataProviders = [];
foreach ($categories as $category) {
$dataProviders[] = new ActiveDataProvider([
'query' => $category->getSeminars(),
]);
}
return $this->render('index', compact('dataProviders'));
Struggled with getting the views to show - dont't work with:
<?php foreach(...){ $this->render()}... ?>
view/seminar/index.php:
<?php
foreach ($dataProviders as $dataProvider) {
?>
<?=
$this->render('_categoryRow', [
'dataProvider' => $dataProvider,
]);
?>
<?php } ?>
/view/seminar/_categoryRow.php:
<?= GridView::widget([
'dataProvider' => $dataProvider,
...
Additional tips:
Getting the current category name from $dataProvider ("name" is a column of "category")
$dataProvider->query->primaryModel->name
Getting the current count of seminars from $dataProvider
$dataProvider->getCount()
One approach is to use multiple data providers with multiple widgets. Consider we have this code in our action:
$categories = Category::find()->All();
$dataProviders = [];
foreach ($categories as $category) {
$dataProviders[] = new ArrayDataProvider([
'allModels' => $category->seminars,
'sort' =>['attributes' => ['id'],], // Here you can choose your custom attributes for sorting
'pagination' => ['pageSize' => 100]
]);
}
return $this->render('test', compact('dataProviders'));
By looping through $dataProviders
array in view, we can show all categories with their child seminars:
foreach ($dataProviders as $dataProvider) {
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'foo',
'bar',
'created_at:relativeTime',
'updated_at:relativeTime',
],
]) ;
}
Hope it can help.
这篇关于Yii2动态呈现GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!