php – 如何让Yii2处理数据提供者的两个不同实例?
作者:互联网
这是siteController index.php.
在这里,我想在同一页面上显示来自不同表的两个列表.但是它只显示两个div的一个表数据.当我在两个div小部件上只设置$dataProvider时如果我设置$dataProvider2在actionIndex()中创建它会得到错误找不到$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的属性..它将起作用
并将控制器操作更改为
return $this->render('index', [
'dataProvider' => $dataProvider,'dataProvider2'=>$dataProvider2 ]);
标签:php,widget,yii2,dataprovider 来源: https://codeday.me/bug/20190528/1168250.html