Yii2的ListView用法
控制器
public function actionListView()
{
$this->layout = false;
$dataProvider = new ActiveDataProvider([
'query' => Material::find()->orderBy('id'),
'pagination' => [//分页
'pagesize' => 12
]
]);
return $this->render('list-view', [
'dataProvider' => $dataProvider
]);
}
视图 list-view.php
<?php
use yii\widgets\ListView;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
</head>
<body>
<?= ListView::widget([
'dataProvider' => $dataProvider,//数据提供器
'itemView' => '_item-view',//指定item视图(该视图文件与当前视图在同一个目录下)
'layout' => '{items}<div class="col-lg-12 sum-pager">{summary}{pager}</div>',//整个ListView布局
'itemOptions' => [//针对渲染的单个item
'tag' => 'div',
'class' => 'col-lg-2'
],
'options' => [//针对整个ListView
'tag' => 'div',
'class' => 'col-lg-12'
],
'pager' => [
//'options' => ['class' => 'hidden'],//关闭分页(默认开启)
/* 分页按钮设置 */
'maxButtonCount' => 5,//最多显示几个分页按钮
'firstPageLabel' => '首页',
'prevPageLabel' => '上一页',
'nextPageLabel' => '下一页',
'lastPageLabel' => '尾页'
]
]);?>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</body>
</html>
列表项视图 _item-view.php
<div class="card" style="border: thin dotted #FF0000; margin: 10px;">
<div class="card-body">
<p>
<label>标题</label>
<span class="card-title"><?=$model->title;?></span>
</p>
<p>
<label>类型</label>
<span><?=$model->type;?></span>
</p>
<a href="#" class="btn btn-link">查看</a>
</div>
</div>
显示效果
标签:dataProvider,视图,用法,item,Yii2,ListView,view From: https://www.cnblogs.com/hu308830232/p/16873202.html