Welcome Guest, Not a member yet? Register   Sign In
Simple Ajax paging with existing pagination lib & jQuery
#1

[eluser]Berserk[/eluser]
Step 1: edit existing pagination lib:

a. Find:
Code:
var $query_string_segment = 'per_page';


Add below:

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

b. Find:
Code:
if ($num_pages == 1)
        {
            return '';
        }

Add below:

Code:
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:
Code:
<a  >
should be
Code:
<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

Quote:$config['is_ajax_paging'] = TRUE; //default FALSE

if don't, set it to FALSE
Ex:

Code:
// 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:
Code:
<div id = 'display-content'>
&lt;?=$pagination?&gt;
&lt;?php foreach($posts AS $post):?&gt;
<p>&lt;?=$post->title?&gt;<p>
&lt;?php endforeach?&gt;


</div>&lt;!--End of display-content--&gt;




Step4 : create jQuery paging function
Code:
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)
#2

[eluser]Unknown[/eluser]
any demos?
#3

[eluser]mrliam69[/eluser]
So does
Code:
<a+ href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;

become (without the plus after the a)

Code:
<a+ '.$paging_function.' href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
#4

[eluser]Berserk[/eluser]
@kurt: sorry, have no demo now Smile
@mrliam69: thanks Smile
#5

[eluser]sihijau[/eluser]
could you give me demo and download files with instruction, please?
i'm newbie
#6

[eluser]sihijau[/eluser]
could u give me demo and download files, please?
i'm a newbie.
thanks
#7

[eluser]jiahui[/eluser]
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:
Code:
$this->pagination->initialize($config);
?

Below is the scripting which I added into controller:

Code:
// 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))
#8

[eluser]charlie spider[/eluser]
you forgot the semicolon ; after config['paging_function'] = 'ajax_paging'

but you've probably figured that out by now.
#10

[eluser]ngocthai[/eluser]
very good, thanks for share




Theme © iAndrew 2016 - Forum software by © MyBB