Codeigniter应用程序:如何将分页显示为JSON? [英] Codeigniter application: how can I display pagination as JSON?
问题描述
我正在使用 Codeigniter 3.1.8 开发博客应用.
当前,其管理区域与前端之间是很好的分隔.
Currently, its admin area is well separated from the frontend.
在前端,我设法用JSONS替换了经典" Codeigniter视图. (使用AngularJS显示并处理"了JSONS).
In the frontend, I have managed to replace "classic" Codeigniter views with JSONS. (the JSONS are displayed and "handled" with AngularJS).
这是所有帖子中的控制器中的代码:
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url($path);
$config['query_string_segment'] = $query_string_segment;
$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
// All posts
$this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}
我无法做的事情(尽管会折磨我的大脑)也是将帖子的分页也显示为JSON.
What I have not been able to do (despite tormenting my brain), is display the pagination of the posts as JSON too.
在帖子模型中:
public function get_posts($limit, $offset) {
$this->db->select('posts.*,categories.name as post_category');
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'posts.cat_id = categories.id', 'inner');
$query = $this->db->get('posts', $limit, $offset);
return $query->result();
}
我的分页视图:
<div class="pagination-container text-center">
<?php echo $this->pagination->create_links(); ?>
</div>
更新:我已经推送了整个 应用程序 .您可以根据需要进行深入了解.要创建所有MySQL表,请运行Install控制器. :)
UPDATE: I have pushed the entire application to Github. You can have an in-depth look if you want. To create all the MySQL tables, run the Install controller. :)
我该怎么办?
推荐答案
尝试如下操作:
在application/libraries/MY_Pagination.php中创建新类
create new class in application/libraries/MY_Pagination.php
<?php
class MY_Pagination extends CI_Pagination
{
public function get_as_array()
{
return [
'total_rows' => $this->total_rows,
'per_page' => $this->per_page,
'cur_page' => $this->cur_page,
];
}
}
在您的控制器中使用:
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
//this is the line I added
$data['pagination'] = $this->pagination->get_as_array();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
// All posts
$this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}
对于Angular来说,可以使用 https://ng-bootstrap.github .io/#/components/pagination/overview
For Angular can use something like https://ng-bootstrap.github.io/#/components/pagination/overview
这篇关于Codeigniter应用程序:如何将分页显示为JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!