ant-design/components/table/demo/jsx.md

99 lines
2.0 KiB
Markdown
Raw Normal View History

---
order: 1
title:
en-US: JSX style API
zh-CN: JSX 风格的 API
---
## zh-CN
使用 JSX 风格的 API2.5.0 以后引入)
2017-01-12 00:03:35 +08:00
> 这个只是一个描述 `columns` 的语法糖,所以你不能用其他组件去包裹 `Column` 和 `ColumnGroup`。
## en-US
Using JSX style API (introduced in 2.5.0)
> Since this is just a syntax sugar for the prop `columns`, you can't compose `Column` and `ColumnGroup` with other Components.
```tsx
2022-05-21 22:14:15 +08:00
import { Space, Table, Tag } from 'antd';
import React from 'react';
const { Column, ColumnGroup } = Table;
interface DataType {
key: React.Key;
firstName: string;
lastName: string;
age: number;
address: string;
tags: string[];
}
const data: DataType[] = [
2019-05-07 14:57:32 +08:00
{
key: '1',
firstName: 'John',
lastName: 'Brown',
age: 32,
address: 'New York No. 1 Lake Park',
tags: ['nice', 'developer'],
},
{
key: '2',
firstName: 'Jim',
lastName: 'Green',
age: 42,
address: 'London No. 1 Lake Park',
tags: ['loser'],
},
{
key: '3',
firstName: 'Joe',
lastName: 'Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
},
];
const App: React.FC = () => (
2017-01-10 14:09:19 +08:00
<Table dataSource={data}>
<ColumnGroup title="Name">
2019-05-07 14:57:32 +08:00
<Column title="First Name" dataIndex="firstName" key="firstName" />
<Column title="Last Name" dataIndex="lastName" key="lastName" />
</ColumnGroup>
2019-05-07 14:57:32 +08:00
<Column title="Age" dataIndex="age" key="age" />
<Column title="Address" dataIndex="address" key="address" />
2018-08-18 12:48:56 +08:00
<Column
title="Tags"
dataIndex="tags"
key="tags"
render={(tags: string[]) => (
2020-05-03 19:46:52 +08:00
<>
2019-05-07 14:57:32 +08:00
{tags.map(tag => (
<Tag color="blue" key={tag}>
{tag}
</Tag>
))}
2020-05-03 19:46:52 +08:00
</>
2018-08-18 12:48:56 +08:00
)}
/>
<Column
title="Action"
key="action"
render={(_: any, record: DataType) => (
2020-05-03 19:46:52 +08:00
<Space size="middle">
<a>Invite {record.lastName}</a>
<a>Delete</a>
2020-05-03 19:46:52 +08:00
</Space>
)}
/>
</Table>
2018-11-28 15:00:03 +08:00
);
export default App;
2019-05-07 14:57:32 +08:00
```