本文介绍了Yii2 ListView 和数据提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
哪些数据必须发送给数据提供者?
What data must be sended to dataprovider?
在我的控制器中:
public function actionIndex() {
$searchModel = new UserSearch();
$dataProvider = $searchModel->search( Yii::$app->request->queryParams );
//other stuff and sending array of params to view
在视图中:
echo ListView::widget( [
'dataProvider' => $dataProvider,
] );
但我只有身份证:
如果我设置了单个视图,例如:
And if i`m set single view like:
'itemView' => '_single',
如何将数据发送到_single.php?
how send data to _single.php ?
我的意思是 - 需要像 GridView 这样的视图列表项的默认模板:
I mean - need default template for view list items like in GridView:
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'id',
'username',
'email:email',
'password',
'role',
//....
然后我得到了完美的网格:
And then i got perfect grid:
推荐答案
Controller - SiteController.php
<?php
// Yii2 Listview Example : by Songwut Kanchanakosai, Thailand.
use commonmodelsMembers;
use commonmodelsSearchMembers;
...
public function actionIndex() {
$searchModel = new SearchMembers();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
?>
查看 (1) - index.php
<?php
use yiiwidgetsListView;
...
echo ListView::widget( [
'dataProvider' => $dataProvider,
'itemView' => '_item',
] ); ?>
查看 (2) - _item.php
<?php
use yiihelpersHtml;
?>
<?=$model->name;?>
<?=$model->age;?>
<?=$model->mobile;?>
示例结果:
Songwut 36 +668-3949-5153
Prawee 41 +668-7323-2334
Kosol 32 +668-8014-0165
Utehn 39 +668-7874-5643
这篇关于Yii2 ListView 和数据提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!