使用Ajax和JSON进行分页

时间:2013-10-01 11:53:31

标签: javascript jquery ajax json pagination

我正在尝试使用我下载的插件将我的回调放入分页插件。

我在正确的位置进行了回调,但pageselectCallback函数未初始化,它复制第一页记录并将其放入div中。

     // This demo shows how to paginate elements that were loaded via AJAX
  // It's very similar to the static demo.

/**
* Callback function that displays the content.
*
* Gets called every time the user clicks on a pagination link.
*
* @param {int}page_index New Page index
* @param {jQuery} jq the container with the pagination links as a jQuery object
*/
function pageselectCallback(page_index, jq){
    var new_content = $('#hiddenresult div.result:eq('+page_index+')').clone();
    $('#Searchresult').empty().append(new_content);
    return false;
   }

    /** 
   * Callback function for the AJAX content loader.
    */
    function initPagination() {
    $('#answer').text('This is a call to see that it is initiated');
    var num_entries = $('#hiddenresult div.result').length;
    // Create pagination element
    $("#Pagination").pagination(num_entries, {
    num_edge_entries: 2,
    num_display_entries: 8,
    callback: pageselectCallback,
    items_per_page:1
    });
}

// Load HTML snippet with AJAX and insert it into the Hiddenresult element
// When the HTML has loaded, call initPagination to paginate the elements        
$(document).ready(function() {
   $.ajax({ 
        url: "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_AU&city=Brisbane&stateProvinceCode=QL&countryCode=AU&&numberOfResults=10&_type=json",
       dataType: "jsonp",
        success: function(data) {
        var StrHotelListResponse = data.HotelListResponse.HotelList.HotelSummary;
            $.each(StrHotelListResponse, function(index, value) {
            var StrlowRate = parseInt(value.lowRate);
               $('#hiddenresult').append('<div class="hotel-name">' + value.name + '</div>');
               $('#hiddenresult').append('<div class="hotel-rating">' + value.hotelRating + '</div>');
               $('#hiddenresult').append('<div class="hotel-address">' + value.address1 + ' ' + value.city + ', ' + value.stateProvinceCode + ' ' + value.postalCode  + '</div>');
        });

              initPagination();
          },
       error: function(e) {
           console.log(e.message);
           //alert('no');
       }
   });
   });

1 个答案:

答案 0 :(得分:1)

通过执行$(initPagination);,您没有调用该函数。你将它包装在jQuery函数中。

你想要像任何其他功能一样调用它:

 initPagination();