bblog icon indicating copy to clipboard operation
bblog copied to clipboard

超简单的分页

Open lishengzxc opened this issue 8 years ago • 0 comments
trafficstars

  function showPages(page, total) {
    var result = [page];
    for (var i = 1; i <= 3; i++) {
      if (page - i > 1) {
        result.unshift(page - i)
      }

      if (page + i < total) {
        result.push(page + i);
      }
    }

    if (page - 4 > 1) {
      result = ['...'].concat(result)
    }

    if (page > 1) {
      result = ['上一页', 1].concat(result)
    }

    if (page + 4 < total) {
      result.push('...');
    }

    if (page < total) {
      result = result.concat([total, '下一页'])
    }
    
    return result.join(' ');
  }

  // test
  var total = 110;  
  for (var i = 1; i <= total; i++) {  
    var ret = showPages(i, total);
    console.log(`${i}:`, ret);
  }

lishengzxc avatar Nov 25 '16 05:11 lishengzxc