index.js 4.2 KB

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