1.ui渲染
//src/paperWork.tsx
import React from 'react';
import { Button ,Alert } from 'antd';
import ProTable from '@ant-design/pro-table';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
const data = [
{ id: 1, title: 'toodlist列表', status: 0 },
{ id: 2, title: 'toodlist添加', status: 1 },
{ id: 3, title: 'toodlist编辑', status: 2 },
{ id: 4, title: 'toodlist修改状态', status: 0 },
{ id: 5, title: 'toodlist列表', status: 0 },
{ id: 6, title: 'toodlist添加', status: 1 },
];
const status=[
<Alert message="待办" type="info" showIcon />,
<Alert message="已完成" type="success" showIcon />,
<Alert message="已取消" type="error" showIcon />,
]
const columns = [
{
title: 'ID',
dataIndex: 'id',
},
{
title: '标题',
dataIndex: 'title',
},
{
title: '状态',
dataIndex: 'status',
render:(_,record)=>{
return status[record.status]
}
},
{
title: '修改状态',
render: () => [<a>待办</a>, <a>完成</a>,<a>取消</a>],
},
];
const PeperWork = () => {
return (
<PageHeaderWrapper>
<ProTable
columns={columns}
rowKey="key"
pagination={{
showQuickJumper: true,
}}
dataSource={data}
search={false}
dateFormatter="string"
headerTitle="待办事项列表"
toolBarRender={() => [
,
<Button type="primary" key="primary">
<PlusOutlined /> 新建
</Button>,
]}
/>
</PageHeaderWrapper>
);
};
export default PeperWork;
2.请求数据
2.1设置mock数据
//mock/todo.ts
export default {
'/api/todo': [
{ id: 1, title: 'toodlist列表', status: 0 },
{ id: 2, title: 'toodlist添加', status: 1 },
{ id: 3, title: 'toodlist编辑', status: 2 },
{ id: 4, title: 'toodlist修改状态', status: 0 },
{ id: 5, title: 'toodlist列表', status: 0 },
{ id: 6, title: 'toodlist添加', status: 1 },
]
}
2.2请求数据
//src/services/todo.ts
import request from '@/utils/request';
export async function getTodolistDatas(): Promise<any> {
return request('/api/todo');
}
2.3在组件中使用
import React, { useEffect, useState } from 'react';
import { Button, Alert } from 'antd';
import ProTable from '@ant-design/pro-table';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { getTodolistDatas } from '@/services/todo';
const status = [
<Alert message="待办" type="info" showIcon />,
<Alert message="已完成" type="success" showIcon />,
<Alert message="已取消" type="error" showIcon />,
];
const columns = [
{
title: 'ID',
dataIndex: 'id',
},
{
title: '标题',
dataIndex: 'title',
},
{
title: '状态',
dataIndex: 'status',
render: (_, record) => {
return status[record.status];
},
},
{
title: '修改状态',
render: () => [<a>待办</a>, <a>完成</a>, <a>取消</a>],
},
];
const PeperWork = () => {
const [data, setData] = useState([]);
useEffect(async () => {
const resdata = await getTodolistDatas();
setData(resdata);
}, []);
return (
<PageHeaderWrapper>
<ProTable
columns={columns}
rowKey="key"
pagination={{
showQuickJumper: true,
}}
dataSource={data}
search={false}
dateFormatter="string"
headerTitle="待办事项列表"
toolBarRender={() => [
,
<Button type="primary" key="primary">
<PlusOutlined /> 新建
</Button>,
]}
/>
</PageHeaderWrapper>
);
};
export default PeperWork;
3.使用model共享数据
3.1设置model
//models/todo.ts
import { Effect,Reducer} from 'umi'
interface IndexModelState {
name: any;
}
interface todolistlType {
namespace :string,
state:{
todoList:any[]
},
effects: {
getTodolist: Effect
},
reducers:{
setTodolist: Reducer<IndexModelState>;
}
}
const todoList:todolistlType ={
namespace:'todo',
state:{
todoList:[]
},
effects:{
*getTodolist({},{call,put}){
//调用方法获取数据
const resData=yield call(getTodolistDatas)
yield put({
type:'setTodolist',
payload:resData
})
}
},
reducers:{
setTodolist(state:any,action:any){
return {
...state,
todoList:action.payload
}
}
}
}
export default todoList;
3.2第一个组件中使用
//components/GlobalHeader/AvatarDropdown.tsx
import {
LogoutOutlined,
SettingOutlined,
UserOutlined,
MenuUnfoldOutlined,
} from '@ant-design/icons';
import { Avatar, Menu, Spin, Badge } from 'antd';
import React from 'react';
import type { ConnectProps } from 'umi';
import { history, connect } from 'umi';
import type { ConnectState } from '@/models/connect';
import type { CurrentUser } from '@/models/user';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';
export type GlobalHeaderRightProps = {
currentUser?: CurrentUser;
menu?: boolean;
todo: any;
dispatch: () => any;
} & Partial<ConnectProps>;
class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
onMenuClick = (event: {
key: React.Key;
keyPath: React.Key[];
item: React.ReactInstance;
domEvent: React.MouseEvent<HTMLElement>;
}) => {
const { key } = event;
//根据key值判断跳转到哪个页面
if (key === 'todo') {
history.push('/paperWork');
return;
}
if (key === 'logout') {
const { dispatch } = this.props;
if (dispatch) {
dispatch({
type: 'login/logout',
});
}
return;
}
history.push(`/account/${key}`);
};
//派发触发函数
componentDidMount() {
this.props.dispatch({
type: 'todo/getTodolist',
payload: null,
});
}
render(): React.ReactNode {
//这里进行筛选
const num=this.props.todo.todoList.filter((item:any)=>item.status===0).length
const {
currentUser = {
avatar: '',
name: '',
},
menu,
} = this.props;
const menuHeaderDropdown = (
<Menu className={styles.menu} selectedKeys={[]} onClick={this.onMenuClick}>
{menu && (
<Menu.Item key="center">
<UserOutlined />
个人中心
</Menu.Item>
)}
{menu && (
<Menu.Item key="settings">
<SettingOutlined />
个人设置
</Menu.Item>
)}
{menu && <Menu.Divider />}
<Menu.Item key="todo">
<MenuUnfoldOutlined />
待办事项
{/* 这里使用redux中的数据 */}
<Badge count={num} offset={[10, 0]} />
</Menu.Item>
<Menu.Item key="logout">
<LogoutOutlined />
退出登录
</Menu.Item>
</Menu>
);
return currentUser && currentUser.name ? (
<HeaderDropdown overlay={menuHeaderDropdown}>
<span className={`${styles.action} ${styles.account}`}>
<Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
<span className={`${styles.name} anticon`}>
{currentUser.name} <Badge count={num} dot={true} />
</span>
</span>
</HeaderDropdown>
) : (
<span className={`${styles.action} ${styles.account}`}>
<Spin
size="small"
style={{
marginLeft: 8,
marginRight: 8,
}}
/>
</span>
);
}
}
//这里进行连接
export default connect(({ user, todo }: ConnectState) => ({
currentUser: user.currentUser,
todo,
}))(AvatarDropdown);
3.3在第二个组件中实现数据共享
使用过一次触发后就不需要在使用dispatch进行触发了
//pages/PaperWork/index.tsx
import React from 'react';
import { Button, Alert } from 'antd';
import ProTable from '@ant-design/pro-table';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { connect } from 'umi';
const status = [
<Alert message="待办" type="info" showIcon />,
<Alert message="已完成" type="success" showIcon />,
<Alert message="已取消" type="error" showIcon />,
];
const columns = [
{
title: 'ID',
dataIndex: 'id',
},
{
title: '标题',
dataIndex: 'title',
},
{
title: '状态',
dataIndex: 'status',
render: (_:any, record:any) => {
return status[record.status];
},
},
{
title: '修改状态',
render: () => [<a key={0}>待办</a>, <a key={1}>完成</a>, <a key={2}>取消</a>]
},
];
const PeperWork = (props:any) => {
return (
<PageHeaderWrapper>
<ProTable
columns={columns}
rowKey="key"
pagination={{
showQuickJumper: true,
}}
dataSource={props.todo.todoList}
search={false}
dateFormatter="string"
headerTitle="待办事项列表"
toolBarRender={() => [
,
<Button type="primary" key="primary">
<PlusOutlined /> 新建
</Button>,
]}
/>
</PageHeaderWrapper>
);
};
export default connect(({todo}:any)=>({todo}))(PeperWork);
4.添加数据
4.1mock数据
//mock/todo.ts
let list:{}[] =[
{ id: 1, title: 'toodlist列表', status: 0 },
{ id: 2, title: 'toodlist添加', status: 1 },
{ id: 3, title: 'toodlist编辑', status: 2 },
{ id: 4, title: 'toodlist修改状态', status: 0 },
{ id: 5, title: 'toodlist列表', status: 0 },
{ id: 6, title: 'toodlist添加', status: 1 },
]
export default {
'GET /api/todo': list,
'POST /api/posttodo':(req:any,ret:any)=>{
//添加todo
const item={
id:list.length+1,
title:req.body.todoList,
status:0
}
list.push(item)
let res={
code:201,
message:'添加待办事项成功'
}
ret.send(res)
}
}
4.2发送请求
import request from '@/utils/request';
export async function getTodolistDatas(): Promise<any> {
return request('/api/todo');
}
//这里是发送请求
export async function postTodolistDatas(params:any): Promise<any> {
return request.post('/api/posttodo',{data:params});
}
4.3组件使用
//pages/PaperWork/index.tsx
import React, { useState } from 'react';
import { Button, Alert, Modal, message } from 'antd';
import ProTable from '@ant-design/pro-table';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { connect } from 'umi';
import ProForm, { ProFormText} from '@ant-design/pro-form';
import {postTodolistDatas}from "@/services/todo"
const status = [
<Alert message="待办" type="info" showIcon />,
<Alert message="已完成" type="success" showIcon />,
<Alert message="已取消" type="error" showIcon />,
];
const columns = [
{
title: 'ID',
dataIndex: 'id',
},
{
title: '标题',
dataIndex: 'title',
},
{
title: '状态',
dataIndex: 'status',
render: (_: any, record: any) => {
return status[record.status];
},
},
{
title: '修改状态',
render: () => [<a key={0}>待办</a>, <a key={1}>完成</a>, <a key={2}>取消</a>],
},
];
const PeperWork = (props: any) => {
const [modelFlag, setModel] = useState<boolean>(false);
const formHandle = () => {
setModel(true);
};
const cancelhandle = () => {
setModel(false);
};
const sumbmithandle=async(val:any)=>{
const res=await postTodolistDatas(val)
if(res.code===201){
props.dispatch({
type:'todo/getTodolist',
payload:null
})
message.success(res.message)
setModel(false);
}
else{
message.error(res.message)
}
}
return (
<PageHeaderWrapper>
<ProTable
columns={columns}
rowKey="key"
pagination={{
showQuickJumper: true,
}}
dataSource={props.todo.todoList}
search={false}
dateFormatter="string"
headerTitle="待办事项列表"
toolBarRender={() => [
,
<Button type="primary" key="primary" onClick={formHandle}>
<PlusOutlined /> 新建
</Button>,
]}
/>
<Modal footer={null} onCancel={cancelhandle} title="待办事项列表" visible={modelFlag}>
<ProForm onFinish={(values:any)=>{sumbmithandle(values)}}>
<ProFormText name="todoList" label="标题" rules={[{ required: true }]} />
</ProForm>
</Modal>
</PageHeaderWrapper>
);
};
export default connect(({ todo }: any) => ({ todo }))(PeperWork);
5.修改状态
5.1 mock数据
//mock/todo.ts
interface listObjectType{
id:number,
title:string,
status:number
}
let list:listObjectType[] =[
{ id: 1, title: 'toodlist列表', status: 0 },
{ id: 2, title: 'toodlist添加', status: 1 },
{ id: 3, title: 'toodlist编辑', status: 2 },
{ id: 4, title: 'toodlist修改状态', status: 0 },
{ id: 5, title: 'toodlist列表', status: 0 },
{ id: 6, title: 'toodlist添加', status: 1 },
]
export default {
'GET /api/todo': list,
'POST /api/posttodo':(req:any,ret:any)=>{
//添加todo
const item={
id:list.length+1,
title:req.body.todoList,
status:0
}
list.push(item)
let res={
code:201,
message:'添加待办事项成功'
}
ret.send(res)
},
//修改操作
'PUT /api/modify':(req:any,ret:any)=>{
//筛选操作
const {id,status}=req.body
list.map((item,index)=>{
if(item.id===id){
list[index].status=status
}
})
let res={
code:201,
message:'修改办事项成功'
}
ret.send(res)
}
}
5.2请求数据
//services/todo.ts
import request from '@/utils/request';
export async function getTodolistDatas(): Promise<any> {
return request('/api/todo');
}
export async function postTodolistDatas(params:any): Promise<any> {
return request.post('/api/posttodo',{data:params});
}
//发送修改请求操作
export async function modifyTodolistDatas(params:any): Promise<any> {
return request.put('/api/modify',{data:params});
}
5.3在组件中使用
import React, { useState } from 'react';
import { Button, Alert, Modal, message } from 'antd';
import ProTable from '@ant-design/pro-table';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { connect } from 'umi';
import ProForm, { ProFormText } from '@ant-design/pro-form';
import { postTodolistDatas,modifyTodolistDatas } from '@/services/todo';
const PeperWork = (props: any) => {
const [modelFlag, setModel] = useState<boolean>(false);
const formHandle = () => {
setModel(true);
};
const cancelhandle = () => {
setModel(false);
};
const sumbmithandle = async (val: any) => {
const res = await postTodolistDatas(val);
if (res.code === 201) {
props.dispatch({
type: 'todo/getTodolist',
payload: null,
});
message.success(res.message);
setModel(false);
} else {
message.error(res.message);
}
};
const status = [
<Alert message="待办" type="info" showIcon />,
<Alert message="已完成" type="success" showIcon />,
<Alert message="已取消" type="error" showIcon />,
];
const columns = [
{
title: 'ID',
dataIndex: 'id',
},
{
title: '标题',
dataIndex: 'title',
},
{
title: '状态',
dataIndex: 'status',
render: (_: any, record: any) => {
return status[record.status];
},
},
{
title: '修改状态',
render: (_: any, record: any) => {
let editOption = [];
//这里是修改中状态
if(record.status !== 0){
editOption.push(<a key={0} style={{ marginRight: 20}} onClick={()=>changeStatus(record.id,0)}>待办</a>);
}
if( record.status !==1){
editOption.push(<a key={1} style={{ marginRight: 20}} onClick={()=>changeStatus(record.id,1)}>完成</a>);
}
if(record.status !== 2){
editOption.push(<a key={2} style={{marginRight: 20}} onClick={()=>changeStatus(record.id,2)}>取消</a>);
}
return editOption
},
},
];
//这里是发送请求改变状态
const changeStatus=async(id:any,status:any)=>{
// 修改状态,调用seivice
const res=await modifyTodolistDatas({
id,
status
})
if(res.code===201){
props.dispatch({
type: 'todo/getTodolist',
payload: null,
});
message.success(res.message);
}
else{
message.error(res.message);
}
//判断执行结果
}
return (
<PageHeaderWrapper>
<ProTable
columns={columns}
rowKey="key"
pagination={{
showQuickJumper: true,
}}
dataSource={props.todo.todoList}
search={false}
dateFormatter="string"
headerTitle="待办事项列表"
toolBarRender={() => [
,
<Button type="primary" key="primary" onClick={formHandle}>
<PlusOutlined /> 新建
</Button>,
]}
/>
<Modal footer={null} onCancel={cancelhandle} title="待办事项列表" visible={modelFlag}>
<ProForm
onFinish={(values: any) => {
sumbmithandle(values);
}}
>
<ProFormText name="todoList" label="标题" rules={[{ required: true }]} />
</ProForm>
</Modal>
</PageHeaderWrapper>
);
};
export default connect(({ todo }: any) => ({ todo }))(PeperWork);