0
votes

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);
            }
        });
1
have you load library of pagination like $this->load->library('pagination');Evince Development
You assign this $this->pagination->create_links(); to $data['pagination']. So instead of echoing create_links(), just echo $pagination in view.kishor10d
@EvinceDevelopment I loaded the pagination library in the autoload filea_simple_traveler
@kishor10d its not showing anything when I echo $data['pagination']a_simple_traveler
Oh, my mistake. I put $this->pagination->create_links(); before the $this->pagination->initialize('$config');. The create_links() should be after the initialization. It's now showing the pagination links but it's still not updating my pagination links. It's still stuck in page 1.a_simple_traveler

1 Answers

0
votes

May be you are using the wrong element ID?

pagination X paginator?

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