2011-05-11 39 views
1

我有這個.js文件的HTML分頁任何人都可以幫助我,我必須改變,以顯示每個頁面的不同url &用戶可以保存該網址&可以使用它的未來用於進入該特定頁面。分頁與不同的網址爲每頁

(從下面.js文件開始)

function Pager(tableName, itemsPerPage) { 
this.tableName = tableName; 
this.itemsPerPage = itemsPerPage; 
this.currentPage = 1; 
this.pages = 0; 
this.inited = false; 

this.showRecords = function(from, to) {   
    var rows = document.getElementById(tableName).rows; 
    // i starts from 1 to skip table header row 
    for (var i = 1; i < rows.length; i++) { 
     if (i < from || i > to) 
      rows[i].style.display = 'none'; 
     else 
      rows[i].style.display = ''; 
    } 
} 

this.showPage = function(pageNumber) { 
    if (! this.inited) { 
     alert("not inited"); 
     return; 
    } 

    var oldPageAnchor = document.getElementById('pg'+this.currentPage); 
    oldPageAnchor.className = 'pg-normal'; 

    this.currentPage = pageNumber; 
    var newPageAnchor = document.getElementById('pg'+this.currentPage); 
    newPageAnchor.className = 'pg-selected'; 

    var from = (pageNumber - 1) * itemsPerPage + 1; 
    var to = from + itemsPerPage - 1; 
    this.showRecords(from, to); 
} 

this.prev = function() { 
    if (this.currentPage > 1) 
     this.showPage(this.currentPage - 1); 
} 

this.next = function() { 
    if (this.currentPage < this.pages) { 
     this.showPage(this.currentPage + 1); 
    } 
}       

this.init = function() { 
    var rows = document.getElementById(tableName).rows; 
    var records = (rows.length - 1); 
    this.pages = Math.ceil(records/itemsPerPage); 
    this.inited = true; 
} 

this.showPageNav = function(pagerName, positionId) { 
    if (! this.inited) { 
     alert("not inited"); 
     return; 
    } 
    var element = document.getElementById(positionId); 

    var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span> | '; 
    for (var page = 1; page <= this.pages; page++) 
     pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page + ');">' + page + '</span> | '; 
    pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';    

    element.innerHTML = pagerHtml; 
} 

}

+0

任何人都可以幫助我了嗎? – 2011-05-11 19:09:41

回答

3
this.showPage()更新的URL哈希的東西,如 「PG = 1」

location.hash = "#pg=" + pageNumber; 

這種方法將增加一個項目到你的頁面歷史。如果您不想將項目添加到您的網頁歷史記錄,使用:

location.replace("#pg=" + pageNumber); 

然後,加載頁面時,你叫pager.init()後,你要打電話pager.showPage()。您可以使用此代碼來決定是否加載第一頁或從網址頁面::

var page = /\bpg=(\d+)\b/.test(location.hash) ? parseInt(RegExp.$1) : 1; 
pager.showPage(page); 
+0

@ gilly3,感謝您的幫助,但在url中使用this(location.hash =「#pg =」+ pageNumber;),它爲每個頁面顯示頁面編號,但瀏覽器無法通過保存url&use來調用特定頁面它下一次。有任何解決這個問題? – 2011-05-11 18:45:45

+0

@Abdul - 我認爲在頁面加載時調用了Pager.init()。是對的嗎?你是否在'Pager.init()'中有RegExp測試和'showPage()'調用? – gilly3 2011-05-11 19:06:19

+0

是當頁面加載時調用'pager.init'調用。但我不知道RegExp測試&我也通過這些行在Pager.init像這樣this.init = function(){ \t \t var rows = document.getElementById(tableName).rows; var records =(rows.length - 1); this.pages = Math.ceil(records/itemsPerPage); this.inited = true; \t \t如果(/\bpg=(\d+)\b/.test(location.hash)) \t \t this.showPage(正則表達式$ 1)。 \t \t}' – 2011-05-11 19:38:18