2016-09-19 66 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