Yii2 ListView and dataprovider

前端 未结 4 723
感情败类
感情败类 2021-01-01 02:43

What data must be sended to dataprovider?

In my controller:

public function actionIndex() {
    $searchModel  = new UserSearch();
    $dataProvider =         


        
相关标签:
4条回答
  • 2021-01-01 02:46

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

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

    0 讨论(0)
  • 2021-01-01 03:00

    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
    
    0 讨论(0)
  • 2021-01-01 03:00

    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'
    ] );
    
    0 讨论(0)
  • 2021-01-01 03:04

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

    DetailView::widget([
        'model' => $model,
        'attributes' => [
            'id',
            'username',
            'email:email',
    //....
    
    0 讨论(0)
提交回复
热议问题