Yii2 ListView 和数据提供者

2022-01-07 00:00:00 listview php yii2

哪些数据必须发送给数据提供者?

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

相关文章