1. 页码向上取整 Math.ceil([].length / 每页现实的数量)
    2. 动态修改 data,利用响应式原理,数组改变,组件自动重新渲染
    1. const [page, pageSize] = [1, 10];
    2. const startIndex = (page - 1) * pageSize; // 1-1 = 0 * 10 = 0; 2-1 = 1 * 10 = 10
    3. const data = data.slice(startIndex, startIndex + pageSize);

PageTable

前端 Table分页

  1. import React, { useState, useMemo, useEffect } from 'react';
  2. import { array, number } from 'prop-types';
  3. import TableList from './TableList';
  4. PageTable.propTypes = {
  5. dataSource: array.isRequired,
  6. current: number,
  7. pageSize: number,
  8. };
  9. PageTable.defaultProps = {
  10. current: 1,
  11. pageSize: 10,
  12. };
  13. function PageTable({ dataSource, current: _c, pageSize: _p, ...rest }) {
  14. const [query, setQuery] = useState({ current: _c, pageSize: _p });
  15. const [state, setState] = useState([]);
  16. const total = useMemo(
  17. () => {
  18. return Array.isArray(dataSource) ? dataSource.length : 0;
  19. },
  20. [dataSource]
  21. );
  22. useEffect(update, [dataSource, query]);
  23. function update() {
  24. const { current, pageSize } = query;
  25. const startIndex = (current - 1) * pageSize;
  26. const _state = dataSource.slice(startIndex, startIndex + pageSize);
  27. setState(_state);
  28. }
  29. function pageChange({ current, pageSize }) {
  30. setQuery({ current, pageSize });
  31. }
  32. return (
  33. <TableList
  34. {...rest}
  35. {...query}
  36. total={total}
  37. dataSource={state}
  38. onChange={pageChange}
  39. />
  40. );
  41. }
  42. export default PageTable;

前端分页原理

计算出数组的 startIndex, endIndex,
然后 slice截取当前区间的数据

  1. const startIndex = (page - 1) * pageSize;
  2. const endIndex = startIndex + pageSize;
  3. // 1-1 = 0 * 10 = 0, 0 + 10;
  4. // 2-1 = 1 * 10 = 10, 10 + 10;
  5. // 3-1 = 2 * 10 = 20, 20 + 10;
  6. // 4-1 = 3 * 10 = 30, 30 + 10;
  7. // 5-1 = 4 * 10 = 40, 40 + 10;
  8. // 6-1 = 5 * 10 = 50, 50 + 10;
  9. // 7-1 = 7 * 10 = 60, 60 + 10;

pagination

jquery 分页插件
https://pagination.js.org/
image.png