EllisLab text mark
Advanced Search
     
Simple Ajax paging with existing pagination lib & jQuery
Posted: 10 July 2009 03:17 AM   [ Ignore ]
Avatar
Joined: 2008-08-17
52 posts

Step 1: edit existing pagination lib:

a. Find:

var $query_string_segment 'per_page'

Add below:

var $is_ajax_paging        FALSE;
    var 
$paging_function       'ajax_paging'// your jquery paging function 

b.  Find:

if ($num_pages == 1)
        
{
            
return '';
        

Add below:

if($this->is_ajax_paging == TRUE)
        
{
            $paging_function 
'onclick="javas+cript:'.$paging_function.'();return false;"';
        
}
        else{
            $paging_function 
'';
        


c. add ’.$paging_function.’ on all of a element you found in Pagination.php

ex:

<a  
should be
<a+'.$paging_function.'//remove plus 


Step 2: config your paging in controller (similar with default paging config):

if you want to use ajax paging just add

$config[‘is_ajax_paging’]    = TRUE; //default FALSE

if don’t, set it to FALSE
Ex:

// Set pagination    
    
$config['base_url']        site_url('my/controller/');        
    
$config['total_rows']      1000;
    
$config['uri_segment']     3;
    
$config['per_page']        '10';
    
$config['is_ajax_paging']      =  TRUE// default FALSE
    
$config['paging_function''ajax_paging' // Your jQuery paging
    
$this->pagination->initialize($config);
            
    
$data['pagination'$this->pagination->create_links();
$data['posts'$this->my_model->get_all_posts($this->pagination->per_page$this->uri->segment(3)) 

;

Step 3: Set your view:

Exp:

<div id 'display-content'>
<?=$pagination?>
<?php 
foreach($posts AS $post):?>
<p><?=$post->title?><p>
<?php endforeach?>


</div><!--End of display-content--> 

 


Step4 : create jQuery paging function

ajax_paging = function(){
    
$("p.pagination a").click(function() {               
               
$.ajax({
                 type
"GET",
                 
url: $(this).get(),
                 
success: function(html){
         
$("#display-content").html(html);
                  
}
               }
);               
             
});            
       return 
false;
     
}


Finish!

——————————-

You can use both non-ajax paging and ajax paging at same time, no need to rewrite your code (just need change $config[‘is_ajax_paging’] to TRUE or FALSE ). Hope it will help smile (sorry for my bad english)

 Signature 

http://drimr.com

 
Posted: 10 July 2009 04:52 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-10
1 posts

any demos?

 
Posted: 10 July 2009 02:03 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-11-09
5 posts

So does

<ahref="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; 

become (without the plus after the a)

<a'.$paging_function.' href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; 
 
Posted: 11 July 2009 01:28 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2008-08-17
52 posts

@kurt: sorry, have no demo now smile
@mrliam69: thanks smile

 Signature 

http://drimr.com

 
Posted: 26 August 2009 11:38 PM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-05
28 posts

could you give me demo and download files with instruction, please?
i’m newbie

 Signature 

Computer Science Student
Freelance Application Developer
NuAzul
blogNuAzul

 
Posted: 27 August 2009 12:01 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-08-05
28 posts

could u give me demo and download files, please?
i’m a newbie.
thanks

 Signature 

Computer Science Student
Freelance Application Developer
NuAzul
blogNuAzul

 
Posted: 30 October 2009 06:47 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2009-10-15
37 posts

hello,

I followed exactly the explanation as Berserk described and applied it into my project.

But I get this error msg: Parse error: syntax error, unexpected T_VARIABLE ....

What is the problem on this line:

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

?

Below is the scripting which I added into controller:

// Set pagination    
        
$config['base_url']        site_url('accounts/');        
        
$config['total_rows']      1000;
        
$config['uri_segment']     3;
        
$config['per_page']        '10';
        
$config['is_ajax_paging']      =  TRUE// default FALSE
        
$config['paging_function''ajax_paging' // Your jQuery paging
        
        
$this->pagination->initialize($config);
                
        
$data['pagination'$this->pagination->create_links();
        
        
$data['accounts'$this->usercontacts_model->get_admin_contacts($this->pagination->per_page$this->uri->segment(3)) 
 
Posted: 05 November 2009 10:41 PM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-10-12
149 posts

you forgot the semicolon ; after config[‘paging_function’] = ‘ajax_paging’

but you’ve probably figured that out by now.

 Signature 

Brent Christensen { lead . developer }
Pyfo Web Design & Development
.(JavaScript must be enabled to view this email address)  |  http://www.pyfo.ca

 
Posted: 07 November 2009 05:38 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Joined: 2008-06-30
36 posts

http://tohin.wordpress.com/2008/08/12/codeigniter-ajax-pagination/
http://tohin.wordpress.com/2008/10/07/codeigniter-ajax-pagination-exampleguideline/

 Signature 

My Blog: tohin.wordpress.com

 
Posted: 01 December 2009 10:44 PM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-03-04
3 posts

very good, thanks for share

 
Posted: 07 February 2010 03:15 PM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-12-25
4 posts

Hi Berserk, thankx for sharing, i got a problem using your code, i have added all your code above and it appears an error in the Pagination.php, here the error at line #3, it says that undefine variable: paging_function and maybe there is something wrong or you forgot to tell us about the jquery paging function code?

      #1   if($this->is_ajax_paging == TRUE)
      #2   {
      #3     $paging_function = ‘onclick=“java+script:’.$paging_function.’();return false;”’;
      $4}
      #5   else{
      #6     $paging_function = ‘’;
      #7   }

Thankx,

 
Posted: 28 March 2011 09:45 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2011-03-16
45 posts

where do i put this code

ajax_paging = function(){
  $(“p.pagination a”).click(function() {         
          $.ajax({
            type: “GET”,
            url: $(this).get(),
            success: function(html){
      $(”#display-content”).html(html);
            }
          });         
        });       
    return false;
    }; 
[/code

and in which file do i call it?

thx

 Signature 

belajar adalah sebuah keberanian diri untuk menantang ketidakmungkinan.

belajar dengan sungguh-sungguh hanya bisa di lakukan oleh seseorang yang bukan penakut.

 
Posted: 11 February 2012 04:48 AM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2010-04-08
7 posts
coma - 07 February 2010 03:15 PM

Hi Berserk, thankx for sharing, i got a problem using your code, i have added all your code above and it appears an error in the Pagination.php, here the error at line #3, it says that undefine variable: paging_function and maybe there is something wrong or you forgot to tell us about the jquery paging function code?

      #1   if($this->is_ajax_paging == TRUE)
      #2   {
      #3     $paging_function = ‘onclick=“java+script:’.$paging_function.’();return false;”’;
      $4}
      #5   else{
      #6     $paging_function = ‘’;
      #7   }

Thankx,

Here’s your answer 2 years later Coma!  The line should be

$paging_function 'onclick="[removed]'.$this->paging_function.'();return false;"'

 

 
Posted: 11 February 2012 10:41 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2010-04-08
7 posts

Here’s a change I recommend for anyone wanting to get this all working.  Instead of using the “onclick” as the OP demonstrates, assign the anchor elements to a class:

if($this->is_ajax_paging == TRUE)
        
{
            $paging_function 
' class="pagecontrol"';
        
}
        else{
            $paging_function 
'';
        

You can then use a simple jQuery selector to detect when a paging control anchor is clicked and react to it.  Here’s mine:

$('.pagingcontrol').live('click', function()
 
{
  
$.ajax({
  type
"GET",
  
url: $(this).get(),
  
success: function(html{
   
$('#content_box').html(html);
  
}
  }
);
  return 
false;
 
}); 
 
Posted: 12 February 2012 12:44 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2009-05-17
1415 posts

Community Auth provides an example of using ajax pagination that falls back on standard pagination if javascript is disabled. It also does not require extending the pagination class. The example is located at “Manage Users” when logged in as and Admin or manager. If you just want to see the code, you can check out the manage_users method in the administration controller.

 Signature 

Brian
Brian’s Web Design - Temecula
Community Auth - CodeIgniter Authentication Application

 
Posted: 27 August 2012 05:20 AM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2012-08-27
1 posts

pls help me to use this code for asynchronous ajax pagination.  I want to do pagination without refreshing the page each time