javascript pagination large array?

2.5k views Asked by At

i have array has over 10K records i need to paginate it , I've found a cool function but its work only in foreground in background i got Error undefined _

for example :

var myArray = bla.GetInfo(); //has over 10K records

var page1 =  getPaginatedItems(myArray, 1 , 500 );

//this function working fine only in foreground 
function getPaginatedItems(items, page , per_page ) {

    var page           = page || 1,
        offset         = (page - 1) * per_page,
        paginatedItems = _.rest(items, offset).slice(0, per_page);

    return { page        : page,
             per_page    : per_page,
             total       : items.length,
             total_pages : Math.ceil(items.length / per_page),
             data        : paginatedItems
           };
}

i think the problem from this method _.rest any ideas how make this function work in background ? if not dose anyone has similar function do same job ! without underscore methods ?

1

There are 1 answers

0
Sadanand Masurkar On

set a per page record here 4

      var array = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];
var record_per_page = 4;
var page = 0; //when page > 0 then page = pagi   
var pagi = record_per_page; // when page > 0 then pagi = pagi + record_per_page   

var paginate_array = array.slice(page,pagi);

console.log(paginate_array);