index.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import React from 'react';
  2. import { Table, Tag, Tooltip } from '../../../index';
  3. class InfiniteScrollDemo extends React.Component {
  4. constructor(props = {}) {
  5. super(props);
  6. this.state = {
  7. data: [],
  8. };
  9. this.columns = [
  10. {
  11. title: 'Name',
  12. dataIndex: 'name',
  13. width: 150,
  14. fixed: true,
  15. filters: [
  16. {
  17. text: 'Code 45',
  18. value: '45',
  19. },
  20. {
  21. text: 'King 4',
  22. value: 'King 4',
  23. },
  24. ],
  25. onFilter: (value, record) => record.name.includes(value),
  26. },
  27. {
  28. title: 'Age',
  29. dataIndex: 'age',
  30. width: 150,
  31. sorter: (a, b) => (a.age - b.age > 0 ? 1 : -1),
  32. },
  33. {
  34. title: 'Address',
  35. // width: 200,
  36. dataIndex: 'address',
  37. },
  38. {
  39. fixed: 'right',
  40. width: 250,
  41. render: (text, record) => (
  42. <Tooltip content={record.description}>
  43. <Tag color="green">Show Info</Tag>
  44. </Tooltip>
  45. ),
  46. },
  47. ];
  48. this.rowSelection = {
  49. fixed: true,
  50. };
  51. this.scroll = { y: 600, x: 1600 };
  52. this.style = { width: 800 };
  53. const itemSize = 56;
  54. this.virtualized = {
  55. itemSize,
  56. onScroll: ({ scrollDirection, scrollOffset, scrollUpdateWasRequested }) => {
  57. const { data } = this.state;
  58. if (
  59. scrollDirection === 'forward' &&
  60. scrollOffset >= (data.length - Math.ceil(this.scroll.y / itemSize) * 1.5) * itemSize &&
  61. !scrollUpdateWasRequested
  62. ) {
  63. this.loadMore();
  64. }
  65. },
  66. };
  67. this.loadMore = this.loadMore.bind(this);
  68. }
  69. loadMore() {
  70. const pageSize = 20; // load 20 records every time
  71. const data = [...this.state.data];
  72. const currentLenght = data.length;
  73. for (let i = currentLenght; i < currentLenght + pageSize; i++) {
  74. let age = (i * 1000) % 149 ;
  75. let name = `Edward King ${i}`;
  76. data.push({
  77. key: '' + i,
  78. name,
  79. age,
  80. address: `London, Park Lane no. ${i}`,
  81. description: `My name is ${name}, I am ${age} years old, living in New York No. ${i + 1} Lake Park.`,
  82. });
  83. }
  84. this.setState({ data });
  85. }
  86. componentDidMount() {
  87. this.loadMore();
  88. }
  89. render() {
  90. return (
  91. <Table
  92. pagination={false}
  93. columns={this.columns}
  94. dataSource={this.state.data}
  95. scroll={this.scroll}
  96. style={this.style}
  97. rowSelection={this.rowSelection}
  98. virtualized={this.virtualized}
  99. />
  100. );
  101. }
  102. }
  103. export default InfiniteScrollDemo;