index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import React, { Component } from 'react';
  2. import './index.less';
  3. import Assets from '@src/components/Assets';
  4. import CheckboxItem from '../CheckboxItem';
  5. import Icon from '../Icon';
  6. import UserPagination from '../UserPagination';
  7. export default class UserTable extends Component {
  8. onSort(key, value) {
  9. const { sortMap = {}, onSort } = this.props;
  10. sortMap[key] = value;
  11. if (onSort) onSort(sortMap);
  12. }
  13. onSelect(checked, key) {
  14. const { selectList = [] } = this.props;
  15. if (checked) {
  16. selectList.push(key);
  17. } else {
  18. selectList.splice(selectList.indexOf(key), 1);
  19. }
  20. if (this.props.onSelect) this.props.onSelect(selectList, key, checked);
  21. }
  22. render() {
  23. const {
  24. columns = [],
  25. rowKey = 'key',
  26. data = [],
  27. select,
  28. selectList = [],
  29. current,
  30. total,
  31. pageSize,
  32. size = 'basic',
  33. even = 'even',
  34. theme = 'defalut',
  35. border = true,
  36. header = true,
  37. sortMap = {},
  38. maxHeight,
  39. onChange,
  40. } = this.props;
  41. return (
  42. <div className={`user-table ${size} ${even} ${theme} ${border ? 'border' : ''}`}>
  43. <table>
  44. {header && (
  45. <thead>
  46. <tr>
  47. {columns.map((item, i) => {
  48. return (
  49. <th
  50. className={`${item.className || ''} ${i === 0 && select ? 'check' : ''}`}
  51. width={item.width}
  52. align={item.align}
  53. >
  54. {item.title}
  55. {item.fixSort &&
  56. (sortMap[item.key] ? (
  57. <Icon active name="arrow-down" onClick={() => this.onSort(item.key, '')} />
  58. ) : (<Icon name="arrow-up" onClick={() => this.onSort(item.key, 'desc')} />))}
  59. {item.sort &&
  60. (sortMap[item.key] ? (
  61. <Assets
  62. name={sortMap[item.key] === 'asc' ? 'seqencing2_up_select' : 'seqencing2_down_select'}
  63. onClick={() => this.onSort(item.key, sortMap[item.key] === 'asc' ? 'desc' : '')}
  64. />
  65. ) : (<Assets name="seqencing2_normal" onClick={() => this.onSort(item.key, 'asc')} />))}
  66. </th>
  67. );
  68. })}
  69. </tr>
  70. </thead>
  71. )}
  72. <tbody style={{ maxHeight }}>
  73. {data.map((row, i) => {
  74. const checked = selectList.indexOf(row[rowKey]) >= 0;
  75. const isEven = i % 2;
  76. if (row.children) {
  77. return [
  78. this.renderTr(row, checked, i, -1, isEven),
  79. ...row.children.map((item, index) => this.renderTr(item, checked, i, index, isEven)),
  80. ];
  81. }
  82. return this.renderTr(row, checked, i, -1, isEven);
  83. })}
  84. </tbody>
  85. </table>
  86. {data.length === 0 && <div className="empty">暂无数据</div>}
  87. {total > 0 && data.length > 0 && <UserPagination total={total} pageSize={pageSize} current={current} onChange={onChange} />}
  88. </div>
  89. );
  90. }
  91. renderTr(row, checked, rowIndex, childIndex, isEven) {
  92. const { columns } = this.props;
  93. return (
  94. <tr hidden={row.disabled} className={`${isEven ? 'even' : 'odd'}`}>
  95. {columns.map((item, i) => {
  96. return this.renderTd(row, checked, item, rowIndex, childIndex, i);
  97. })}
  98. </tr>
  99. );
  100. }
  101. renderTd(row, checked, item, index, childIndex, columnIndex) {
  102. const { select, rowKey = 'key' } = this.props;
  103. return (
  104. <td
  105. className={`${item.className || ''} ${columnIndex === 0 && select ? 'check' : ''}`}
  106. width={item.width}
  107. align={item.align}
  108. >
  109. {childIndex === -1 && columnIndex === 0 && select && (
  110. <CheckboxItem theme="white" checked={checked} onClick={value => this.onSelect(value, row[rowKey], row, rowKey)} />
  111. )}
  112. {item.render ? item.render(row[item.key], row, index, childIndex) : row[item.key]}
  113. </td>
  114. );
  115. }
  116. }