分页的函数例子
public function actionIndex($perPage = 20, $type = null, $isWeek = 0)
{
Yii::$app->response->format = Response::FORMAT_JSON;
//判断周末非周末
$weekWhere = $isWeek == 0 ? ['>','start_time',getLastEndTime()]: ['<','start_time',getLastEndTime()];
// 判断互动的类型是否为全部或单独的活动类型
$typeWhere = $type > 0 ? ['type_id' => $type]: '';
$query = Activity::find()
->with([
'type',
'tags',
'question',
'user',
'answerList',
'feedbackList'
])
->asArray()
->where(
['and',
['in', 'status', [
Activity::STATUS_DRAFT,
Activity::STATUS_RELEASE,
Activity::STATUS_PREVENT,
Activity::STATUS_SHUT,
Activity::STATUS_CANCEL,
]],
$weekWhere,
$typeWhere,
]
)
->orderBy($this->activity_order);
$countQuery = clone $query;
$pagination = new Pagination([
'totalCount' => $countQuery->count(),
'pageSize' => $perPage
]);
// 总页数
$totalCount = $pagination->totalCount;
// 活动的数据
$activities = $query->offset($pagination->offset)
->limit($pagination->limit)
->all();
foreach ($activities as $key => $activity) {
$activities[$key]['answer_count'] = count($activity['answerList']);
$activities[$key]['feedback_count'] = count($activity['feedbackList']);
$activities[$key]['preview_url'] = Yii::$app->params['domain'].'preview/'.$activity['id'];
$activities[$key]['filter_url'] = Yii::$app->params['domain'].'filter/'.$activity['id'];
//set last week days
$activities[$key]['this_week'] = getLastEndTime() < $activity['end_time'] ? 1 : 0;
}
return [
'totalCount' => $totalCount,
'activities' => $activities,
];
}
注意当前页数是yii内置的,访问这个参数的时候,直接传参数page就可以!
前台代码 angular框架
fetchPage: function(type, page, isWeek) {
page = page || 1;
var params = {
'type': type,
'page': page,
'isWeek': isWeek,
'perPage': 10 //每页20条
};
return $http.get('/activity', {
params: params
}).then(function(data) {
return data;
});
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。