The $config array contains your configuration variables. It is passed to the $this->pagination->initialize function. At a minimum we need the three configuration variables shown above.

base_url: Full URL to the controller class/function containing our pagination.

total_rows: Total rows in the result set we are creating pagination for.

per_page: Number of rows we intend to show per page

The pagination links provided by create_links function may not be appropriate for all cases. After-all clicking on these links will do page load which may not be desired on many situation. We may be interested to invoke a javascript function while clicking these links for doing AJAX style pagination. Still we want to use all other features provided by CodeIgniter's Pagination class. This can be accomplished simply. Just follow me ;)

1. All you need to do is adding some more functionality to the existing library i.e. CodeIgniter's Pagination class. To extend the native Pagination class you'll create a file named application/libraries/MY_Pagination.php, and declare your class with:

class MY_Pagination extends CI_Pagination{}

Here MY_ is the sub-class prefix, defined in application/config/config.php as follows:

$config['subclass_prefix'] = 'MY_';

If you need to use a constructor in yo ur class make sure you extend the parent constructor:

js_function_name is the name of the javascript function to invoke when pagination links are clicked.

js_function_params is an array of parameters required for that javascript function. Our customized pagination will; add one more parameter for that javascript function; offset. While viewing the first page, offset=0. In the second page, offset = rows_per_page as defined by $config['per_page'] provided to the $this->pagination->initialize function.

js_href is the name of the div used in the pagination links as the value of the href attribute prefixed with #

This function will be called from the controller function where we are using pagination stuffs.

4. Now modify the function create_links and add this modified function to the MY_Pagination class as create_js_links function. The modification basically ensures that each of the anchor tags generated, will have value of onclick attribute equals the javascript function as defined by the member variable js_function_name having the parameters as defined by the member variable js_function_params plus one extra parameter offset. A comma sperated list of the items in js_function_params will be created. Then the offset parameter will be appended in that comma separated list.

function create_js_links(){ // If our item count or per-page total is zero there is no need to continue. if ($this->total_rows == 0 OR $this->per_page == 0) { return ''; }

// Calculate the total number of pages $num_pages = ceil($this->total_rows / $this->per_page);

// Is there only one page? Hm... nothing more to do here then. if ($num_pages == 1) { return ''; }

5. Now in the controller, where you are using pagination stuffs, initialize the pagition in this way:

//In actual scenario, populate it with number rows to paginate$config['total_rows'] = 100;$config['per_page'] = 10;$config['first_link'] = 'First';$config['last_link'] = 'Last';//change it as per your controller's function's number of parameters$config['uri_segment'] = 3;$this->pagination->initialize($config);

$jsFunction['name'] = 'your_javascript_function_name';//provide your params for the javascript function if there is any//In my case, it is empty$jsFunction['params'] = array();$this->pagination->initialize_js_function($jsFunction);//pass/use this $page_link in your view as per your need$page_link = $this->pagination->create_js_links($pageNo);

This is the basic outline or structure. You have to write down your own javascript function to make ajax call and fill in the missing or leftover details ;)

Now taste the AJAXified pagiantion in action :D We have done a great job already ;)

You can download a complete sample or demo from here: http://www.mediafire.com/file/4ygym0trmjh/ajax_pagination_demo.zip

Sorry. I forgot to mention the password. Even I have forgotten the password:( I'll try to upload the source as soon as I can manage some free time although I think, the content of the post is enough to write the code:)