table.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import moment from 'moment';
  4. import classnames from 'classnames';
  5. import queryString from 'query-string';
  6. import { Modal, Table, Menu, Icon, Badge } from 'antd';
  7. import AnimTableBody from '../../../components/Animation/AnimTableBody';
  8. import styles from './table.less';
  9. import { statuses, Codes } from '../../../utils/config';
  10. export default class TableList extends PureComponent {
  11. handleOperateItem = (record) => {
  12. const { onDeleteItem, onRecoverItem } = this.props;
  13. Modal.confirm({
  14. title: `您确定要${record.status === Codes.CODE_NORMAL ? '删除' : '恢复'}该课程?`,
  15. onOk () {
  16. if (record.status === Codes.CODE_NORMAL) {
  17. onDeleteItem({id: record.id});
  18. } else if (record.status === Codes.CODE_DELETE) {
  19. onRecoverItem({ id: record.id, status: Codes.CODE_NORMAL });
  20. }
  21. },
  22. })
  23. }
  24. render() {
  25. const { curStatus, onDeleteItem, onRecoverItem, onEditItem, location, pagination, ...tableProps } = this.props;
  26. const columns = [{
  27. title: '课程编号',
  28. dataIndex: 'code',
  29. key: 'code',
  30. },{
  31. title: '课程名称',
  32. dataIndex: 'name',
  33. key: 'name',
  34. },{
  35. title: '状态',
  36. dataIndex: 'status',
  37. key: 'status',
  38. render: (text, record) => {
  39. const statusMap = {[Codes.CODE_NORMAL]: 'success', [Codes.CODE_DELETE]: 'error'};
  40. return (<Badge status={statusMap[record.status]} text={statuses[record.status]} />);
  41. },
  42. filters: Object.keys(statuses).map(key => ({ text: statuses[key], value: key })),
  43. filterMultiple: false,
  44. filteredValue: [curStatus],
  45. },{
  46. title: '添加时间',
  47. dataIndex: 'gmtCreated',
  48. key: 'gmtCreated',
  49. render: (text, record) => (
  50. <div>{moment(text).format('YYYY-MM-DD')}</div>
  51. )
  52. },{
  53. title: '操作',
  54. dataIndex: 'operation',
  55. key: 'operation',
  56. render: (text, record) => (
  57. <div>
  58. <a onClick={() => onEditItem(record)}>编辑</a>
  59. <span className={styles.splitLine} />
  60. <a onClick={() => this.handleOperateItem(record)}>{record.status === Codes.CODE_NORMAL ? '删除' : '恢复'}</a>
  61. </div>
  62. )
  63. }];
  64. // 数据table列表表头的筛选按钮点击重置后status值为空,此时删除该参数
  65. columns.map(item => {
  66. item.dataIndex === 'status' && !curStatus ? delete item.filteredValue : null;
  67. });
  68. // 配置分页
  69. tableProps.pagination = !!pagination && { ...pagination, showSizeChanger: true, showQuickJumper: true, showTotal: total => `共 ${total} 条`};
  70. // 添加动画
  71. const AnimationTableBody = (props) => (<AnimTableBody {...props}/>);
  72. return (
  73. <Table
  74. simple
  75. bordered
  76. { ...tableProps }
  77. columns={columns}
  78. className={classnames({ [styles.table]: true, [styles.motion]: true })}
  79. rowKey={record => record.id}
  80. components={{
  81. body: { wrapper: AnimationTableBody }
  82. }}
  83. />
  84. );
  85. }
  86. }