Codeigniter: AJAX Pagination

[ Update:  Download the example and source file from download page. ]

Few days ago, I was looking for an AJAX pagination for codeigniter. Then i saw this AJAX pagniation by Gin2. He used Prototype. In my case i wanted to use JQuery. I saw his approach and wanted to make it work for my project and after a quick typo the pagination library is done.

Though the use is same as the built in pagination system except another extra config parameter :

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

Requirements: Make sure you have added the Jquery.js file on top of the view file. Download latest Jquery from here. Put “Jquery_pagination.php” file in you application/library folder and use it as a separate library.

Files: Jquery_pagination Check here : www.github.com/neotohin

I will try to add an example and a little tutorial very soon. Be in touch… :D

Modification:

To add extra javascript code after clicking the links or do other binding add the extra javascript code in

$config['js_rebind'] = “alert(‘it works !!’); “;

Here is the new File: Jquery_pagination Check here : www.github.com/neotohin

49 Comments »

  1. Soman said

    boss taratari tutorial ta den

  2. ko said

    Great plugin, did exactly what I needed.

  3. s9 said

    I’m having trouble with re-binding other event handlers within the #targetDiv. I have a function which does the rebinding, where should I put it?

  4. tohin said

    when i wrote this ajax pagination .. i wanted a quick solution for my work. That’s why i did not put any options for such kind. Hopefully i will add this soon.

  5. tohin said

    hey s9, i have changed the code. Hopefully this will help you.

  6. neryo said

    thanks man! you are the best! :P

  7. neryo said

    there is a small error in Render the “previous” link

    $this->getAJAXlink( $i, $prev_link )

    to

    $this->getAJAXlink( $i, $this->prev_link )

  8. tohin said

    I am not sure.. how i missed that.. it means i have to be more careful from now on. Thanks man..

    Note: i have changed the code and updated the download links ..
    Again Thanks.

  9. Rick said

    Hey Im using your jquery version. But im running into a problem here
    I have have a view that loads a main template css js etc.. when i use
    the jquery pagination it returns the whole page template and all
    inside the innerHTML div block How do i just retun the data not the whole
    page in CI?

    Thanks
    Rick

  10. tohin said

    Please check the example, hopefully that will help.. thanks

  11. axing said

    Great plugin… Thanks a lot !! :)

  12. Rick said

    Hey I’m using your JQuery Page Lib for CI.. Nice Job i must say
    but I’m having a problem and hope you could help. Basicly
    I have a.click function that is embeded within the data that
    pageinates. the a.click function works on the first paginated page
    with no issues but if i were to click the second page when the
    data loads.. the a.click function does not work no more.. it
    kind of looks like the returned ajax table with the embeded
    a.click function does not see the jQuery is loaded. Did you ever
    have this type of problem?

    Thanks For Your Help
    Rick

  13. tohin said

    $config['js_bind'] actually solves your problem.

    i think your click event is called in ready function of JQuery. copy that part of code and put that in $config['js_bind'].

  14. Rick said

    Hey Thanks for getting back to me Here is the output from the
    js_bind within the jQuery Link

    »

    IM trying to have the #remote click function be read on the second page but
    it doesnt want to work. The remote function Just opens a div with the location
    contents of the href.. It works on the First page but as soon as i click the second page its like the inner content does not see jquery. I even tried putting jquery in the href location with no luck.. Maybee the function is too
    heavy inside the onclick statment?

    Oriniginally i have this function in the document.ready of an external js file
    and the .click is binded to a href with id remote.. This works well
    but it has somthing to do with the contxt being returned in the ajax call
    that stops this binding. Do you have any thoughts? Your Help appreciated.

    Rick

  15. Rick said

    DoAh It Didnt Post The code..

    »

  16. Rick said

    Sorry about that last one..

    <a class="pagination" href="#" onclick=’loading_post("https://paypeopleonline.com/payments/ajax_page/10","#contentAjax","");
    $("#remote").click(function () {
    var loadingDiv = $("#loading");
    loadingDiv.show();
    $("#other").fadeIn();
    var location = $(this).attr("href");
    $("#other").animate({opacity: 1}, 300);
    $("#other").load(location,"",function(){
    loadingDiv.fadeOut("fast");
    })});return false;’>&raquo;</a>

  17. tohin said

    please take this issue to private message communication in codeIgniter forum. I need more information regarding your problem. send me a private message in codeIgniter.

  18. djalma said

    hey, i use you pagination class and nothing else more? the view file is == the other pagination on the wiki?

  19. kailash said

    Hey, great work, i needed this one!
    however, I found a little bug in the js_rebind mechanism.

    The function specified in js_rebind is executed before completion of the ajax request and the binding happens before the requested content is loaded in the page, so loaded content doesn’t get bound.

    I solved this by slightly changing the getAJAXlink method, the rebind function is now called in the ajax callback:

    function getAJAXlink( $count, $text) {
    return “base_url . $count .”‘, {‘t’ : ‘t’}, function(data){
    $(‘”. $this->div . “‘).attr(‘innerHTML’,data);
    ” . $this->js_rebind .”;
    });
    return false;\”>”
    . $text .’
    ‘;
    }

  20. tohin said

    Oops.. thanks.. will change the source ASAP.

  21. Kir said

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: prev_link
    Filename: libraries/Jquery_pagination.php
    Line Number: 180
    . $this->getAJAXlink($i,$prev_link)
    fix
    . $this->getAJAXlink($i,$this->prev_link)

  22. Nikola said

    I’m getting the same error as Kir.

    “Undefined variable: prev_link”

  23. Nikola said

    I’m having some problems with UI Dialog and Tabs here. If I don’t ‘rebind’ the Dialogs / Tabs I obviously don’t Dialogs or Tabs but when I do I get a new instance of them each time I ‘tab’ to the next AJAX page. I’m not clear on why or how this could happen. Any ideas?

  24. Nikola said

    Also, in profiling this page I’ve just noticed the profiler output is being loaded multiple times as well…

  25. tohin said

    @Nikola plz follow the fix of kir and kailash

    That may solve your problem.

  26. Nikola said

    It did, I caught myself shortly after posting… great job!

  27. abada said

    nice job tohin

    iam usin ur JQuery Page Lib but olz
    how can i send a parameter

    such as $config['js_rebind'] = “‘&userpage=’+$(‘#user_id’).val()”;

    to request it ?

    plz help

  28. tohin said

    you want to send an extra parameter during ajax call right ?? rebind’s purpose is different .. to achieve your goal:

    send an extra value in config:
    $config['extra_param'] = 'your_code';

    in library :

    add a variable in class :

    var $extra_param = '';

    and in function: getAJAXlink()

    $.post('". $this->base_url . $count ."' " . $this-> extra_param ."

    I hope this solves your problem :) ..

  29. abada said

    thanks tohin for ur fast reply

    but i have an error

    missing ) after argument.

    thanks for ur efforts

  30. abada said

    hey tohin

    iam trying to change {‘t’ :’t'}

    to {‘t’ : $(‘#user_id’).val()}

    and it work !

    thanks man :D

  31. abada said

    it work if i use {’t’ : $(’#user_id’).val()}

    but if i use {’t’ : “.$_REQUEST["userid"].”}

    it doesn`t work ..

    because $_REQUEST["userid"] is empty in second time

  32. tohin said

    you can put the $_REQUEST ['userid'] transferred into flashdata or session. this will solve your next problem..

  33. abada said

    ya sure but session have an expired time

    i tried cookie but it doesn`t work sometimes

  34. MAK said

    Thanks alot for this tutorial.
    I have a question, if I want to display the pagination links from right to left, for languages like hebrew and arabic as the following:
    Last Next 5 4 3 Prev First
    How I can do that with this library?

  35. tohin said

    hmmm…
    To that i think here is a quick / dirty solution … maybe a better solution can be generate.
    In create_link() function of this library the each link is concat in $output variable.
    Now follow this steps:

    1. Create an array $output = array();
    2. Change all $output .= to $output[] =
    3. Now handle double slash and wrapper ( you can see these 2 lines before return $output. )
    4. Then generate the output string reverse from that array. ( you can use implode after using array reverse )

    Hope this does your work .. :)

  36. abada said

    MAK r u arabian ?

    iam egyptian
    if u arabian
    plz add me
    abadahenno@gmail.com

    i hope we will be friends :D

  37. tohin said

    What mistake i and other made ??
    Why can’t we be friends just because we are not egyptian ?? :( (

    JUST kiDDING… :)

  38. abada said

    tohin ofcours u didn`t make mistake or other

    plz can u and all other be my friends :D

  39. MAK said

    Thanks alot tohin said,

    It works for me but when i made reverse to the output array I got the same prblem, when I skipped the reverse the pagination links works fine. :)

  40. tohin said

    @abada.. be a twitter guy .. add me .. and i will add you :)

    @MAK what problem . ? put your code in http://www.etherpad.com and paste the link here.. this will help me to take a glance and other may help you out too.. :)

  41. MAK said

    @tohin said
    Here is the link of the jquery pagination class after i add my changes
    http://bt2vca.bay.livefilestore.com/y1pzKX5M9BNHVfWT0JaagKvRTO1C13VPkvjp32jyc6_RhWppkiE3swKpHA9d2J1scz3j32i1ERhmz_-hDRzhuCofHoSfWSMhv7j/Jquery_pagination.php?download

    when replace the LAST with الاخيره
    And NEXT with التالي
    FIRST with الاول
    PREVIOUS with السابق

    and generate the paging links the links displayed incorrect.

    But when I skipped the reverse and making array with the paging links and then generate the links the links displayed correctly.

  42. tohin said

    @MAK displayed incorrent means arabic character or some other incorrection.

    another thing MAK please download the library from download section .. i haven’t updated the link inside the post. Just put a Note on top..where to find the latest code.

  43. MAK said

    @tohin said

    first i downloaded the files from the the links which added in this page
    http://www.jhorotek.com/downloads/codeigniter/jquery_pagination.rar
    AND
    http://www.jhorotek.com/downloads/codeigniter/Jquery_pagination_20080925.rar

    But now I have downloaded the new files from the download section.

    I guess the problem which occured with me when displaying arabic labels in the bagination with numbers like 1 2 3 , the numbers must be like 3 2 1, when I made reverse of the array the problem still occure, but when I made implode then return the links the paging links appears correctly.

    Thanks tohin for this library.

  44. kmil0 said

    I’m still waiting for a Demo

    jeje , nice job guy!

  45. tohin said

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

    check this and download section. i am planning to move this to github soon. Please hold little patience :)

  46. mazhel said

    how about pagination from array variable in codeigniter ?
    example :
    i have $mydata = array();
    array (
    [0]=>”hallo”
    [1] =>”world”
    [2]=>’!!!”
    );
    and i want to place per array index to different page..
    please advice me..!!

  47. tohin said

    I think you have found your solution:
    Here In your blog

  48. Husain said

    is it correct statement to initialise Jquery_pagination ?
    $this->Jquery_pagination->initialize($config);
    because i m using this and it will not work and show following error

    Call to a member function initialize() on a non-object in D:\wamp\www\College\system\application\controllers\College_controller.php on line 44

    if not what will be the way to execute your Jquery_pagination code

  49. tohin said

    Have you loaded the library before hand :

    $this->load->library(‘Jquery_pagination’);

RSS feed for comments on this post · TrackBack URI

Leave a Comment