问题
I am trying to add the number of pages paginated value to the json response for Cakephp 3.3. I followed the Cakephp 3.3 documentation to make queries using the paginator method and activated the json extension where you just have to add .json to the end of your controller and the function to get a json response. The results I get are the first page of pagination, however I'd like to add a parameter before that tells me how many pages have been paginated so that I can show how many more pages can be continued to. Example of the json response i am trying to get is below with pages being the paginator value:
{ pages: [pages:5],
clients: [
{
id: 1,
first_name: "Kirby",
last_name: "Bonner"
}]
}
I've been told multiple times to look through the documentation, but to be honest I don't see a clear explanation of what I'm trying to do here. I'd really appreciate if someone could direct me to an example or documentation that explains clearly how to do this. Thank you.
回答1:
I found the answer to the question at: http://florian-kraemer.net/2015/10/cakephp-angularjs-pagination/
However you have to put the validation for pagination after you complete the query otherwise it will always return false;
Here is an example of the code:
public function index() {
$query = $this->Table->find();
if (empty($this->request->params['paging'][$this->Table->alias()])) {
$paging = false;
} else {
$paging = $this->request->params['paging'][$this->Table->alias()];
}
$this->set('records', $this->paginate($query));
$this->set('paging', $paging);
$this->set('_serialize', ['records', 'paging']);
}
来源:https://stackoverflow.com/questions/40225505/adding-pagination-value-to-json-response-in-cakephp-3-3