table.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 { Popover, Modal, Table, Menu, Icon, Badge } from 'antd';
  7. import AnimTableBody from '../../../components/Animation/AnimTableBody';
  8. import { statuses, quality, Codes } from '../../../utils/config'
  9. import styles from './table.less';
  10. const confirm = Modal.confirm;
  11. export default class TableList extends PureComponent {
  12. static propTypes = {
  13. location: PropTypes.object,
  14. onChange: PropTypes.func.isRequired,
  15. onDeleteItem: PropTypes.func.isRequired,
  16. onPlayVideo: PropTypes.func.isRequired,
  17. };
  18. handleDeleteItem = (record) => {
  19. const { onDeleteItem } = this.props;
  20. confirm({
  21. title: `您确定要${record.status === Codes.CODE_NORMAL ? '删除' : '恢复'}该视频?`,
  22. onOk () {
  23. onDeleteItem(record.id);
  24. },
  25. })
  26. }
  27. render() {
  28. const { curStatus, onPlayVideo, location, pagination, ...tableProps } = this.props;
  29. const columns = [{
  30. title: '视频编号',
  31. dataIndex: 'code',
  32. key: 'code',
  33. },{
  34. title: '视频名称',
  35. dataIndex: 'name',
  36. key: 'name',
  37. },{
  38. title: '视频格式',
  39. dataIndex: 'format',
  40. key: 'format',
  41. },{
  42. title: '视频质量',
  43. dataIndex: 'quality',
  44. key: 'quality',
  45. render: (text, record) => quality[text],
  46. },{
  47. title: '状态',
  48. dataIndex: 'status',
  49. key: 'status',
  50. render: (text, record) => {
  51. const statusMap = {[Codes.CODE_NORMAL]: 'success', [Codes.CODE_DELETE]: 'error'};
  52. return (<Badge status={statusMap[record.status]} text={statuses[record.status]} />);
  53. },
  54. filters: Object.keys(statuses).map(key => ({ text: statuses[key], value: key })),
  55. filterMultiple: false,
  56. filteredValue: [curStatus],
  57. },{
  58. title: '添加时间',
  59. dataIndex: 'gmtCreated',
  60. key: 'gmtCreated',
  61. render: (text, record) => (
  62. <div>{moment(text).format('YYYY-MM-DD')}</div>
  63. )
  64. },{
  65. title: '操作',
  66. dataIndex: 'operation',
  67. key: 'operation',
  68. render: (text, record) => (
  69. <div>
  70. <a onClick={() => onPlayVideo(record)}>播放</a>
  71. <span className={styles.splitLine} />
  72. <a onClick={() => this.handleDeleteItem(record)}>{record.status === Codes.CODE_NORMAL ? '删除' : '恢复'}</a>
  73. </div>
  74. )
  75. }];
  76. // 数据table列表表头的筛选按钮点击重置后status值为空,此时删除该参数
  77. columns.map(item => {
  78. item.dataIndex === 'status' && !curStatus ? delete item.filteredValue : null;
  79. });
  80. tableProps.pagination = !!pagination && { ...pagination, showSizeChanger: true, showQuickJumper: true, showTotal: total => `共 ${total} 条`};
  81. const getBodyWrapperProps = {
  82. page: location.query.page,
  83. current: tableProps.pagination.current,
  84. };
  85. const getBodyWrapper = (body) => (<AnimTableBody {...getBodyWrapperProps} body={body} />);
  86. // 视频返回的数据量每页不固定,会出现分页bug,这里截取与pageSize相等
  87. const pageSize = tableProps.pagination.pageSize;
  88. const newSource = tableProps.dataSource.slice(0, pageSize);
  89. return (
  90. <Table
  91. simple
  92. bordered
  93. { ...tableProps }
  94. columns={columns}
  95. className={classnames({ [styles.table]: true, [styles.motion]: true })}
  96. rowKey={record => record.id}
  97. getBodyWrapper={getBodyWrapper}
  98. dataSource={newSource}
  99. />
  100. );
  101. }
  102. }