2011-02-18 77 views
2

我需要執行分頁我需要每次顯示5行。顯示前5行。在第二次點擊時,接下來的5,第三行沒有。 11-15,等等。我需要顯示即使至少有一行(在totalRows mod 5小於5的n次點擊之後)。我不明白這一點。Js分頁錯誤

function rightArrow() 
{ 
     pageCount = document.getElementById('pgCnt').value; //totalRows/5 
     totCount = document.getElementById('totCnt').value; //totalRows 
     currPage = tempRows - pageCount + 2; //current set of rows 
     document.getElementById('tempPage').value = tempPage; 
     var m = totCount%5; 

     if(pageCount != tempRows) 
      m = 5; 
     else 
     { m = (totCount % 5) - 1; 
      document.getElementById('rightArr').disabled = true; 
     } 

     document.getElementById('pgCnt').value = document.getElementById('pgCnt').value - 1; 

     for(i = 0; i < m;i++) 
     { 
      $.ajax({  
       type: "POST", 
       url: "getEODRow.php", 
       data: "page=" + currPage + "&row=" + i, 
       success: function(html) 
       { 
        var row = document.getElementById('chRecommend').insertRow(0); 
        temp = html.split(","); 
        for(j = 0; j < 9; j++) 
        { 
         str = temp[j].replace("\"",""); 
         str = temp[j].replace("\"",''); 
         str = temp[j].replace("[",''); 
         col = row.insertCell(j); 
         col.innerHTML = str; 
        } 
       } 
      }); 
     } 
     currPage++; 
} 
+3

...什麼_do_你? – 2011-02-18 14:55:34

回答

0

我意識到這個問題幾乎已經死了,但是我發現它找了別的東西,我想我會折騰一個答案。由於這不是一個關鍵問題,我繼續前進,重寫了你在這裏得到的所有內容以及許多你沒有選擇包含的代碼。

// App namespace 
var MyApp = {Paging: {}}; 

