Ayan Bera
Ayan Bera

Reputation: 80

CodeIgniter pagination with Ajax

I want to include pagination in my site operated by AJAX.

MY CONTROLLER

public function get_details() {
    //pagination
    $config =   array(
        'base_url'          =>  base_url()."exam/create/created/get_details/",
        'total_rows'        =>  $this->exam->get_total(),
        'first_link'        =>  "First",
        'last_link'         =>  "Last",
        'per_page'          =>  5,
        'uri_segment'       =>  5,
        'full_tag_open'     =>  "<div id='ajax_pg' class='paginate'>",
        'full_tag_close'    =>  "</div>",
        'num_tag1_open'     =>  "<a>",
        'num_tag1_close'    =>  "</a>",
        'cur_tag_open'      =>  "<a class='current'>",
        'cur_tag_close'     =>  "</a>",
        'next_tag1_open'    =>  "<a>",
        'next_tag1_close'   =>  "</a>",
        'prev_tag1_open'    =>  "<a>",
        'prev_tag1_close'   =>  "</a>",
        'first_tag1_open'   =>  "<a>",
        'first_tag1_close'  =>  "</a>",
        'last_tag1_open'    =>  "<a>",
        'last_tag1_close'   =>  "</a>"
    );
    $page=($this->uri->segment(5) != '') ? $this->uri->segment(5) : 0;
    $this->pagination->initialize($config);

    $data   =   array(
        'query' =>  json_encode($this->exam->get_details($page, $config['per_page'])),
        'links' =>  $this->pagination->create_links()
    );

    echo json_encode($data);
}

MY VIEW

$(function() {
    paginate();

    function paginate() {
        $('#ajax_pg a').on('click', function() {
            var url = $(this).attr('href');
            alert(url);

            return false;
        });
    }
});

Now the CI pagination is working all right but somehow or the other the ajax jQuery click event i not firing.

Thanks in advance.

Upvotes: 0

Views: 3245

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Please Use this code in view

$(function() {
     paginate();
 });

 function paginate() {
     $(document).on('click', '#ajax_pg a', function(event) {
         var url = $(this).attr('href');
         alert(url);
         return false;
     }).click(); //to trigger click event 
 }

Upvotes: 1

Related Questions