jQuery pagination without all the rows of data before hand

Posted by Aaron Mc Adam on Stack Overflow See other posts from Stack Overflow or by Aaron Mc Adam
Published on 2010-04-14T18:58:48Z Indexed on 2010/04/14 19:03 UTC
Read the original article Hit count: 259

Filed under:
|

Hi guys, I'm trying to find a solution for paginating my search results. I have a built the links and everything works without jQuery. I've also got AJAX working for the links, but I just need a way of tidying up the list of links, as some search results return up to 60 pages and the links spread to two rows in my template.

I am searching the Amazon API, and can only return 10 results at a time. Upon clicking the pagination links, the next 10 results are returned. I have access to the total results number from the XML, but not all the data at once, so I can't put all the data into a "hiddenResults" div that the jQuery Pagination plugin needs.

Here is the jQuery I have for the pagination:

var $pagination = $("#pagination ul");
$pagination.delegate('.pagenumbers', 'click', function() {
    var $$this = $(this);
    $$this.closest('li').addClass('cached');
    $pagination.find("#currentPage").removeAttr('id').wrapInner($('<a>', {'class':'pagenumbers', 'href':'book_data.php?keywords='+keywords}));
    $$this.closest('li').attr('id','currentPage').html( $$this.text() );

    $.ajax({
        type    : "GET",
        cache   : false,
        url     : "book_data.php",
        data    : { keywords : keywords, page : $$this.text() },
        success : show_results
    });

    return false;
});

function show_results(res) {
    $("#searchResults").replaceWith($(res).find('#searchResults')).find('table.sortable tbody tr:odd').addClass('odd');
    detailPage();
    selectForm();
    if ( ! ( $pagination.find('li').hasClass('cached') ) ) {
        $.get("book_data.php", { keywords : keywords, page : ( $pagination.find("#currentPage").text() + 1 ) } );
    }
}

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about php