Yii2 ListView and Data Provider

What data should be referred to dataprovider?

In my controller:

public function actionIndex() { $searchModel = new UserSearch(); $dataProvider = $searchModel->search( Yii::$app->request->queryParams ); //other stuff and sending array of params to view 

in view:

 echo ListView::widget( [ 'dataProvider' => $dataProvider, ] ); 

but I only got id`:

enter image description here

And if i`m sets one view, for example:

 'itemView' => '_single', 

How to send data to _single.php?

I mean - I need a default template for view list items, for example, in a GridView:

 GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], 'id', 'username', 'email:email', 'password', 'role', //.... 

And then I got the perfect grid:

enter image description here

+5
source share
4 answers

Controller - SiteController.php

 <?php // Yii2 Listview Example : by Songwut Kanchanakosai, Thailand. use common\models\Members; use common\models\SearchMembers; ... public function actionIndex() { $searchModel = new SearchMembers(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); ?> 

View (1) - index.php

 <?php use yii\widgets\ListView; ... echo ListView::widget( [ 'dataProvider' => $dataProvider, 'itemView' => '_item', ] ); ?> 

View (2) - _item.php

 <?php use yii\helpers\Html; ?> <?=$model->name;?> <?=$model->age;?> <?=$model->mobile;?> 

Example Result:

 Songwut 36 +668-3949-5153 Prawee 41 +668-7323-2334 Kosol 32 +668-8014-0165 Utehn 39 +668-7874-5643 
+12
source

how send data to _single.php ? Here's how, use $viewParams

$ viewParams public property array $ viewParams = []

Additional parameters that should be passed to $ itemView when it is rendered. This property is used only when $ itemView is a string representing the name of the view.

 echo ListView::widget( [ 'dataProvider' => $dataProvider, 'viewParams'=>['name'=>'My Name is Stefano'], //acccessed in view as $name with value 'My Name is Stefano' ] ); 
+3
source

in official docs http://www.yiiframework.com/doc-2.0/yii-widgets-listview.html# $ itemView-detail

$ itemView public property

The name of the view for rendering each data item or callback (for example, an anonymous function) for rendering each data item. If it sets the view name, the following variables will be available in the view:

model $:, data model

$ key:, the key value associated with the data item

$ index: integer, an index based on the zero of the data element in the array of elements returned by $ dataProvider.

$ widget: ListView, this widget instance

So your user model data should be accessible in _single.php as $ model-> username

+2
source

So, I can use Detail View in _single.php, I think:

 DetailView::widget([ 'model' => $model, 'attributes' => [ 'id', 'username', 'email:email', //.... 
0
source

Source: https://habr.com/ru/post/1204732/


All Articles