English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - ListView Widget
  • 时间:2024-09-17

Yii - ListView Widget


Previous Page Next Page  

The ListView widget uses a data provider to display data. Each model is rendered using the specified view file.

Step 1 − Modify the actionDataWidget() method this way.

pubpc function actionDataWidget() {
   $dataProvider = new ActiveDataProvider([
       query  => MyUser::find(),
       pagination  => [
          pageSize  => 20,
      ],
   ]);
   return $this->render( datawidget , [
       dataProvider  => $dataProvider
   ]);
}

In the above code, we create a data provider and pass it to the datawidget view.

Step 2 − Modify the datawidget view file this way.

<?php
   use yiiwidgetsListView;
   echo ListView::widget([
       dataProvider  => $dataProvider,
       itemView  =>  _user ,
   ]);
?>

We render the ListView widget. Each model is rendered in the _user view.

Step 3 − Create a file called _user.php inside the views/site folder.

<?php
   use yiihelpersHtml;
   use yiihelpersHtmlPurifier;
?>
<span class = "user">
   <?= $model->id ?>
   <?= Html::encode($model->name) ?>
   <?= HtmlPurifier::process($model->email) ?>
</span>

Step 4 − Type http://localhost:8080/index.php?r=site/data-widget in the address bar of the web browser, you will see the following.

ListView Widget Example Output Advertisements