纯功能自定义表格(可自动滚动)

import React, { useEffect, useRef, useState } from 'react';
import { Row, Col } from 'antd';

interface CustomTableProps {
  /** 表头 */
  column: any;
  /** 数据 */
  dataSource: any;
  /** 表格内容样式 */
  dataClassName: any;
  /** 表头样式 */
  columnClassName: any;
  /** 表头背景图片 */
  src?: any;
  /** 表格内容间隔线颜色 */
  dividerColor?: string;
  /** 表格高度 */
  height?: string;
  /** 移动速度 */
  speed?: number;
}

const CustomTable = (props: CustomTableProps) => {
  const {
    column = [],
    dataSource = [],
    dataClassName,
    columnClassName,
    src,
    dividerColor = '1px solid #085261',
    height = '100%',
    speed,
  } = props;
  const ref1 = useRef<any>();
  const ref2 = useRef<any>();
  const data = [...dataSource];
  let time: any = 0;
  const [a, setA] = useState(0);
  const [childTran, setChildTran] = useState(0);
  const scrollTop1 = useRef(0);
  const autoScroll = () => {
    if (speed) {
      time = setInterval(() => {
        scrollTop1.current++;
        let childHeight = ref1?.current?.offsetHeight;
        if (scrollTop1.current >= (childHeight + 12) / 2) {
          scrollTop1.current = 0;
          setChildTran(scrollTop1.current);
          ref2.current.innerHTML = '';
          ref2.current.innerHTML = ref1.current.innerHTML;
        }
        setChildTran(scrollTop1.current);
        setA(time);
      }, speed);
    } else {
      return;
    }
  };

  useEffect(() => {
    if (speed) {
      ref2.current.innerHTML = ref1.current.innerHTML;
      autoScroll();
    }

    return () => {
      clearInterval(a);
    };
  }, [speed]);

  return (
    <div>
      {src && <img src={src} width="100%" height="auto" style={{ marginTop: 20 }} />}
      <Row className={columnClassName}>
        {column?.map((item: any) => {
          return <Col span={item?.span}>{item?.title}</Col>;
        })}
      </Row>
      <div
        style={{
          height: height,
          overflow: speed ? 'hidden' : 'auto',
        }}
        id="container"
      >
        <div
          ref={ref1}
          style={{ transform: `translateY(-${childTran}px)` }}
          onMouseOver={() => {
            clearInterval(a);
          }}
          onMouseLeave={() => {
            autoScroll();
          }}
          id="activeData"
        >
          {data?.map((item: any, index: number) => {
            return (
              <Row className={dataClassName}>
                {column?.map((it: any) => {
                  return (
                    <Col span={it.span}>
                      {it?.render
                        ? it?.render(item, item[it.dataIndex], index)
                        : item[it.dataIndex]}
                    </Col>
                  );
                })}
                <div
                  style={{
                    width: '100%',
                    height: 1,
                    border: dividerColor,
                  }}
                />
              </Row>
            );
          })}
          {speed && <div ref={ref2}></div>}
        </div>
      </div>
    </div>
  );
};

export default CustomTable;
// 示例
<CustomTable
  column={column}
  dataSource={data}
  dataClassName={S.tableData}
  columnClassName={S.TableColumn}
  dividerColor="transparent"
  height="192px"
  speed={50}
/>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值