首页 文章

jQuery分页与切换帖子=问题

提问于
浏览
-1

我得到的回应缺乏让我相信这是不可能的......

所以两个简单的概念:

1)单击分配给帖子的按钮将切换整个帖子( Headers 内容),使其消失 .

2)我的分页采用class =“z”的所有div并对它们进行分页(每页显示10个div) .

我的问题是,如果我在第1页和切换(EX:切换3个帖子)帖子,页面不会保持每页10个帖子的常量 . 相反,它将在第1页上留下7个帖子 .

我想要发生的是,对于x量的帖子切换,下一个x的帖子数量将向上移动(第2页的最高x个帖子数量显示在第1页的底部,依此类推) .

我知道可以做到......我只是不知道怎么做!

toggle.js(查找带有class =“toggle”的div,单击时会发送一些ajax并在成功时切换div)

$(document).on("click", ".toggle", function(){
postID = $(this).attr('id').replace('toggle_', '');

// Declare variables
value = '0';

myajax();

return false;
});

function myajax(){
// Send values to database
$.ajax({
    url: 'check.php',
    //check.php receives the values sent to it and stores them in the database
    type: 'POST',
    data: 'postID=' + postID + '&value=' + value,
    success: function(result) {
         $('#post_' + postID).toggle();
                }
});
}

pagination.js

var Imtech = {};
Imtech.Pager = function() {
this.paragraphsPerPage = 3;
this.currentPage = 1;
this.pagingControlsContainer = '#pagingControls';
this.pagingContainerPath = '#contained';
this.numPages = function() {
var numPages = 0;
if (this.paragraphs != null && this.paragraphsPerPage != null) {
    numPages = Math.ceil(this.paragraphs.length / this.paragraphsPerPage);
}
return numPages;
};
this.showPage = function(page) {
this.currentPage = page;
var html = '';
this.paragraphs.slice((page-1) * this.paragraphsPerPage,
    ((page-1)*this.paragraphsPerPage) + this.paragraphsPerPage).each(function() {
    html += '<div>' + $(this).html() + '</div>';
});
$(this.pagingContainerPath).html(html);
renderControls(this.pagingControlsContainer, this.currentPage, this.numPages());
}
var renderControls = function(container, currentPage, numPages) {
var pagingControls = 'Page: <ul>';
for (var i = 1; i <= numPages; i++) {
    if (i != currentPage) {
        pagingControls += '<li><a href="#" onclick="pager.showPage(' + i + '); return false;">' + i + '</a></li>';
    } else {
        pagingControls += '<li>' + i + '</li>';
    }
}
pagingControls += '</ul>';
$(container).html(pagingControls);
}
}

1 回答

  • 0

    使用基于除法和模数除法的算法,如下所示:

    1. Read the page number requested from the user
    
    2. Identify how many total records exist
    
    3. Calculate the index of the last page
    
    4. Check whether the requested that page number is valid
    
    5. Calculate the range of records needed for the requested page
    
    6. Create a subset of records based on the calculated range
    
    7. Output the records along with updated hyperlinks
    

相关问题