// Paging Controller 
MyApp.Paging.Controller = function(){ this.init.apply(this,arguments);}; 
MyApp.Paging.Controller.prototype = 
{ 
    // Initializer gets everything hooked up 
    init: function() 
    { 
     $.log("Initializing Paging Controller.") 
     // Get all the nodes we'll need 
     this.totCntNode = document.getElementById("totCnt"); 
     this.pgCntNode = document.getElementById("pgCnt"); 
     this.currMinNode = document.getElementById("currMin"); 
     this.currMaxNode = document.getElementById("currMax"); 
     this.prevPageButton = document.getElementById("prevPageButton"); 
     this.nextPageButton = document.getElementById("nextPageButton"); 

     this.pageContainer = document.getElementById("pageOfItems"); 
     // Mimic table's .insertRow to make row adding easy 
     this.pageContainer.insertRow = function() { 
      var row = document.createElement("div"); 
      row.className = "row clearfix"; 
      for (var i = 0; i < MyApp.Paging.Model.itemsPerRow; i++) 
      { 
       var cell = document.createElement("span"); 
       row.appendChild(cell); 
      } 
      this.appendChild(row); 
      return row; 
     }; 

     // Attach listeners to the next and previous buttons 
     this.prevPageButton.onclick = this.showPrevPage.bind(this); 
     this.nextPageButton.onclick = this.showNextPage.bind(this); 

     // Update the display for the first time 
     this.updatePageInfo(); 
    }, 

    // Run this whenever the model has changed and needs to update the display 
    updatePageInfo: function() 
    { 
     // Get info about what page we're on 
     var currentPage = MyApp.Paging.Model.currentPage, 
      totalPages = MyApp.Paging.Model.getTotalPages(), 
      itemCount = MyApp.Paging.Model.itemCount, 
      pageSize = MyApp.Paging.Model.getPageSize(), 
      rowsPerPage = MyApp.Paging.Model.rowsPerPage, 
      rowsOnThisPage = Math.ceil(MyApp.Paging.Model.getItemsOnPage(currentPage)/MyApp.Paging.Model.itemsPerRow); 

     // Clear out previous page data 
     while (this.pageContainer.children.length > 0) 
     { 
      this.pageContainer.removeChild(this.pageContainer.children[0]); 
     } 

     // Add space for the new page data 
     for (var rowInd = 0; rowInd < rowsOnThisPage ; rowInd++) 
     { 
      this.pageContainer.insertRow(); 
     } 

     $.log("Loading Page " + currentPage + "."); 

     // Request the data via ajax for each row 
     for(var i = 0; i < rowsOnThisPage ; i++) 
     { 
      $.ajax({  
       type: MyApp.Paging.Model.queryType, 
       url: MyApp.Paging.Model.queryURI, 
       data: MyApp.Paging.Model.getQueryData(currentPage, i), 
       success: function(pageNum, rowNum, result) 
       { 
        // Don't serve data from the wrong page 
        if (pageNum !== MyApp.Paging.Model.currentPage) return; 

        // When we get the data back, put it into the correct container 
        // regardless of when it was received 
        var row = this.pageContainer.children[rowNum], 
         temp = result.replace(/[\["]/g,"").split(","), 
         str = ""; 
        for(var j = 0; j < temp.length; j++) 
        { 
         row.children[j].innerHTML = temp[j]; 
        } 
       }.bind(this, currentPage, i) 
      }); 
     } 

     // Update the informational bits under the items 
     this.totCntNode.textContent = itemCount; 
     this.pgCntNode.textContent = totalPages; 
     var min = currentPage * (pageSize) + 1; 
     this.currMinNode.textContent = min; 
     this.currMaxNode.textContent = Math.min(min + pageSize - 1, itemCount); 

     // Disable the prev page button if there are no previous pages 
     if (currentPage <= 0) 
     { 
      if (this.prevPageButton.className.indexOf("disabled") < 0) 
      { 
       this.prevPageButton.className += " disabled"; 
      } 
     } 
     // Enable the prev page button if there are previous pages 
     else 
     { 
      if (this.prevPageButton.className.indexOf("disabled") > -1) 
      { 
       this.prevPageButton.className = this.prevPageButton.className.replace(/(?:^|\s+)disabled(?!\S)/g, ""); 
      } 
     } 

     // Disable the next page button if there are next pages 
     if (currentPage + 1 >= totalPages) 
     { 
      if (this.nextPageButton.className.indexOf("disabled") < 0) 
      { 
       this.nextPageButton.className += " disabled"; 
      } 
     } 
     // Enable the next page button if there are next pages 
     else 
     { 
      if (this.nextPageButton.className.indexOf("disabled") > -1) 
      { 
       this.nextPageButton.className = this.nextPageButton.className.replace(/(?:^|\s+)disabled(?!\S)/g, ""); 
      } 
     } 

    }, 

    // This is called when the next page button is clicked. 
    showNextPage: function() 
    { 
     if (MyApp.Paging.Model.currentPage + 1 >= MyApp.Paging.Model.getTotalPages()) 
     { 
      // Shouldn't have been able to activate this anyway 
     } 
     else 
     { 
      MyApp.Paging.Model.currentPage++; 

      this.updatePageInfo(); 
     } 
     return false; 
    }, 

    // This is called when the prev page button is clicked 
    showPrevPage: function() 
    { 
     if (MyApp.Paging.Model.currentPage <= 0) 
     { 
      // Shouldn't have been able to activate this anyway 
     } 
     else 
     { 
      MyApp.Paging.Model.currentPage--; 

      this.updatePageInfo(); 
     } 
     return false; 
    } 
}; 

// I typically expect an object like this to be created by the server and dropped dynamically onto the page 
MyApp.Paging.Model = { 
    itemCount: 140, 
    itemsPerRow: 9, 
    rowsPerPage: 5, 
    currentPage: 0, 

    queryType: "POST", 
    queryURI: "getEODRow.php", 
    queryDataFormat: "page={itemPage}&row={itemRow}", 

    getTotalPages: function() { 
     with(MyApp.Paging.Model) { 
      return Math.ceil(itemCount/(itemsPerRow*rowsPerPage)); 
     } 
    }, 
    getPageSize: function() { 
     with(MyApp.Paging.Model) { 
      return itemsPerRow * rowsPerPage; 
     } 
    }, 
    getItemsOnPage: function(pageNum) { 
     with(MyApp.Paging.Model) { 
      return Math.min(((pageNum+1) * getPageSize()), itemCount) - pageNum*getPageSize(); 
     } 
    }, 
    getItemsInRow: function(pageNum, rowNum) { 
     with(MyApp.Paging.Model) { 
      var onPage = getItemsOnPage(pageNum); 
      return Math.min((rowNum+1)*itemsPerRow, onPage) - rowNum*itemsPerRow; 
     } 
    }, 
    getQueryData: function(itemPage, itemRow) { 
     with(MyApp.Paging.Model) { 
      var data = queryDataFormat; 
      data = data.replace(/{itemPage}/gi, itemPage); 
      data = data.replace(/{itemRow}/gi, itemRow); 
      return data; 
     } 
    } 
}; 

所以,每當加載頁面時,有有載處理器或什麼的,你會創建並掛到呼叫控制器的單一實例。

MyApp.Paging.Controller.instance = new MyApp.Paging.Controller(); 

這應該處理與Ajax調用返回的無序以及那些網頁中的數據和部分行的部分頁面的問題。

演示:http://jsfiddle.net/2UJH8/8/