這篇文章將為大家詳細講解有關SqlDataProvider怎么在Yii2中使用,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

第一種方法:
$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'],
],
]); ?>關于SqlDataProvider怎么在Yii2中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享標題:SqlDataProvider怎么在Yii2中使用-創新互聯
文章來源:http://www.yijiale78.com/article2/ceihoc.html
成都網站建設公司_創新互聯,為您提供網站維護、用戶體驗、網站營銷、動態網站、小程序開發、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