Summary.tsx 2.27 KB
Newer Older
涂茜's avatar
涂茜 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// @ts-ignore
import React, { useEffect, useState } from 'react';
import Empty from '@wisdom-components/empty';
import { Table } from 'antd';
import BasicTable from '../index';
import request from 'umi-request';

const Demo = () => {
  const [columns, setColumns] = useState([]);
  const [dataSource, setDataSource] = useState([]);

  useEffect(() => {
    fetchData();
  }, []);

  const fetchData = (params = {}) => {
    request(`${baseUrl}/AcrossTable/GetEquipmentDataReport`, {
      method: 'get',
      params: {},
    }).then(function (response) {
      const data = response.data;
      let column = data.map((item, index) => {
        return {
          title: `${item.DName + item.DType}(${item.Unit})`,
          dataIndex: `name${index}`,
          key: `name${index}`,
          width: 300,
        };
      });
      column.unshift({
        title: '时间',
        dataIndex: 'time',
        key: 'time',
        fixed: true,
        width: 200,
36
        resize: true,
李纪文's avatar
李纪文 committed
37 38
        maxWidth: 250,
        minWidth: 100,
涂茜's avatar
涂茜 committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
      });
      let dataSource = data[0].NameDate.map((item, index) => ({ key: index, time: item.Time }));
      data.forEach((item, index) => {
        item.NameDate.forEach((child) => {
          dataSource.forEach((v) => {
            if (child.Time === v.time) v[`name${index}`] = child.Value;
          });
        });
      });
      setColumns(column);
      setDataSource(dataSource);
    });
  };

  const Summary = (currentData) => (
    <Table.Summary.Row>
      {columns.map((item, index) => {
        let sum = 0;
        currentData.reduce((prev, next) => {
          sum += next[item.key];
        }, 0);
        return (
          <Table.Summary.Cell key={index} index={index}>
            {index === 0 ? '总计' : sum}
          </Table.Summary.Cell>
        );
      })}
    </Table.Summary.Row>
  );

  // @ts-ignore
  return (
    <div style={{ height: '400px' }}>
      {!!dataSource.length && (
李纪文's avatar
李纪文 committed
73 74 75 76 77 78 79
        <BasicTable
          dataSource={dataSource}
          columns={columns}
          bordered
          summary={Summary}
          pagination={false}
        />
涂茜's avatar
涂茜 committed
80 81 82 83 84 85 86 87 88
      )}
      {!dataSource.length && <Empty description={'暂无数据'} />}
    </div>
  );
};

export default Demo;

const baseUrl = 'https://www.fastmock.site/mock/162c15dca15c4dba9ba51e0a0b76929b/api';