"Front-end paging, two-dimensional array" data processing

Sometimes when we process data, we need to process it into the data format of front-end paging (two-dimensional array);

let Arr = [
  [],
  [],
  [],
  ...
]

Specific data processing method:

paging(list){
  let Arr = [],
      total = list.length,                     // 总条数
      pageSize = 10,                           // 每页显示条数
      totalPage = Math.ceil(total / pageSize), // 总页数
      sum = 0;                                 // 记录上一次循环的起点
  for(var j=0; j<totalPage; j++){
    Arr.push([]);
    let n = (j+1)*pageSize;   // 每页显示条数的倍数
    for(var i=0; i<total; i++){
      if( i<n && i>=sum ) Arr[j].push(list[i]);
    }
    sum = n;
  }
  return Arr
},

The result thrown by this method

Tips: If you want to encapsulate it into a front-end paging method, you only need to create corresponding variables and parameters (previous page, next page, first page, last page, click the corresponding page number to jump, search page number to jump), these parameters are actually They are all operating this two-dimensional array, and querying the page number is to find the corresponding (index value + 1) of the two-dimensional array; if you want to add a current page display number on the basis of this paging, you only need to pass pageSize in the form of parameters just pass in

Guess you like

Origin blog.csdn.net/qq_42660374/article/details/129588655