Learning ant design table - table component introduced into the first

1. Install the create-react-app. Creating your first project my-app.
Npm install the Create-REACT $-App
$ the Create-REACT-My-App App
$ cd My-App
2. Installation antd, reqwest.
. $ Npm install reqwest --save-dev
. $ Npm install reqwest --save-dev
3. Modify APP.js file as follows. (Must be introduced import 'antd / dist / antd.css' ; otherwise the table can not be displayed)
Import from React 'REACT';
Import {} from the Table 'antd';
Import reqwest from 'reqwest';

const columns = [
{
title: 'Name',
dataIndex: 'name',
sorter: true,
render: name => ${name.first} ${name.last},
width: '20%',
},
{
title: 'Gender',
dataIndex: 'gender',
filters: [{ text: 'Male', value: 'male' }, { text: 'Female', value: 'female' }],
width: '20%',
},
{
title: 'Email',
dataIndex: 'email',
},
];

export default class App extends React.Component {
state = {
data: [],
pagination: {},
loading: false,
};

componentDidMount() {
this.fetch();
}

handleTableChange = (pagination, filters, sorter) => {
const pager = { ...this.state.pagination };
pager.current = pagination.current;
this.setState({
pagination: pager,
});
this.fetch({
results: pagination.pageSize,
page: pagination.current,
sortField: sorter.field,
sortOrder: sorter.order,
...filters,
});
};

fetch = (params = {}) => {
console.log('params:', params);
this.setState({ loading: true });
reqwest({
url: 'https://randomuser.me/api',
method: 'get',
data: {
results: 10,
...params,
},
type: 'json',
}).then(data => {
const pagination = { ...this.state.pagination };
// Read total count from server
// pagination.total = data.totalCount;
pagination.total = 200;
this.setState({
loading: false,
data: data.results,
pagination,
});
});
};

render() {
return (
<Table
columns={columns}
rowKey={record => record.login.uuid}
dataSource={this.state.data}
pagination={this.state.pagination}
loading={this.state.loading}
onChange={this.handleTableChange}
/>
);
}
}

4.npm start.
5. Results
Learning ant design table - table component introduced into the first

Guess you like

Origin blog.51cto.com/13625527/2433108