前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >快速学习Ant Design-表格

快速学习Ant Design-表格

作者头像
cwl_java
发布2020-02-14 14:10:33
5960
发布2020-02-14 14:10:33
举报
文章被收录于专栏:cwl_Java

2.4、表格

2.4.1、基本用法

参考官方文档进行实现:https://ant.design/components/table-cn/ 改造UserList.js页面:

代码语言:javascript
复制
import React from 'react'

import {
	Table,
	Divider,
	Tag,
	Pagination
} from 'antd';
const {
	Column
} = Table;

const data = [{
	key: '1',
	name: '张三',
	age: 32,
	address: '上海市',
	tags: ['程序员', '帅气'],
}, {
	key: '2',
	name: '李四',
	age: 42,
	address: '北京市',
	tags: ['屌丝'],
}, {
	key: '3',
	name: '王五',
	age: 32,
	address: '杭州市',
	tags: ['高富帅', '富二代'],
}];
class UserList extends React.Component {
		render() {
				return ( <
					div >
					<
					Table dataSource = {
						data
					}
					pagination = {
						{
							position: "bottom",
							total: 500,
							pageSize: 10,
							defaultCurrent: 3
						}
					} >
					<
					Column title = " 姓 名 "
					dataIndex = "name"
					key = "name" /
					>

					<
					Columntitle = " 年 龄 "
					dataIndex = "age"
					key = "age" /
					>
					<
					Column title = " 地 址 "
					dataIndex = "address"
					key = "address"












					{
						tag
					} > {
						tag
					} < /Tag>)}

					/
					>
					<
					Column title = " 标 签 "
					dataIndex = "tags"
					key = "tags"
					render = {
						tags => ( <
							span > {
								tags.map(tag => < Tag color = "blue"
									key =

									<
									/span>

								)
							}
							/> <
							Column title = "操作"
							key = "action"
							render = {
								(text, record) => ( <
									span >
									<
									a href = "javascript:;" > 编辑 < /a> <
									Divider type = "vertical" / >
									<
									a href = "javascript:;" > 删除 < /a> <
									/span>
								)
							}
							/> <
							/Table> <
							/div>
						);
					}


				}

				export default UserList;

实现效果:

2.4.2、将数据分离到model中

model的实现:UserListData.js

代码语言:javascript
复制
import request from "../util/request";

export default {
	namespace: 'userList',
	state: {
		list: []
	},
	effects: {
		* initData(params, sagaEffects) {
			const {
				call,
				put
			} = sagaEffects;
			const url = "/ds/user/list";
			let data = yield call(request, url);
			yield put({
				type: "queryList",
				data: data
			});
		}
	},
	reducers: {
		queryList(state, result) {
			let data = [...result.data];
			return { //更新状态值
				list: data
			}
		}
	}


}

修改UserList.js中的逻辑:

代码语言:javascript
复制
import React from 'react';
import {
	connect
} from 'dva';


import {
	Table,
	Divider,
	Tag,
	Pagination
} from 'antd';
const {
	Column
} = Table;
const namespace = 'userList';

@connect((state) => {
	return {
		data: state[namespace].list
	}
}, (dispatch) => {
	return {
		initData: () => {
			dispatch({
				type: namespace + "/initData"
			});
		}
	}
})
class UserList extends React.Component {

		componentDidMount() {
			this.props.initData();
		}

		render() {
				return ( <
					div >
					<
					Table dataSource = {
						this.props.data
					}
					pagination = {
						{
							position: "bottom",
							total: 500,
							pageSize: 10,
							defaultCurrent: 3
						}
					} >
					<
					Column title = " 姓 名 "
					dataIndex = "name"
					key = "name" /
					>

					<
					Columntitle = " 年 龄 "
					dataIndex = "age"
					key = "age" /
					>
					<
					Column title = " 地 址 "
					dataIndex = "address"
					key = "address"












					{
						tag
					} > {
						tag
					} < /Tag>)}

					/
					>
					<
					Column title = " 标 签 "
					dataIndex = "tags"
					key = "tags"
					render = {
						tags => ( <
							span > {
								tags.map(tag => < Tag color = "blue"
									key =

									<
									/span>

								)
							}
							/> <
							Column title = "操作"
							key = "action"
							render = {
								(text, record) => ( <
									span >
									<
									a href = "javascript:;" > 编辑 < /a> <
									Divider type = "vertical" / >
									<
									a href = "javascript:;" > 删除 < /a> <
									/span>
								)
							}
							/> <
							/Table> <
							/div>
						);
					}


				}

				export default UserList;

mock数据:MockListData.js

代码语言:javascript
复制
export default {
	'get /ds/list': function(req, res) {
		res.json({
			data: [1, 2, 3, 4],
			maxNum: 4
		});
	},
	'get /ds/user/list': function(req, res) {
		res.json([{
			key: '1',
			name: '张三1',
			age: 32,
			address: '上海市',
			tags: ['程序员', '帅气'],
		}, {
			key: '2',
			name: '李四',
			age: 42,
			address: '北京市',
			tags: ['屌丝'],
		}, {
			key: '3',
			name: '王五',
			age: 32,
			address: '杭州市',
			tags: ['高富帅', '富二代'],
		}]);
	}
}

测试结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/01/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.4、表格
    • 2.4.1、基本用法
      • 2.4.2、将数据分离到model中
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档