yii2.0的分页和排序
//排序 $sort = new Sort([ 'attributes' => [ 'age' => [ 'asc' => ['age' => SORT_ASC], 'desc' => ['age' => SORT_DESC], 'default' => SORT_ASC, 'label' => '年龄' /*'label' => Inflector::camel2words('age'),*/ ], 'name'=>[ 'asc' => ['name' => SORT_ASC], 'desc' => ['name' => SORT_DESC], 'default' => SORT_ASC, 'label' => '姓名' ], ], //默认按id排序,现在改成按name排序 /*'defaultOrder' => ['name' => SORT_ASC],*/ ]);
//创建name和age排序链接 $data['name'] = $sort->link('name'); $data['age'] = $sort->link('age');
$data['person'] = Person::find() -> andWhere(['deleted' => '-1']);; //分页 $data['pagination'] = new Pagination([ 'defaultPageSize' => 4,
//count()是Query类中的方法 'totalCount' => $data['person']->count(), ]); //***$data['person']必须是一个Query对象,才可以调用orderBy(),offset(),limit()等方法 $data['person'] = $data['person']->orderBy($sort->orders) ->offset($data['pagination']->offset) ->limit($data['pagination']->limit) ->all(); return $this->render("show", $data);