index.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react';
  2. import { Table, Pagination, ButtonGroup, Button, Switch } from '../../../index';
  3. export default class RenderPagination extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. customPagination: true,
  8. pagination: { ...props.pagination, pageSize: 8 },
  9. };
  10. this.renderPagination = pagination => {
  11. return <Pagination {...pagination} />;
  12. };
  13. this.switchPagination = position => {
  14. let { pagination } = this.state;
  15. const positions = ['bottom', 'top', 'both'];
  16. if (position === true || position === false) {
  17. pagination = position ? { ...pagination } : false;
  18. } else if (positions.includes(position)) {
  19. pagination = { ...pagination, position };
  20. }
  21. this.setState({ pagination });
  22. };
  23. this.toggleRenderPagination = checked => {
  24. this.setState({ customPagination: checked });
  25. };
  26. this.TableSwitch = function TableSwitch({
  27. text,
  28. children,
  29. checked,
  30. onChange,
  31. style = { display: 'inline-flex', alignItems: 'center', margin: 5 },
  32. }) {
  33. const switchProps = { onChange };
  34. if (checked != null) {
  35. switchProps.checked = !!checked;
  36. }
  37. return (
  38. <span style={style}>
  39. <span>{text}</span>
  40. {children != null ? children : <Switch size="small" {...switchProps} />}
  41. </span>
  42. );
  43. };
  44. this.data = [
  45. {
  46. key: '1',
  47. name: 'John Brown',
  48. age: 32,
  49. address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
  50. },
  51. {
  52. key: '2',
  53. name: 'Jim Green',
  54. age: 42,
  55. address: 'London No. 1 Lake Park',
  56. },
  57. {
  58. key: '3',
  59. name: 'Joe Black',
  60. age: 32,
  61. address: 'Sidney No. 1 Lake Park',
  62. },
  63. {
  64. key: '4',
  65. name: 'Disabled User',
  66. age: 99,
  67. address: 'Sidney No. 1 Lake Park',
  68. },
  69. {
  70. key: '5',
  71. name: 'John Brown',
  72. age: 32,
  73. address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
  74. },
  75. {
  76. key: '6',
  77. name: 'Jim Green',
  78. age: 42,
  79. address: 'London No. 1 Lake Park',
  80. },
  81. {
  82. key: '7',
  83. name: 'Joe Black',
  84. age: 32,
  85. address: 'Sidney No. 1 Lake Park',
  86. },
  87. {
  88. key: '8',
  89. name: 'Disabled User',
  90. age: 99,
  91. address: 'Sidney No. 1 Lake Park',
  92. },
  93. {
  94. key: '9',
  95. name: 'John Brown',
  96. age: 32,
  97. address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
  98. },
  99. {
  100. key: '10',
  101. name: 'Jim Green',
  102. age: 42,
  103. address: 'London No. 1 Lake Park',
  104. },
  105. {
  106. key: '11',
  107. name: 'Joe Black',
  108. age: 32,
  109. address: 'Sidney No. 1 Lake Park',
  110. },
  111. {
  112. key: '12',
  113. name: 'Disabled User',
  114. age: 99,
  115. address: 'Sidney No. 1 Lake Park',
  116. },
  117. ];
  118. }
  119. render() {
  120. const TableSwitch = this.TableSwitch;
  121. const { customPagination, pagination } = this.state;
  122. return (
  123. <div>
  124. <div>
  125. <TableSwitch text="自定义渲染分页" checked={customPagination} onChange={this.toggleRenderPagination} />
  126. <TableSwitch text="分页器位置">
  127. <ButtonGroup>
  128. <Button onClick={() => this.switchPagination('bottom')}>Bottom</Button>
  129. <Button onClick={() => this.switchPagination('top')}>Top</Button>
  130. <Button onClick={() => this.switchPagination('both')}>Both</Button>
  131. <Button onClick={() => this.switchPagination(false)}>None</Button>
  132. </ButtonGroup>
  133. </TableSwitch>
  134. </div>
  135. <Table dataSource={this.data} pagination={pagination} renderPagination={customPagination ? this.renderPagination : null}>
  136. <Table.Column
  137. title="Name"
  138. dataIndex="name"
  139. key="name"
  140. render={(text, record, index) => <a>{text}</a>}
  141. />
  142. <Table.Column title="Age" dataIndex="age" key="age" />
  143. <Table.Column title="Address" dataIndex="address" key="address" />
  144. </Table>
  145. </div>
  146. );
  147. }
  148. }