jQuery分页与切换帖子=问题

时间:2011-11-19 22:56:48

标签: jquery ajax post pagination toggle

我得到的反应不足让我相信这是不可能的......

所以有两个简单的概念:

1)点击分配给帖子的按钮将切换整个帖子(标题+内容),使其消失。

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

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

我想要发生的是,对于x个帖子的数量,下一个x的帖子数量会向上移动(第2页的最高x个帖子显示在第1页的底部,依此类推)。< / p>

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

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 :(得分: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