简体   繁体   中英

Codeigniter: How to fix pagination links, not updating when clicked, using ajax

I'm trying to put pagination links into my website using ajax to keep the page from refreshing. I managed to make it work but the pagination links are not updating. When I click on the next page, the page will load but the pagination link will stay on page 1. I tried putting $this->pagination->create_links(); inside a variable and echo it inside a div but its showing nothing. Here are my codes:

model:

public function get_posts($limit = FALSE, $rowno = FALSE)
        {
            if($limit)
            {
                $this->db->limit($limit, $rowno);
            }

            $this->db->select('*, posts.image AS post_image, posts.id AS post_id, posts.user_id AS post_user_id');
            $this->db->from('posts');
            $this->db->join('categories', 'categories.id = posts.category_id');
            $this->db->join('users', 'users.id = posts.user_id');
            $this->db->order_by('posts.created_at', 'DESC');

            $query = $this->db->get();
            return $query->result_array();
        }

controller:

public function index()
        {
            // pagination page number
            $pageno = $this->input->post('pageno');

            // pagination config
            // $config['base_url'] = base_url()."posts/index";
            $config['base_url'] = '#';
            $config['total_rows'] = $this->db->count_all('posts');
            $config['per_page'] = 2;
            $config['num_links'] = 3;
            $config['attributes'] = array('class' => 'pagination-class');
            $config['full_tag_open'] = '<div id="paginator">';
            $config['full_tag_close'] = '</div>';

            $data['posts'] = $this->post_model->get_posts($config['per_page'], $pageno);
            $data['title'] = 'Latest Posts';

            $data['pagination'] = $this->pagination->create_links();
            $data['row'] = $pageno;

            // init pagination
            $this->pagination->initialize($config);

            $this->load->view('posts/index', $data);

        }

view:

<div id="pagination">
            <?php echo $this->pagination->create_links(); ?>
    </div>

ajax:

$('#paginator').on('click', 'a', function(e) {
        e.preventDefault();

        var pageno = $(this).attr('data-ci-pagination-page');

        $.ajax({
            data : { 'pageno' : pageno },
            method : 'post',
            // dataType : 'json',
            url : base_url + 'posts/index',
            success : function(response) {
                $("#main_container").html(response);
            }
        });

May be you are using the wrong element ID?

pagination X paginator ?

<div id="pagination"> </div>
$('#pagination') /* .... */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM