Yii Tutorial on Yii ListView Widget

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.

public 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 yii\widgets\listview;
   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 yii\helpers\html;
   use yii\helpers\htmlpurifier;
?>
<div class = "user">
   <?= $model->id ?>
   <?= html::encode($model->name) ?>
   <?= htmlpurifier::process($model->email) ?>
</div>

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