gpt4 book ai didi

php - 如何让 Yii2 处理数据提供者的两个不同实例?

转载 作者:行者123 更新时间:2023-12-03 22:54:40 28 4
gpt4 key购买 nike

这是 siteController index.php。在这里,我想在同一页上显示来自不同表格的两个列表。但它只为两个 div 显示一个表格数据。当我在两个 div 小部件上仅设置 $dataProvider 时,如果我设置在 actionIndex() 中创建的 $dataProvider2,则会出现错误未找到 $dataProvider2。

                        <?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[

'attribute'=>'practiceCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget) {
},
],
'firstName',

],

]); ?>

</div>
<div class="col-lg-6" style="border:solid 1px;">
<h2>Member</h2>
<?= GridView::widget([
'dataProvider2' => $dataProvider2,//Error $dataProvider2 not found
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[

'attribute'=>'memberCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget) {

},
],
'firstName',
]); ?>
</div>

这是 actionIndex() 在这里我写了两个 dataProvider($dataProvider,$dataProvider2) 但是我无法在 index.php 小部件中设置这个 dataProvider

public function actionIndex()
{
$query = new \yii\db\Query;
$query->select('*')
->from('practice');
$query->createCommand();

$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
$query = new \yii\db\Query;
$query->select('*')
->from('member');
$query->createCommand();

$dataProvider2 = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
],
]);
return $this->render('index', [
'dataProvider' => $dataProvider,'dataprovider2'=>$dataProvider2 ]);
}

最佳答案

您需要将dataProvider2 更改为dataProvider

<div class="col-lg-6" style="border:solid 1px;">
<h2>Member</h2>
<?= GridView::widget([
'dataProvider' => $dataProvider2,//Error $dataProvider2 not found
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[

'attribute'=>'memberCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget) {

},
],
'firstName',
]
]); ?>

因为 dataProvider 是 GridView 的一个属性..它会起作用

并将您的 Controller 操作更改为

return $this->render('index', [
'dataProvider' => $dataProvider,'dataProvider2'=>$dataProvider2 ]);

关于php - 如何让 Yii2 处理数据提供者的两个不同实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327206/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com