admin管理员组文章数量:1328357
I have an array in javascript with following data to be populated in the divs. The array is as follows
var array1 = new array('abc','def', 'ghi', 'jkl','mno');
I want to implement pagination for a div containing set of divs holding each with data of above array. i.e., div 1 will contain abc, div 2 will contain def and so on...
I am sending the page no clicked and maximum number of divs to be displayed in a page to a function. I am setting max_num of divs to be displayed is 2. The function is as follows.
function renderPagination(pageno, max_num){
for(var i=0;i< max_num; i++){
}
}
Here i have to run a loop where the set of divs to be populated. i.e. if pageno is '0', the abc, def divs should get seen. If pageno is '1', the ghi, jkl divs should get rendered. I think instead of i=0, I have to initialize to other variable.
I have an array in javascript with following data to be populated in the divs. The array is as follows
var array1 = new array('abc','def', 'ghi', 'jkl','mno');
I want to implement pagination for a div containing set of divs holding each with data of above array. i.e., div 1 will contain abc, div 2 will contain def and so on...
I am sending the page no clicked and maximum number of divs to be displayed in a page to a function. I am setting max_num of divs to be displayed is 2. The function is as follows.
function renderPagination(pageno, max_num){
for(var i=0;i< max_num; i++){
}
}
Here i have to run a loop where the set of divs to be populated. i.e. if pageno is '0', the abc, def divs should get seen. If pageno is '1', the ghi, jkl divs should get rendered. I think instead of i=0, I have to initialize to other variable.
Share Improve this question edited Aug 12, 2011 at 8:06 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Aug 12, 2011 at 8:01 BhaskarBhaskar 1631 gold badge4 silver badges7 bronze badges3 Answers
Reset to default 7Please use Array.Slice
function renderPagination(pageno, max_num) {
return array1.slice( (pageno - 1) * max_num, pageno * max_num );
}
Demo here: http://jsfiddle/naveen/Ctp99/
On an unrelated side-note, the Array
declaration should be new Array()
and not new array()
If you are asking how to initialise i, I believe you mean:
for(var i=pageno*max_num;i< max_num; i++){
}
Assuming pageno starts on 0, as per your text.
var array1 = ['abc','def', 'ghi', 'jkl','mno'];
function renderPagination(pageno, max_num){
max_num += pageno; // max_num will be max_num + pageno
// make sure max_num will not go out of bounds
if(max_num > array1.length){
max_num = array1.length;
}
for(pageno; pageno < max_num; pageno++){
console.log(array1[pageno]);
}
}
renderPagination(1, 2);
See a demo
With this solution, no matter where your page will start or what the max_num
will be, it will always return the correct pages, and it will never go out of array bounds.
本文标签: paginationJavascript loopsStack Overflow
版权声明:本文标题:pagination - Javascript loops - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742253506a2441198.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论