index.js 3.6 KB
Newer Older
徐立's avatar
徐立 committed
1 2 3 4 5
import React, { Component, Fragment } from 'react';
import NormalTable from '../NormalTable';
import Index from '../Shell';

function initTotalList(columns) {
6 7 8 9 10 11 12
	const totalList = [];
	columns.forEach((column) => {
		if (column.needTotal) {
			totalList.push({ ...column, total: 0 });
		}
	});
	return totalList;
徐立's avatar
徐立 committed
13 14 15
}

class StandardTable extends Component {
16 17 18 19
	constructor(props) {
		super(props);
		const { columns } = props;
		const needTotalList = initTotalList(columns);
徐立's avatar
徐立 committed
20

21 22 23 24 25
		this.state = {
			selectedRowKeys: [],
			needTotalList,
		};
	}
徐立's avatar
徐立 committed
26

27 28 29 30 31 32 33 34 35 36 37
	static getDerivedStateFromProps(nextProps) {
		// clean state
		if (nextProps.selectedRows.length === 0) {
			const needTotalList = initTotalList(nextProps.columns);
			return {
				selectedRowKeys: [],
				needTotalList,
			};
		}
		return null;
	}
徐立's avatar
徐立 committed
38

39 40 41 42 43 44 45 46 47 48
	handleRowSelectChange = (selectedRowKeys, selectedRows) => {
		let { needTotalList } = this.state;
		needTotalList = needTotalList.map((item) => ({
			...item,
			total: selectedRows.reduce((sum, val) => sum + parseFloat(val[item.dataIndex], 10), 0),
		}));
		const { onSelectRow } = this.props;
		if (onSelectRow) {
			onSelectRow(selectedRows, selectedRowKeys);
		}
徐立's avatar
徐立 committed
49

50 51
		this.setState({ selectedRowKeys, needTotalList });
	};
徐立's avatar
徐立 committed
52

53 54 55 56 57 58
	handleTableChange = (pagination, filters, sorter) => {
		const { onChange } = this.props;
		if (onChange) {
			onChange(pagination, filters, sorter);
		}
	};
徐立's avatar
徐立 committed
59

60 61 62
	cleanSelectedKeys = () => {
		this.handleRowSelectChange([], []);
	};
徐立's avatar
徐立 committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76
	checkRowKey = () => {
	  const { rowKey,
      data: { list, pagination },
    } = this.props;
	  if(rowKey && list && list.length){
	    if(list[0][rowKey]){
	      return rowKey;
      }
    }
	  return 'id';
  }


77 78 79 80 81 82 83 84 85 86 87 88 89
	render() {
		const { selectedRowKeys, needTotalList } = this.state;
		const {
			data: { list, pagination },
			loading,
			columns,
			rowKey,
			components,
			scroll,
			noSelectRow,
			bordered,
			showHeader,
		} = this.props;
徐立's avatar
徐立 committed
90

91 92 93
		const paginationProps = {
			showSizeChanger: true,
			showQuickJumper: true,
钟是志's avatar
钟是志 committed
94
			pageSizeOptions: ['10', '20', '50', '100', '200'],
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
			...pagination,
		};
		if (typeof paginationProps.total === 'string') {
			paginationProps.total = Number(paginationProps.total) || 0;
		}
		const rowSelection = {
			selectedRowKeys,
			onChange: this.handleRowSelectChange,
			type: this.props.selectType || 'checkbox ',
			getCheckboxProps: (record) => ({
				disabled: record.disabled,
			}),
		};
		return (
			<div>
				<Index>
					{!noSelectRow ? (
						<span style={{ paddingLeft: '12px', paddingTop: '8px' }}>
							已选择
							{selectedRowKeys.length}
							{needTotalList.map((item) => (
								<span style={{ marginLeft: 8 }} key={item.dataIndex}>
									{item.title}
									总计&nbsp;
									<span style={{ fontWeight: 600 }}>
										{item.render ? item.render(item.total) : item.total}
									</span>
								</span>
							))}
							<a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
								清空
							</a>
						</span>
					) : (
						<div style={{ height: '1px' }} />
					)}
				</Index>
徐立's avatar
徐立 committed
132

133 134 135
				<NormalTable
					components={components}
					loading={loading}
136
          rowKey={this.checkRowKey()}
137 138 139 140
					rowSelection={typeof noSelectRow !== 'undefined' && noSelectRow ? null : rowSelection}
					dataSource={
						!!list &&
						list.map((x) => {
141
							return !!x.id
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
								? x
								: {
										id: Math.random() * 10000 + '',
										...x,
								  };
						})
					}
					bordered={bordered}
					columns={columns}
					size="small"
					scroll={scroll}
					showHeader={showHeader}
					pagination={this.props.isHiddenPage ? null : pagination === null ? null : paginationProps}
					onChange={this.handleTableChange}
				/>
			</div>
		);
	}
徐立's avatar
徐立 committed
160 161 162
}

export default StandardTable;