所以,我正在通过Ajax接收一个JSON文件,并将其显示在表中。我想为那个阵列铺设。所以这就是我在做的
function Paginator(groups){
/**
* This should work.
* The children() function returns a JQuery object that contains the children.
* So you just need to check the size and see if it has at least one child.
* #grouplist > * makes it slightly faster - I think...
*/
if ( $('#grouplist > *').length > 0 ) {
$.each($('#grouplist').children(), function(i, current) {
current.remove();
});
}
//http://davidwalsh.name/javascript-clone-array
var clone_group = groups.slice(0);
var page = _globalpage,
startRec = Math.max(page - 1, 0) * _recPerPage,
endRec = Math.min(startRec + _recPerPage, clone_group.length)
recordsToShow = clone_group.splice(startRec, endRec);
console.log('start '+startRec+' end '+endRec+' page '+ page + ' records to show '+recordsToShow.length);
// loop through the array to populate your list
$.each(recordsToShow, function(i, currentGroup){
console.log('id '+currentGroup.id);
$('#grouplist').append('<tr> /*html with stuff I need*/ </tr>');
});
}
可能不是很好的做法,但是每个以下划线'_'开头的变量都是全球定义的。唯一更改的是_globalpage,每次点击"上一个"或"下一个"时会增加或减少1。我正在考虑将它们更改为Cookie,一旦我将其正常工作。
这是Console.log
的结果" 开始0结束10页1记录要显示10 "
" id 1 "通过" id 10 " "
击中下一个
" 开始10结束20页2记录要显示20 "
" id 11 " to" id 30 "
这怎么了?预先感谢
已解决。我通常在发布之前弄清楚... XP我什至不需要克隆数组,我只需要使用array.slice而不是array.splice
解决方案:
var page = _globalpage,
startRec = Math.max(page - 1, 0) * _recPerPage,
endRec = Math.min(startRec + _recPerPage, groups.length)
recordsToShow = groups.slice(startRec, endRec);
$.each(recordsToShow, function(i, currentGroup){ //print stuff
无论如何,
,互联网的集体精神甚至在窗帘后面也有所帮助。:p