2016-11-02 15:09:54 +08:00
|
|
|
---
|
2019-09-03 18:47:17 +08:00
|
|
|
order: 100
|
2016-11-02 15:09:54 +08:00
|
|
|
title:
|
|
|
|
en-US: Dynamic Settings
|
|
|
|
zh-CN: 动态控制表格属性
|
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
选择不同配置组合查看效果。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
|
|
|
Select different settings to see the result.
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
```tsx
|
2019-11-28 12:34:33 +08:00
|
|
|
import { DownOutlined } from '@ant-design/icons';
|
2022-05-19 09:46:26 +08:00
|
|
|
import type { RadioChangeEvent } from 'antd';
|
2022-05-21 22:14:15 +08:00
|
|
|
import { Form, Radio, Space, Switch, Table } from 'antd';
|
2022-07-04 22:09:54 +08:00
|
|
|
import type { SizeType } from 'antd/es/config-provider/SizeContext';
|
|
|
|
import type { ColumnsType, TableProps } from 'antd/es/table';
|
|
|
|
import type { ExpandableConfig, TableRowSelection } from 'antd/es/table/interface';
|
2022-05-21 22:14:15 +08:00
|
|
|
import React, { useState } from 'react';
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
interface DataType {
|
|
|
|
key: number;
|
|
|
|
name: string;
|
|
|
|
age: number;
|
|
|
|
address: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
type TablePaginationPosition =
|
|
|
|
| 'topLeft'
|
|
|
|
| 'topCenter'
|
|
|
|
| 'topRight'
|
|
|
|
| 'bottomLeft'
|
|
|
|
| 'bottomCenter'
|
|
|
|
| 'bottomRight';
|
|
|
|
|
|
|
|
const columns: ColumnsType<DataType> = [
|
2019-05-07 14:57:32 +08:00
|
|
|
{
|
|
|
|
title: 'Name',
|
|
|
|
dataIndex: 'name',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Age',
|
|
|
|
dataIndex: 'age',
|
2020-04-03 13:42:57 +08:00
|
|
|
sorter: (a, b) => a.age - b.age,
|
2019-05-07 14:57:32 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Address',
|
|
|
|
dataIndex: 'address',
|
2020-04-03 13:42:57 +08:00
|
|
|
filters: [
|
|
|
|
{
|
|
|
|
text: 'London',
|
|
|
|
value: 'London',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'New York',
|
|
|
|
value: 'New York',
|
|
|
|
},
|
|
|
|
],
|
2022-05-19 09:46:26 +08:00
|
|
|
onFilter: (value, record) => record.address.indexOf(value as string) === 0,
|
2019-05-07 14:57:32 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Action',
|
|
|
|
key: 'action',
|
2020-04-03 13:42:57 +08:00
|
|
|
sorter: true,
|
2019-11-22 15:59:32 +08:00
|
|
|
render: () => (
|
2020-08-04 17:49:31 +08:00
|
|
|
<Space size="middle">
|
|
|
|
<a>Delete</a>
|
2022-04-22 13:35:27 +08:00
|
|
|
<a>
|
|
|
|
<Space>
|
|
|
|
More actions
|
|
|
|
<DownOutlined />
|
|
|
|
</Space>
|
2019-05-07 14:57:32 +08:00
|
|
|
</a>
|
2020-08-04 17:49:31 +08:00
|
|
|
</Space>
|
2019-05-07 14:57:32 +08:00
|
|
|
),
|
|
|
|
},
|
|
|
|
];
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const data: DataType[] = [];
|
2016-11-09 20:16:57 +08:00
|
|
|
for (let i = 1; i <= 10; i++) {
|
|
|
|
data.push({
|
|
|
|
key: i,
|
|
|
|
name: 'John Brown',
|
2022-05-19 09:46:26 +08:00
|
|
|
age: Number(`${i}2`),
|
2016-11-09 20:16:57 +08:00
|
|
|
address: `New York No. ${i} Lake Park`,
|
|
|
|
description: `My name is John Brown, I am ${i}2 years old, living in New York No. ${i} Lake Park.`,
|
|
|
|
});
|
|
|
|
}
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const defaultExpandable = { expandedRowRender: (record: DataType) => <p>{record.description}</p> };
|
|
|
|
const defaultTitle = () => 'Here is title';
|
|
|
|
const defaultFooter = () => 'Here is footer';
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const App: React.FC = () => {
|
|
|
|
const [bordered, setBordered] = useState(false);
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const [size, setSize] = useState<SizeType>('large');
|
|
|
|
const [expandable, setExpandable] = useState<ExpandableConfig<DataType> | undefined>(
|
|
|
|
defaultExpandable,
|
|
|
|
);
|
|
|
|
const [showTitle, setShowTitle] = useState(false);
|
|
|
|
const [showHeader, setShowHeader] = useState(true);
|
|
|
|
const [showfooter, setShowFooter] = useState(true);
|
|
|
|
const [rowSelection, setRowSelection] = useState<TableRowSelection<DataType> | undefined>({});
|
|
|
|
const [hasData, setHasData] = useState(true);
|
|
|
|
const [tableLayout, setTableLayout] = useState(undefined);
|
|
|
|
const [top, setTop] = useState<TablePaginationPosition | 'none'>('none');
|
|
|
|
const [bottom, setBottom] = useState<TablePaginationPosition>('bottomRight');
|
|
|
|
const [ellipsis, setEllipsis] = useState(false);
|
|
|
|
const [yScroll, setYScroll] = useState(false);
|
|
|
|
const [xScroll, setXScroll] = useState<string | undefined>(undefined);
|
|
|
|
|
|
|
|
const handleBorderChange = (enable: boolean) => {
|
|
|
|
setBordered(enable);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleLoadingChange = (enable: boolean) => {
|
|
|
|
setLoading(enable);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleSizeChange = (e: RadioChangeEvent) => {
|
|
|
|
setSize(e.target.value);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleTableLayoutChange = (e: RadioChangeEvent) => {
|
|
|
|
setTableLayout(e.target.value);
|
2019-09-04 18:12:57 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleExpandChange = (enable: boolean) => {
|
|
|
|
setExpandable(enable ? defaultExpandable : undefined);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleEllipsisChange = (enable: boolean) => {
|
|
|
|
setEllipsis(enable);
|
2019-09-04 18:12:57 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleTitleChange = (enable: boolean) => {
|
|
|
|
setShowTitle(enable);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleHeaderChange = (enable: boolean) => {
|
|
|
|
setShowHeader(enable);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-12-27 17:47:27 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleFooterChange = (enable: boolean) => {
|
|
|
|
setShowFooter(enable);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleRowSelectionChange = (enable: boolean) => {
|
|
|
|
setRowSelection(enable ? {} : undefined);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-09 20:16:57 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleYScrollChange = (enable: boolean) => {
|
|
|
|
setYScroll(enable);
|
2019-11-15 14:35:25 +08:00
|
|
|
};
|
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleXScrollChange = (e: RadioChangeEvent) => {
|
|
|
|
setXScroll(e.target.value);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2016-11-09 20:16:57 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const handleDataChange = (newHasData: boolean) => {
|
|
|
|
setHasData(newHasData);
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-09-17 14:36:26 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const scroll: { x?: number | string; y?: number | string } = {};
|
|
|
|
if (yScroll) {
|
|
|
|
scroll.y = 240;
|
|
|
|
}
|
|
|
|
if (xScroll) {
|
|
|
|
scroll.x = '100vw';
|
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const tableColumns = columns.map(item => ({ ...item, ellipsis }));
|
|
|
|
if (xScroll === 'fixed') {
|
|
|
|
tableColumns[0].fixed = true;
|
|
|
|
tableColumns[tableColumns.length - 1].fixed = 'right';
|
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
const tableProps: TableProps<DataType> = {
|
|
|
|
bordered,
|
|
|
|
loading,
|
|
|
|
size,
|
|
|
|
expandable,
|
|
|
|
title: showTitle ? defaultTitle : undefined,
|
|
|
|
showHeader,
|
|
|
|
footer: showfooter ? defaultFooter : undefined,
|
|
|
|
rowSelection,
|
|
|
|
scroll,
|
|
|
|
tableLayout,
|
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Form
|
|
|
|
layout="inline"
|
|
|
|
className="components-table-demo-control-bar"
|
|
|
|
style={{ marginBottom: 16 }}
|
|
|
|
>
|
|
|
|
<Form.Item label="Bordered">
|
|
|
|
<Switch checked={bordered} onChange={handleBorderChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="loading">
|
|
|
|
<Switch checked={loading} onChange={handleLoadingChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Title">
|
|
|
|
<Switch checked={showTitle} onChange={handleTitleChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Column Header">
|
|
|
|
<Switch checked={showHeader} onChange={handleHeaderChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Footer">
|
|
|
|
<Switch checked={showfooter} onChange={handleFooterChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Expandable">
|
|
|
|
<Switch checked={!!expandable} onChange={handleExpandChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Checkbox">
|
|
|
|
<Switch checked={!!rowSelection} onChange={handleRowSelectionChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Fixed Header">
|
|
|
|
<Switch checked={!!yScroll} onChange={handleYScrollChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Has Data">
|
|
|
|
<Switch checked={!!hasData} onChange={handleDataChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Ellipsis">
|
|
|
|
<Switch checked={!!ellipsis} onChange={handleEllipsisChange} />
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Size">
|
|
|
|
<Radio.Group value={size} onChange={handleSizeChange}>
|
|
|
|
<Radio.Button value="large">Large</Radio.Button>
|
|
|
|
<Radio.Button value="middle">Middle</Radio.Button>
|
|
|
|
<Radio.Button value="small">Small</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Table Scroll">
|
|
|
|
<Radio.Group value={xScroll} onChange={handleXScrollChange}>
|
|
|
|
<Radio.Button value={undefined}>Unset</Radio.Button>
|
|
|
|
<Radio.Button value="scroll">Scroll</Radio.Button>
|
|
|
|
<Radio.Button value="fixed">Fixed Columns</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Table Layout">
|
|
|
|
<Radio.Group value={tableLayout} onChange={handleTableLayoutChange}>
|
|
|
|
<Radio.Button value={undefined}>Unset</Radio.Button>
|
|
|
|
<Radio.Button value="fixed">Fixed</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Pagination Top">
|
|
|
|
<Radio.Group
|
|
|
|
value={top}
|
|
|
|
onChange={e => {
|
|
|
|
setTop(e.target.value);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Radio.Button value="topLeft">TopLeft</Radio.Button>
|
|
|
|
<Radio.Button value="topCenter">TopCenter</Radio.Button>
|
|
|
|
<Radio.Button value="topRight">TopRight</Radio.Button>
|
|
|
|
<Radio.Button value="none">None</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
|
|
|
<Form.Item label="Pagination Bottom">
|
|
|
|
<Radio.Group
|
|
|
|
value={bottom}
|
|
|
|
onChange={e => {
|
|
|
|
setBottom(e.target.value);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Radio.Button value="bottomLeft">BottomLeft</Radio.Button>
|
|
|
|
<Radio.Button value="bottomCenter">BottomCenter</Radio.Button>
|
|
|
|
<Radio.Button value="bottomRight">BottomRight</Radio.Button>
|
|
|
|
<Radio.Button value="none">None</Radio.Button>
|
|
|
|
</Radio.Group>
|
|
|
|
</Form.Item>
|
|
|
|
</Form>
|
|
|
|
<Table
|
|
|
|
{...tableProps}
|
|
|
|
pagination={{ position: [top as TablePaginationPosition, bottom] }}
|
|
|
|
columns={tableColumns}
|
|
|
|
dataSource={hasData ? data : []}
|
|
|
|
scroll={scroll}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2016-11-02 15:09:54 +08:00
|
|
|
|
2022-05-19 09:46:26 +08:00
|
|
|
export default App;
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2016-11-02 15:09:54 +08:00
|
|
|
|
|
|
|
<style>
|
2016-11-03 10:27:34 +08:00
|
|
|
.components-table-demo-control-bar .ant-form-item {
|
2022-05-31 10:10:35 +08:00
|
|
|
margin-right: 16px !important;
|
|
|
|
margin-bottom: 8px !important;
|
2016-11-03 10:27:34 +08:00
|
|
|
}
|
2016-11-02 15:09:54 +08:00
|
|
|
</style>
|