动态菜单和动态路由的 antd pro

index.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import React, { PureComponent, Fragment } from 'react';
  2. import { Table, Alert } from 'antd';
  3. import styles from './index.less';
  4. function initTotalList(columns) {
  5. const totalList = [];
  6. columns.forEach(column => {
  7. if (column.needTotal) {
  8. totalList.push({ ...column, total: 0 });
  9. }
  10. });
  11. return totalList;
  12. }
  13. class StandardTable extends PureComponent {
  14. constructor(props) {
  15. super(props);
  16. const { columns } = props;
  17. const needTotalList = initTotalList(columns);
  18. this.state = {
  19. selectedRowKeys: [],
  20. needTotalList,
  21. };
  22. }
  23. componentWillReceiveProps(nextProps) {
  24. // clean state
  25. if (nextProps.selectedRows.length === 0) {
  26. const needTotalList = initTotalList(nextProps.columns);
  27. this.setState({
  28. selectedRowKeys: [],
  29. needTotalList,
  30. });
  31. }
  32. }
  33. handleRowSelectChange = (selectedRowKeys, selectedRows) => {
  34. let needTotalList = [...this.state.needTotalList];
  35. needTotalList = needTotalList.map(item => {
  36. return {
  37. ...item,
  38. total: selectedRows.reduce((sum, val) => {
  39. return sum + parseFloat(val[item.dataIndex], 10);
  40. }, 0),
  41. };
  42. });
  43. if (this.props.onSelectRow) {
  44. this.props.onSelectRow(selectedRows);
  45. }
  46. this.setState({ selectedRowKeys, needTotalList });
  47. };
  48. handleTableChange = (pagination, filters, sorter) => {
  49. this.props.onChange(pagination, filters, sorter);
  50. };
  51. cleanSelectedKeys = () => {
  52. this.handleRowSelectChange([], []);
  53. };
  54. render() {
  55. const { selectedRowKeys, needTotalList } = this.state;
  56. const { data: { list, pagination }, loading, columns, rowKey } = this.props;
  57. const paginationProps = {
  58. showSizeChanger: true,
  59. showQuickJumper: true,
  60. ...pagination,
  61. };
  62. const rowSelection = {
  63. selectedRowKeys,
  64. onChange: this.handleRowSelectChange,
  65. getCheckboxProps: record => ({
  66. disabled: record.disabled,
  67. }),
  68. };
  69. return (
  70. <div className={styles.standardTable}>
  71. <div className={styles.tableAlert}>
  72. <Alert
  73. message={
  74. <Fragment>
  75. 已选择 <a style={{ fontWeight: 600 }}>{selectedRowKeys.length}</a> 项&nbsp;&nbsp;
  76. {needTotalList.map(item => (
  77. <span style={{ marginLeft: 8 }} key={item.dataIndex}>
  78. {item.title}总计&nbsp;
  79. <span style={{ fontWeight: 600 }}>
  80. {item.render ? item.render(item.total) : item.total}
  81. </span>
  82. </span>
  83. ))}
  84. <a onClick={this.cleanSelectedKeys} style={{ marginLeft: 24 }}>
  85. 清空
  86. </a>
  87. </Fragment>
  88. }
  89. type="info"
  90. showIcon
  91. />
  92. </div>
  93. <Table
  94. loading={loading}
  95. rowKey={rowKey || 'key'}
  96. rowSelection={rowSelection}
  97. dataSource={list}
  98. columns={columns}
  99. pagination={paginationProps}
  100. onChange={this.handleTableChange}
  101. />
  102. </div>
  103. );
  104. }
  105. }
  106. export default StandardTable;