|
| 1 | +import React, { useRef, useState } from 'react'; |
| 2 | +import { Button } from 'antd'; |
| 3 | +import { SpreadSheet } from 'dt-react-component'; |
| 4 | + |
| 5 | +export default () => { |
| 6 | + const _columns = ['name', 'gender', 'age', 'address']; |
| 7 | + const _data = [ |
| 8 | + ['zhangsan', 'male', '20', 'xihu'], |
| 9 | + ['lisi', 'male', '18', 'yuhang'], |
| 10 | + [' 前面有空格', '后面有空格 ', '中间有 空 格', 'yuhang'], |
| 11 | + ]; |
| 12 | + const [columns, setColumns] = useState(_columns); |
| 13 | + const [data, setData] = useState(_data); |
| 14 | + const hotTableInstanceRef = useRef<any>(null); |
| 15 | + |
| 16 | + const handleData = () => { |
| 17 | + setData(data?.length === 2 ? _data : _data.slice(0, 2)); |
| 18 | + }; |
| 19 | + |
| 20 | + const handleColumns = () => { |
| 21 | + setColumns(columns?.length === 3 ? _columns : _columns.slice(0, 3)); |
| 22 | + }; |
| 23 | + |
| 24 | + const handleRef = () => { |
| 25 | + console.log(hotTableInstanceRef?.current?.hotInstance?.getData()); |
| 26 | + }; |
| 27 | + |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <Button style={{ margin: '0 12px 12px 0' }} type="primary" onClick={handleColumns}> |
| 31 | + 改变列 |
| 32 | + </Button> |
| 33 | + <Button style={{ margin: '0 12px 12px 0' }} type="primary" onClick={handleData}> |
| 34 | + 改变数据 |
| 35 | + </Button> |
| 36 | + <Button style={{ margin: '0 12px 12px 0' }} type="primary" onClick={handleRef}> |
| 37 | + 使用 HotTable 示例(打印数据) |
| 38 | + </Button> |
| 39 | + |
| 40 | + <SpreadSheet |
| 41 | + ref={hotTableInstanceRef} |
| 42 | + columns={columns} |
| 43 | + data={data} |
| 44 | + options={{ |
| 45 | + trimWhitespace: false, |
| 46 | + copyTypes: ['copyData', 'copyHeaders', 'copyHeadersAndData'], |
| 47 | + }} |
| 48 | + /> |
| 49 | + </> |
| 50 | + ); |
| 51 | +}; |
0 commit comments