将SQL查询返回的记录拆分为一个数组
Splitting SQL query returned records into an array
嘿,我想把我返回的查询记录分割成一个"分页"类型的东西,因此我想显示前51条记录,然后将其余的添加到一个数组中,以便在用户想要移动到另一个页面时稍后获取。
我可以在页面上显示前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之后,它进入if (totalX>= 51) {,这就是我试图弄清楚如何将其余部分分割成51每个数组插槽。
上面的代码循环,直到它到达每个第三条记录,然后放置一个
之后,所以它有一行3条记录,然后它只是继续这样做,直到它达到记录51。 17行,每行3条记录。true告诉函数将
放在末尾,而false告诉函数不要将
on yet.
任何帮助将是伟大的!
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
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- JavaScript数组包含一个值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- jQuery$.inArray()总是返回-1和一个对象数组
- 在数组中的一个元素上设置多个值
- javascript处理一个对象数组以获得一个新的对象数组
- 作为一个二维数组,从ajax接收
- 你能用来自数组的属性名称生成一个对象吗
- 多维关联数组的最后一个索引
- 如何创建一个方法来验证数组的范围
- 循环以检查数组中的最后一个图像
- 在Javascript中将一个值和字符串数组转换为if语句
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 如何将一个对象添加到每个对象数组中
- 如何创建一个谷歌地图地理坐标数组
- 如何从另一个带下划线的数组中筛选带元素的数组
- 使用window.location.htm和匹配的URL数组(一个用于桌面,一个用于移动)将桌面网站重定向到移动
- Javascript排序多维数组-一个完整的例子
- 刽子手的游戏.2数组.一个需要相应地更新另一个