Saturday, October 12, 2013

AJAX Pagination in Codeigniter

How to Use JQuery Pagination:

The use is same as the built in pagination system with extra config parameter :

$config['div'] = ‘#content’; /* Here #content is the CSS selector for target DIV */

$config['js_rebind'] = “alert(‘it works !!’); “; /* if you want to bind extra js code */

$config['additional_param'] = ‘serialize_form()’; /* If you are using ajax pagination with Search */


Requirements: Make sure you have added the Jquery.js file on top of the view file.


Put “Jquery_pagination.php” file in you application/library folder.


Don’t forget to use : $this->jquery_pagination-> instead of $this->pagination


 


/* Include Searching with Ajax Pagniation */


1. You have a form with id ‘myform’ before pagination. Which holds input, textarea, selectbox etc for filtering.

2. Create a function in view file as :


<script type="text/javascript">
function serialize_form() {
return $('#myform').serialize();
}
</script>

 


3. Now add another parameter in Config of jquery pagination:


$config['additional_param'] = 'serialize_form()';

 


4. Now you will receive filtering datas as POST in your pagination function in controller.


 


Here is some example :


I wanted to create a full application example but now i think it’s better to show this one. It’s not a tested code rather than a small guideline.


class Test extends Controller {

function page($offset = 0)
{
ob_start();
$this->ajax_page( 0 );
$initial_content = ob_get_contents();
ob_end_clean();

$data['table'] = "<div id='content'>" . $initial_content . "</div>" ;

$this->load->view('page',$data);

}

function ajax_page($offset = 0)
{
$this->load->model('model');
$this->load->library('Jquery_pagination');

$config['base_url'] = site_url('test/ajax_page/');
/* Here i am indicating to the url from where the pagination links and the table section will be fetched */

$config['div'] = '#content';
/* CSS selector for the AJAX content */

$config['total_rows'] = $this->model->num_rows();
$config['per_page'] = 20;

$this->jquery_pagination->initialize($config);

$this->load->library('table');

$html = $this->jquery_pagination->create_links() . br(1)
. $this->table->generate($this->model->content( $limit, $offset));

echo $html;
}
}

Note: Don’t forget to add the jquery.js in your view file, Jquery_pagination.php in your library folder.

Jquery_pagination



AJAX Pagination in Codeigniter

No comments:

Post a Comment