本文介绍了Yii2 ListView和数据提供器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么数据必须sended到DataProvider的?

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,
] );

但我只拿到id`s:

but i got only id`s:

如果我真的设置单一视图,如:

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' => 'yii\grid\SerialColumn'],

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

然后我得到了完美的网格:

And then i got perfect grid:

推荐答案

控制器 - 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,
    ]);
?>

查看(1) - 的index.php

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

查看(2) - _item.php

<?php
use yii\helpers\Html;
?>

<?=$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和数据提供器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:26