2016-09-19 99 views
-1

嘿,我想我返回的查詢記錄分割成「尋呼」型的東西,所以因此,我想顯示第一個51條記錄,然後添加其餘的到一個數組當用戶想要移動到另一個頁面時稍後獲取。分裂SQL查詢返回的記錄到一個數組

我可以顯示第51分的結果就好在頁面上,但我有找不到出路由51

到目前爲止我的代碼來劃分的記錄的其餘部分的麻煩:

var arrayHTML  = []; 

for(var key in data){ 
    if(data.hasOwnProperty(key)) { 
     if (totalX >= 51) { 
      //Start putting the results into an array. 51 results per array 
     }else if (x > 1) { 
      x = 0; 
      _userName = data[key].fname + ' ' + data[key].lname; 

      populateCards(_userName, 
          data[key].email, 
          data[key].img, 
          data[key].school, 
          data[key].userID, 
          true, 
          data[key].encoded); 
     } else { 
      _userName = data[key].fname + ' ' + data[key].lname; 

      populateCards(_userName, 
          data[key].email, 
          data[key].img, 
          data[key].school, 
          data[key].userID, 
          false, 
          data[key].encoded); 
      x++; 
     } 

     totalRowCnt = data[key].totalRows; 
     _tmpMath = Math.round(totalRowCnt/totalNum); 
     total++; 
     totalX++; 

     console.log('totalX: ' + totalX) 
    } 
} 

它之後打51就進入如果(totalX> = 51){而這正是我試圖弄清楚如何去分割剩下成51%陣列插槽。

上面的代碼中循環,直到它到達每3記錄,然後後放置一個< BR />所以它有3條記錄一行,然後它只是不斷這樣做,直到它達到創紀錄的51。 所以17行每行3條記錄。真正告訴函數把< BR />就結束,而說的是功能不穿上< BR />呢。

任何幫助將是偉大的!

回答

0

JavaScript代碼下面:

添加此功能代碼:

// this is a simple pager function that return part of the array you want 
// so you can easily loop over it 
// @param page you want 
// @param how many values you want 
// @return the sliced array with the parts you want 
//  (this will return an empty array if your page is out of bound 
//  e.g. when you array only contains less than 51 and you tell it you want page 2) 
array_pager = function (array, page, show) { 
    var start = (page -1) * show; // this sets the offset 
    return array.slice(start, start + show); 
} 

而且你可以使用這個功能是這樣的:

// assuming the array's name is data 
var this_page = array_pager(data, 1, 51); // get this_page 1, with 51 values 

// then you can safely loop over it 
for(var key in this_page) { 
    _userName = this_page[key].fname + ' ' + this_page[key].lname; 

    populateCards(_userName, 
       this_page[key].email, 
       this_page[key].img, 
       this_page[key].school, 
       this_page[key].userID, 
       false, 
       this_page[key].encoded); 
} 

// for page 2: array_pager(data, 2, 51) ... 
// 10 per page: array_pager(data, 1, 10) ... I think you get it now