Yii2学习笔记二十八:分页插件与排序实例
1.Yii2分页插件
//分页插件 $query = Country::find(); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize' => '5']); $models = $query->offset($pages->offset) ->limit($pages->limit) ->all(); return $this->render('index', [ 'models' => $models, 'pages' => $pages, ]);1.Yii2分页排序
//排序 $sort = new Sort([ 'attributes' => [ 'population' => [ 'desc' => ['population'=>SORT_DESC], 'asc' => ['population'=>SORT_ASC], 'default' => SORT_DESC, 'label' =>'#population', ], ], 'defaultOrder' => ['attributes' => SORT_DESC], // 看这里看这里看这里,重要的话说三遍 ]); $models = Country::find() ->orderBy($sort->orders)->all(); return $this->render('index', [ 'models' => $models, 'sort' => $sort, ]);
文章
总共 0 条评论