2011-11-19 75 views
-1

缺乏迴應我越來越讓我相信這是不可能的......jQuery的分頁與切換帖=問題

所以兩個簡單的概念:

1)單擊分配給後一個按鈕會切換整個帖子(標題+內容),使其消失。

2)我的分頁需要class =「z」的所有div,並對它們進行分頁(每頁顯示10 div)。

我的問題是,如果我在頁面1上切換(EX:切換3個帖子)帖子,頁面不會保持每頁10個帖子的常量。相反,它會在第1頁留下7個帖子。

我想要發生的事情是,對於x個帖子切換,下一個x的帖子數量將向上移動(第2頁上的帖子頂部×數量出現在頁面1的底部等)。

我知道它可以做到......我只是不知道如何!

toggle.js(尋找帶有class = 「切換」,單擊時,會發送一些AJAX和更迭一個div,將切換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); 
} 
} 
+0

我沒有任何地方改變ID。你在哪裏看到? – Sweepster

+0

對,錯過閱讀 – Hogan

+0

可能的重複[如何做很多頁面的頁面導航?對數頁面導航](http://stackoverflow.com/questions/7835752/how-to-do-page-navigation-for-many-many-pages-logarithmic-page-navigation) –

回答

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