Yii2中SqlDataProvider用法示例
程序员文章站
2024-02-29 10:29:22
本文实例讲述了yii2中sqldataprovider用法。分享给大家供大家参考,具体如下:
第一种方法:
$totalcount = yii::$app-&g...
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于yii框架的php程序设计有所帮助。
上一篇: hibernate一对多关联映射学习小结
下一篇: java异步写日志到文件中实现代码