Calculating item offset for pagination

前端 未结 9 2122
眼角桃花
眼角桃花 2020-12-12 20:53

this seems like very simple maths but somehow, my brain cant think ...

i am trying to implement pagination and will need to calculate the item offset to use in limi

9条回答
  •  -上瘾入骨i
    2020-12-12 20:59

      start = (page - 1) * itemsPerPage + 1
      end = totalItems
    
      if (itemsPerPage < totalItems) {
        end = itemsPerPage * page
        if (end > totalItems) {
          end = totalItems;
        }
      }
    
      // e.g. "21-30 of 193 items"
      start + '-' + end + ' of ' + totalItems + ' items'
    

提交回复
热议问题