Yii2 ListView 和数据提供者

Yii2 ListView and dataprovider(Yii2 ListView 和数据提供者)
本文介绍了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 和数据提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)