admin管理员组

文章数量:1328568

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 badges
Add a ment  | 

3 Answers 3

Reset to default 7

Please 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