jQuery分页显示多个元素

时间:2011-05-17 09:32:12

标签: javascript jquery plugins jquery-plugins jquery-pagination

我正在尝试使用Pagination Plugin对某些项目搜索结果进行分页。不幸的是,在显示多个结果时,演示和文档不是很清楚。至少那是我的感受......

所以无论如何,似乎我需要在回调函数中编写自己的Skip和Take代码以获得我需要的结果。这是我到目前为止所得到的:

function setupPagination(num_items) {
    var num_entries = $('#hiddenItemsContainer div.indexItem').length;
    // Create pagination element
    $("#paginationWidget").pagination(num_entries, {
        current_page: 0,
        items_per_page: num_items,
        num_display_entries: 5,
        next_text: 'Next',
        prev_text: 'Prev',
        callback: pageselectCallback,
        num_edge_entries: 1
    }); 
}

function pageselectCallback(page_index, jq){
    var num_entries = $('#hiddenItemsContainer div.indexItem').length;
    var items_per_page = $('#ItemsPerPage').val();
    var newcontent = ($('#hiddenItemsContainer div.indexItem').slice(Math.min((page_index+1) * items_per_page), items_per_page)).clone();
    console.log(newcontent);

    // Replace old content with new content
    $('#itemsContainer').empty().html(newcontent);

    return false;
}

我一直得到一个空数组作为newContent的值。所以我使用slice函数的方式有问题。

有什么想法吗?

更新

问题解决了。我终于想通了!这是解决方案:

function pageselectCallback(page_index, jq){
    console.log(page_index);
    var num_entries = $('#hiddenItemsContainer div.indexItem').length;
    var items_per_page = $('#ItemsPerPage').val();
    var newcontent = ($('#hiddenItemsContainer div.indexItem').slice(Math.min(page_index * items_per_page), ((page_index + 1) * items_per_page))).clone();
    console.log(newcontent);

    // Replace old content with new content
    $('#itemsContainer').html(newcontent);

    return false;
}

但还有一件事......有没有办法将items_per_page值传递给回调函数?我必须将它保存在隐藏的字段中才有意义,因此我可以访问它...

0 个答案:

没有答案