当前位置:   article > 正文

React+Antd实现表格自动向上滚动

React+Antd实现表格自动向上滚动

1、效果

2、环境

1、react18

2、antd 4+

3、代码实现

原理:创建一个定时器,修改表格ant-table-body的scrollTop属性实现滚动,监听表层的元素div的鼠标移入和移出实现实现鼠标进入元素滚动暂停,移出元素的时候表格滚动继续。

一、滚动组件实现如下,

  1. /**
  2. * 公共组件:表格滚动
  3. */
  4. import { Table } from 'antd';
  5. import { useEffect, useRef } from 'react';
  6. /**
  7. * 表格滚动组件
  8. * @param {Number} props.rollTime 表格每次滚动间隔时间 单位ms
  9. * @param {Number} props.rollNum 表格超过指定条数开始滚动
  10. * @param {Number} props.rollTop 表格每次滚动的高度 单位px
  11. * @param {Boolean} props.flag 是否滚动
  12. * @returns
  13. */
  14. const ScrollTable = (props: any) => {
  15. const {
  16. dataSource,
  17. rollTime = 100,
  18. rollNum = 10,
  19. rollTop = 2.5,
  20. flag = true,
  21. } = props;
  22. let timer: any = null;
  23. const tableContainer = useRef();
  24. // 开启定时器
  25. const initialScroll = (data: any) => {
  26. let container: any = tableContainer.current;
  27. container = container.getElementsByClassName('ant-table-body')[0];
  28. if (data.length > Number(rollNum) && flag) {
  29. // 只有当大于10条数据的时候 才会看起来滚动
  30. let time = setInterval(() => {
  31. container.scrollTop += Number(rollTop);
  32. if (
  33. Math.ceil(container.scrollTop) >=
  34. Number(container.scrollHeight - container.clientHeight)
  35. ) {
  36. container.scrollTop = 0;
  37. }
  38. }, Number(rollTime));
  39. timer = time;
  40. }
  41. };
  42. useEffect(() => {
  43. initialScroll(dataSource);
  44. return () => {
  45. clearInterval(timer);
  46. };
  47. }, []); // 检测数组内变量 如果为空 则监控全局
  48. return (
  49. <div
  50. onMouseOver={() => {
  51. clearInterval(timer);
  52. }}
  53. onMouseOut={() => {
  54. initialScroll(dataSource);
  55. }}
  56. >
  57. <Table
  58. rowKey="id"
  59. ref={tableContainer}
  60. pagination={false}
  61. scroll={{
  62. y: 500,
  63. x: '100%',
  64. scrollToFirstRowOnChange: true,
  65. }}
  66. {...props}
  67. />
  68. </div>
  69. );
  70. };
  71. export default ScrollTable;

二、调用该组件

  1. /**
  2. * 示例: 滚动表格示例
  3. */
  4. import ScrollTable from '@/components/ScrollTable';
  5. import clsx from 'clsx';
  6. const COLUMNS = [
  7. {
  8. dataIndex: 'index',
  9. valueType: 'indexBorder',
  10. width: 48,
  11. },
  12. {
  13. title: '姓名',
  14. dataIndex: 'name',
  15. key: 'name',
  16. },
  17. {
  18. title: '年龄',
  19. dataIndex: 'age',
  20. key: 'age',
  21. },
  22. {
  23. title: '住址',
  24. dataIndex: 'address',
  25. key: 'address',
  26. },
  27. ];
  28. const DATA_SOURCE = new Array(30).fill(0).map((item, index) => ({
  29. id: index + 1,
  30. name: `张三-${index}`,
  31. labels: `labels-${index}`,
  32. age: index,
  33. address: `武汉-${index}`,
  34. }));
  35. const ScrollTableExample = () => {
  36. return (
  37. <div className={clsx(['w-1/3', 'h-full', 'px-6', 'py-6', 'text-white'])}>
  38. <ScrollTable dataSource={DATA_SOURCE} columns={COLUMNS} />
  39. </div>
  40. );
  41. };
  42. export default ScrollTableExample;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/79694
推荐阅读
相关标签
  

闽ICP备14008679号