最新消息: 新版网站上线了!!!

Yii2 ListView显示数据

控制器
<?php

namespace app\controllers;

use yii\web\Controller;
use yii\data\ActiveDataProvider;
use app\models\ZsDynasty;

class TestController extends Controller
{
    public function actionIndex()
    {
        $query = ZsDynasty::find();
		$dataProvider = new ActiveDataProvider([
							'query' => $query,
							'pagination' => [
								'pageSize' => 15,
							],
						]);
		return $this->render('index', [
            'dataProvider' => $dataProvider
        ]);
    }
}

视图
view/test/index.php
<?php
use yii\helpers\Html;
use yii\widgets\ListView;

$this->title = "朝代";
$this->params['breadcrumbs'][] = $this->title;
?>
<h1>朝代</h1>
<ul>
<?php
echo ListView::widget([
	'dataProvider' => $dataProvider,
	'itemView' => '_item',//子视图
]);
?>
view/test/_item.php
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<div class="post">
    <strong><?= Html::encode($model->dyn_id) ?></strong>
    <a href="test/index/<?= $model->dyn_id ?>"><?= HtmlPurifier::process($model->dyn_name) ?> </a>   
</div>

listview 默认文字修改 \basic\vendor\yiisoft\yii2\widgets\BaseListView.php

转载请注明:谷谷点程序 » Yii2 ListView显示数据