index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. jump,
  41. } = this.props;
  42. return (
  43. <div className={`user-table ${size} ${even} ${theme} ${border ? 'border' : ''}`}>
  44. <table>
  45. {header && (
  46. <thead>
  47. <tr>
  48. {columns.map((item, i) => {
  49. return (
  50. <th
  51. className={`${item.className || ''} ${i === 0 && select ? 'check' : ''}`}
  52. width={item.width}
  53. align={item.align}
  54. >
  55. {item.title}
  56. {item.fixSort &&
  57. (sortMap[item.key] ? (
  58. <Icon active name="arrow-down" onClick={() => this.onSort(item.key, '')} />
  59. ) : (
  60. <Icon name="arrow-up" onClick={() => this.onSort(item.key, 'desc')} />
  61. ))}
  62. {item.sort &&
  63. (sortMap[item.key] ? (
  64. <Assets
  65. name={sortMap[item.key] === 'asc' ? 'seqencing2_up_select' : 'seqencing2_down_select'}
  66. onClick={() => this.onSort(item.key, sortMap[item.key] === 'asc' ? 'desc' : '')}
  67. />
  68. ) : (
  69. <Assets name="seqencing2_normal" onClick={() => this.onSort(item.key, 'asc')} />
  70. ))}
  71. </th>
  72. );
  73. })}
  74. </tr>
  75. </thead>
  76. )}
  77. <tbody style={{ maxHeight }}>
  78. {data.map((row, i) => {
  79. const checked = selectList.indexOf(row[rowKey]) >= 0;
  80. const isEven = i % 2;
  81. if (row.children) {
  82. return [
  83. this.renderTr(row, checked, i, -1, isEven),
  84. ...row.children.map((item, index) => this.renderTr(item, checked, i, index, isEven)),
  85. ];
  86. }
  87. return this.renderTr(row, checked, i, -1, isEven);
  88. })}
  89. </tbody>
  90. </table>
  91. {data.length === 0 && <div className="empty">暂无数据</div>}
  92. {total > 0 && data.length > 0 && (
  93. <UserPagination jump={jump} total={total} pageSize={pageSize} current={current} onChange={onChange} />
  94. )}
  95. </div>
  96. );
  97. }
  98. renderTr(row, checked, rowIndex, childIndex, isEven) {
  99. const { columns } = this.props;
  100. return (
  101. <tr hidden={row.disabled} className={`${isEven ? 'even' : 'odd'}`}>
  102. {columns.map((item, i) => {
  103. return this.renderTd(row, checked, item, rowIndex, childIndex, i);
  104. })}
  105. </tr>
  106. );
  107. }
  108. renderTd(row, checked, item, index, childIndex, columnIndex) {
  109. const { select, rowKey = 'key' } = this.props;
  110. return (
  111. <td
  112. className={`${item.className || ''} ${columnIndex === 0 && select ? 'check' : ''}`}
  113. width={item.width}
  114. align={item.align}
  115. >
  116. {childIndex === -1 && columnIndex === 0 && select && (
  117. <CheckboxItem
  118. theme="white"
  119. checked={checked}
  120. onClick={value => this.onSelect(value, row[rowKey], row, rowKey)}
  121. />
  122. )}
  123. {item.render ? item.render(row[item.key], row, index, childIndex) : row[item.key]}
  124. </td>
  125. );
  126. }
  127. }