JSXColumnsComplex.jsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React from 'react';
  2. import { Switch, Table, Dropdown, Icon } from '@douyinfe/semi-ui';
  3. import { IconCaretdown } from '@douyinfe/semi-icons';
  4. const CREATOR_MAP = {
  5. ALL: {
  6. value: 0,
  7. desc: '创建者',
  8. },
  9. MINE: {
  10. value: 1,
  11. desc: '只看我的',
  12. },
  13. };
  14. export default class JSXColumnsComplex extends React.Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. showName: true,
  19. showAge: true,
  20. showAddress: true,
  21. showCreator: true,
  22. currentCreator: {},
  23. };
  24. this.data = [];
  25. for (let i = 0; i < 46; i++) {
  26. this.data.push({
  27. key: `${i}`,
  28. name: `Edward King ${i}`,
  29. age: (i * 1000) % 149 ,
  30. address: `London, Park Lane no. ${i}`,
  31. });
  32. }
  33. this.columns = [
  34. {
  35. width: 150,
  36. onFilter: (value, record) => record.name.includes(value),
  37. filters: [
  38. {
  39. text: 'Code 45',
  40. value: '45',
  41. },
  42. {
  43. text: 'King 4',
  44. value: 'King 4',
  45. },
  46. ],
  47. title: 'Name',
  48. dataIndex: 'name',
  49. render: (text, record, index) => <a>{text}</a>,
  50. },
  51. {
  52. title: <span>Age</span>,
  53. dataIndex: 'age',
  54. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  55. },
  56. {
  57. title: (
  58. <Dropdown
  59. position={'bottomLeft'}
  60. render={(
  61. <Dropdown.Menu>
  62. <Dropdown.Item onClick={this.setCreator.bind(this, 'ALL')}>创建者</Dropdown.Item>
  63. <Dropdown.Item onClick={this.setCreator.bind(this, 'MINE')}>只看我的</Dropdown.Item>
  64. </Dropdown.Menu>
  65. )}
  66. >
  67. <span>{this.state.currentCreator.desc}</span>
  68. <Icon type={<IconCaretdown />} />
  69. </Dropdown>
  70. ),
  71. key: 'creator',
  72. width: 168,
  73. },
  74. {
  75. title: 'Address',
  76. dataIndex: 'address',
  77. },
  78. ];
  79. }
  80. setCreator(type) {
  81. this.setState({
  82. currentCreator: { ...CREATOR_MAP[type] },
  83. });
  84. }
  85. toggleStatus = (type, status) => {
  86. this.setState({
  87. [type]: status,
  88. });
  89. };
  90. render() {
  91. const { showAddress, showAge, showName, showCreator, currentCreator } = this.state;
  92. return (
  93. <div>
  94. <div>
  95. <div>
  96. 显示名称
  97. <Switch checked={showName} onChange={v => this.toggleStatus('showName', v)} />
  98. </div>
  99. <div>
  100. 显示年龄
  101. <Switch checked={showAge} onChange={v => this.toggleStatus('showAge', v)} />
  102. </div>
  103. <div>
  104. 显示创建者
  105. <Switch checked={showCreator} onChange={v => this.toggleStatus('showCreator', v)} />
  106. </div>
  107. <div>
  108. 显示地址
  109. <Switch checked={showAddress} onChange={v => this.toggleStatus('showAddress', v)} />
  110. </div>
  111. </div>
  112. <Table
  113. dataSource={this.data} // columns={this.columns}
  114. >
  115. {showName ? (
  116. <Table.Column
  117. width={150}
  118. onFilter={(value, record) => record.name.includes(value)}
  119. filters={[
  120. {
  121. text: 'Code 45',
  122. value: '45',
  123. },
  124. {
  125. text: 'King 4',
  126. value: 'King 4',
  127. },
  128. ]}
  129. title="Name"
  130. dataIndex="name"
  131. render={(text, record, index) => <a>{text}</a>}
  132. />
  133. ) : null}
  134. {showAge ? (
  135. <Table.Column
  136. title={<span>Age</span>}
  137. dataIndex="age"
  138. sorter={(a, b) => (a.age - b.age > 0 ? 1 : -1)}
  139. />
  140. ) : null}
  141. {showCreator ? (
  142. <Table.Column
  143. title={(
  144. <Dropdown
  145. position={'bottomLeft'}
  146. render={(
  147. <Dropdown.Menu>
  148. <Dropdown.Item onClick={this.setCreator.bind(this, 'ALL')}>
  149. 创建者
  150. </Dropdown.Item>
  151. <Dropdown.Item onClick={this.setCreator.bind(this, 'MINE')}>
  152. 只看我的
  153. </Dropdown.Item>
  154. </Dropdown.Menu>
  155. )}
  156. >
  157. <span>{currentCreator.desc}</span>
  158. <Icon type={<IconCaretdown />} />
  159. </Dropdown>
  160. )}
  161. key={'creator'}
  162. />
  163. ) : null}
  164. {showAddress ? <Table.Column title="Address" dataIndex="address" /> : null}
  165. </Table>
  166. </div>
  167. );
  168. }
  169. }