php Yii2 ListView 和数据提供者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26382110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:52:51  来源:igfitidea点击:

Yii2 ListView and dataprovider

phplistviewyii2

提问by WebArtisan

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:

但我只有身:

enter image description here

在此处输入图片说明

And if i`m set single view like:

如果我设置了单个视图,例如:

'itemView' => '_single',

how send data to _single.php ?

如何将数据发送到 _single.php ?

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

我的意思是 - 需要像 GridView 这样的视图列表项的默认模板:

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

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

And then i got perfect grid:

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

enter image description here

在此处输入图片说明

回答by Songwut K.

Controller - SiteController.php

控制器 - 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

查看(一)——index.php

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

View (2) - _item.php

查看(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

回答by Stefano Mtangoo

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

how send data to _single.php ?这是如何使用 $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.

$viewParams 公共属性数组 $viewParams = []

渲染时传递给 $itemView 的附加参数。仅当 $itemView 是表示视图名称的字符串时才使用此属性。

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

回答by user1852788

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

在官方文档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 (e.g. 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

$itemView 公共属性

用于呈现每个数据项的视图的名称,或用于呈现每个数据项的回调(例如匿名函数)。如果它指定了视图名称,则视图中将提供以下变量:

$model:混合,数据模型

$key:混合,与数据项关联的键值

$index:整数,$dataProvider 返回的 items 数组中数据项的从零开始的索引。

$widget:ListView,这个widget实例

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

所以你的用户模型数据应该在 _single.php 中作为$model->username可用

回答by WebArtisan

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

所以我想可以在 _single.php 中使用 Detail View 我认为:

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