Yii2中SqlDataProvider用法示例
作者:懒人 时间:2023-11-20 10:10:05
本文实例讲述了Yii2中SqlDataProvider用法。分享给大家供大家参考,具体如下:
第一种方法:
$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM posts WHERE publish=:publish', [':publish' => 1])
->queryScalar();
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT * FROM posts WHERE publish=:publish',
'params' => [':publish' => 1],
'totalCount' => $totalCount,
//'sort' =>false, to remove the table header sorting
'sort' => [
'attributes' => [
'title' => [
'asc' => ['title' => SORT_ASC],
'desc' => ['title' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Post Title',
],
'author' => [
'asc' => ['author' => SORT_ASC],
'desc' => ['author' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Name',
],
'created_on'
],
],
'pagination' => [
'pageSize' => 10,
],
]);
return $dataProvider;
第二种:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' =>"Name",
'attribute' => 'tbl_column_name',
'value'=>function($data){
return $data["tbl_column_name"];
}
],
'title',
'author',
'created_on',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。
标签:Yii2,SqlDataProvider
0
投稿
猜你喜欢
SQL Server基础之行数据转换为列数据
2024-01-19 22:16:00
Python企业编码生成系统之系统主要函数设计详解
2021-05-11 20:43:49
PHP截取指定图片大小的方法
2023-09-11 13:20:21
OpenCV实现对象跟踪的方法
2022-02-06 04:23:42
python Matplotlib数据可视化(1):简单入门
2022-05-11 19:43:16
一个不错的js+css二级分类菜单代码
2007-12-28 21:22:00
Python Socket实现远程木马弹窗详解
2022-11-28 10:04:39
MySQL存储过程的概念与用法实例
2024-01-20 07:45:30
IE6实现min-width
2008-06-12 12:40:00
SQL Server 获取服务器时间的sql语句
2024-01-20 02:14:52
python实现顺序表的简单代码
2022-01-10 05:03:20
解决python3.6 右键没有 Edit with IDLE的问题
2023-07-31 17:15:00
基于python爬取链家二手房信息代码示例
2022-09-01 18:36:25
python实现套接字创建
2021-09-09 05:12:04
MacOS下本地golang环境搭建详细教程
2024-04-26 17:24:53
Python3读取和写入excel表格数据的示例代码
2022-01-21 10:33:55
Python Mysql自动备份脚本
2024-01-16 00:57:46
python读取csv和txt数据转换成向量的实例
2021-09-12 12:01:24
Python中django学习心得
2023-12-24 17:10:15
opencv对多种颜色小球的形状及位置判断方式
2022-12-18 22:47:50