简体   繁体   中英

Yii2 ListView and dataprovider

What data must be sended 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 a view:

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

but i got only id`s:

在此输入图像描述

And if i`m set single view like:

'itemView' => '_single',

how send data to _single.php ?

I mean - need default template for view list items like in GridView:

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

        'id',
        'username',
        'email:email',
        'password',
        'role',
//....

And then i got perfect grid:

在此输入图像描述

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

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

$viewParams public property array $viewParams = []

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

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

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 a callback (eg an anonymous function) for rendering each data item. If it specifies a view name, the following variables will be available in the view:

$model: mixed, the data model

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

$index: integer, the zero-based index of the data item in the items array returned by $dataProvider.

$widget: ListView, this widget instance

So your User model data should be available in _single.php as $model->username

So i suppose can use Detail View in _single.php i think:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM