1.ui渲染

  1. //src/paperWork.tsx
  2. import React from 'react';
  3. import { Button ,Alert } from 'antd';
  4. import ProTable from '@ant-design/pro-table';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  7. const data = [
  8. { id: 1, title: 'toodlist列表', status: 0 },
  9. { id: 2, title: 'toodlist添加', status: 1 },
  10. { id: 3, title: 'toodlist编辑', status: 2 },
  11. { id: 4, title: 'toodlist修改状态', status: 0 },
  12. { id: 5, title: 'toodlist列表', status: 0 },
  13. { id: 6, title: 'toodlist添加', status: 1 },
  14. ];
  15. const status=[
  16. <Alert message="待办" type="info" showIcon />,
  17. <Alert message="已完成" type="success" showIcon />,
  18. <Alert message="已取消" type="error" showIcon />,
  19. ]
  20. const columns = [
  21. {
  22. title: 'ID',
  23. dataIndex: 'id',
  24. },
  25. {
  26. title: '标题',
  27. dataIndex: 'title',
  28. },
  29. {
  30. title: '状态',
  31. dataIndex: 'status',
  32. render:(_,record)=>{
  33. return status[record.status]
  34. }
  35. },
  36. {
  37. title: '修改状态',
  38. render: () => [<a>待办</a>, <a>完成</a>,<a>取消</a>],
  39. },
  40. ];
  41. const PeperWork = () => {
  42. return (
  43. <PageHeaderWrapper>
  44. <ProTable
  45. columns={columns}
  46. rowKey="key"
  47. pagination={{
  48. showQuickJumper: true,
  49. }}
  50. dataSource={data}
  51. search={false}
  52. dateFormatter="string"
  53. headerTitle="待办事项列表"
  54. toolBarRender={() => [
  55. ,
  56. <Button type="primary" key="primary">
  57. <PlusOutlined /> 新建
  58. </Button>,
  59. ]}
  60. />
  61. </PageHeaderWrapper>
  62. );
  63. };
  64. export default PeperWork;

2.请求数据

2.1设置mock数据

  1. //mock/todo.ts
  2. export default {
  3. '/api/todo': [
  4. { id: 1, title: 'toodlist列表', status: 0 },
  5. { id: 2, title: 'toodlist添加', status: 1 },
  6. { id: 3, title: 'toodlist编辑', status: 2 },
  7. { id: 4, title: 'toodlist修改状态', status: 0 },
  8. { id: 5, title: 'toodlist列表', status: 0 },
  9. { id: 6, title: 'toodlist添加', status: 1 },
  10. ]
  11. }

2.2请求数据

  1. //src/services/todo.ts
  2. import request from '@/utils/request';
  3. export async function getTodolistDatas(): Promise<any> {
  4. return request('/api/todo');
  5. }

2.3在组件中使用

  1. import React, { useEffect, useState } from 'react';
  2. import { Button, Alert } from 'antd';
  3. import ProTable from '@ant-design/pro-table';
  4. import { PlusOutlined } from '@ant-design/icons';
  5. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  6. import { getTodolistDatas } from '@/services/todo';
  7. const status = [
  8. <Alert message="待办" type="info" showIcon />,
  9. <Alert message="已完成" type="success" showIcon />,
  10. <Alert message="已取消" type="error" showIcon />,
  11. ];
  12. const columns = [
  13. {
  14. title: 'ID',
  15. dataIndex: 'id',
  16. },
  17. {
  18. title: '标题',
  19. dataIndex: 'title',
  20. },
  21. {
  22. title: '状态',
  23. dataIndex: 'status',
  24. render: (_, record) => {
  25. return status[record.status];
  26. },
  27. },
  28. {
  29. title: '修改状态',
  30. render: () => [<a>待办</a>, <a>完成</a>, <a>取消</a>],
  31. },
  32. ];
  33. const PeperWork = () => {
  34. const [data, setData] = useState([]);
  35. useEffect(async () => {
  36. const resdata = await getTodolistDatas();
  37. setData(resdata);
  38. }, []);
  39. return (
  40. <PageHeaderWrapper>
  41. <ProTable
  42. columns={columns}
  43. rowKey="key"
  44. pagination={{
  45. showQuickJumper: true,
  46. }}
  47. dataSource={data}
  48. search={false}
  49. dateFormatter="string"
  50. headerTitle="待办事项列表"
  51. toolBarRender={() => [
  52. ,
  53. <Button type="primary" key="primary">
  54. <PlusOutlined /> 新建
  55. </Button>,
  56. ]}
  57. />
  58. </PageHeaderWrapper>
  59. );
  60. };
  61. export default PeperWork;

3.使用model共享数据

3.1设置model

  1. //models/todo.ts
  2. import { Effect,Reducer} from 'umi'
  3. interface IndexModelState {
  4. name: any;
  5. }
  6. interface todolistlType {
  7. namespace :string,
  8. state:{
  9. todoList:any[]
  10. },
  11. effects: {
  12. getTodolist: Effect
  13. },
  14. reducers:{
  15. setTodolist: Reducer<IndexModelState>;
  16. }
  17. }
  18. const todoList:todolistlType ={
  19. namespace:'todo',
  20. state:{
  21. todoList:[]
  22. },
  23. effects:{
  24. *getTodolist({},{call,put}){
  25. //调用方法获取数据
  26. const resData=yield call(getTodolistDatas)
  27. yield put({
  28. type:'setTodolist',
  29. payload:resData
  30. })
  31. }
  32. },
  33. reducers:{
  34. setTodolist(state:any,action:any){
  35. return {
  36. ...state,
  37. todoList:action.payload
  38. }
  39. }
  40. }
  41. }
  42. export default todoList;

3.2第一个组件中使用

  1. //components/GlobalHeader/AvatarDropdown.tsx
  2. import {
  3. LogoutOutlined,
  4. SettingOutlined,
  5. UserOutlined,
  6. MenuUnfoldOutlined,
  7. } from '@ant-design/icons';
  8. import { Avatar, Menu, Spin, Badge } from 'antd';
  9. import React from 'react';
  10. import type { ConnectProps } from 'umi';
  11. import { history, connect } from 'umi';
  12. import type { ConnectState } from '@/models/connect';
  13. import type { CurrentUser } from '@/models/user';
  14. import HeaderDropdown from '../HeaderDropdown';
  15. import styles from './index.less';
  16. export type GlobalHeaderRightProps = {
  17. currentUser?: CurrentUser;
  18. menu?: boolean;
  19. todo: any;
  20. dispatch: () => any;
  21. } & Partial<ConnectProps>;
  22. class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
  23. onMenuClick = (event: {
  24. key: React.Key;
  25. keyPath: React.Key[];
  26. item: React.ReactInstance;
  27. domEvent: React.MouseEvent<HTMLElement>;
  28. }) => {
  29. const { key } = event;
  30. //根据key值判断跳转到哪个页面
  31. if (key === 'todo') {
  32. history.push('/paperWork');
  33. return;
  34. }
  35. if (key === 'logout') {
  36. const { dispatch } = this.props;
  37. if (dispatch) {
  38. dispatch({
  39. type: 'login/logout',
  40. });
  41. }
  42. return;
  43. }
  44. history.push(`/account/${key}`);
  45. };
  46. //派发触发函数
  47. componentDidMount() {
  48. this.props.dispatch({
  49. type: 'todo/getTodolist',
  50. payload: null,
  51. });
  52. }
  53. render(): React.ReactNode {
  54. //这里进行筛选
  55. const num=this.props.todo.todoList.filter((item:any)=>item.status===0).length
  56. const {
  57. currentUser = {
  58. avatar: '',
  59. name: '',
  60. },
  61. menu,
  62. } = this.props;
  63. const menuHeaderDropdown = (
  64. <Menu className={styles.menu} selectedKeys={[]} onClick={this.onMenuClick}>
  65. {menu && (
  66. <Menu.Item key="center">
  67. <UserOutlined />
  68. 个人中心
  69. </Menu.Item>
  70. )}
  71. {menu && (
  72. <Menu.Item key="settings">
  73. <SettingOutlined />
  74. 个人设置
  75. </Menu.Item>
  76. )}
  77. {menu && <Menu.Divider />}
  78. <Menu.Item key="todo">
  79. <MenuUnfoldOutlined />
  80. 待办事项
  81. {/* 这里使用redux中的数据 */}
  82. <Badge count={num} offset={[10, 0]} />
  83. </Menu.Item>
  84. <Menu.Item key="logout">
  85. <LogoutOutlined />
  86. 退出登录
  87. </Menu.Item>
  88. </Menu>
  89. );
  90. return currentUser && currentUser.name ? (
  91. <HeaderDropdown overlay={menuHeaderDropdown}>
  92. <span className={`${styles.action} ${styles.account}`}>
  93. <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
  94. <span className={`${styles.name} anticon`}>
  95. {currentUser.name} <Badge count={num} dot={true} />
  96. </span>
  97. </span>
  98. </HeaderDropdown>
  99. ) : (
  100. <span className={`${styles.action} ${styles.account}`}>
  101. <Spin
  102. size="small"
  103. style={{
  104. marginLeft: 8,
  105. marginRight: 8,
  106. }}
  107. />
  108. </span>
  109. );
  110. }
  111. }
  112. //这里进行连接
  113. export default connect(({ user, todo }: ConnectState) => ({
  114. currentUser: user.currentUser,
  115. todo,
  116. }))(AvatarDropdown);

3.3在第二个组件中实现数据共享

使用过一次触发后就不需要在使用dispatch进行触发了

  1. //pages/PaperWork/index.tsx
  2. import React from 'react';
  3. import { Button, Alert } from 'antd';
  4. import ProTable from '@ant-design/pro-table';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  7. import { connect } from 'umi';
  8. const status = [
  9. <Alert message="待办" type="info" showIcon />,
  10. <Alert message="已完成" type="success" showIcon />,
  11. <Alert message="已取消" type="error" showIcon />,
  12. ];
  13. const columns = [
  14. {
  15. title: 'ID',
  16. dataIndex: 'id',
  17. },
  18. {
  19. title: '标题',
  20. dataIndex: 'title',
  21. },
  22. {
  23. title: '状态',
  24. dataIndex: 'status',
  25. render: (_:any, record:any) => {
  26. return status[record.status];
  27. },
  28. },
  29. {
  30. title: '修改状态',
  31. render: () => [<a key={0}>待办</a>, <a key={1}>完成</a>, <a key={2}>取消</a>]
  32. },
  33. ];
  34. const PeperWork = (props:any) => {
  35. return (
  36. <PageHeaderWrapper>
  37. <ProTable
  38. columns={columns}
  39. rowKey="key"
  40. pagination={{
  41. showQuickJumper: true,
  42. }}
  43. dataSource={props.todo.todoList}
  44. search={false}
  45. dateFormatter="string"
  46. headerTitle="待办事项列表"
  47. toolBarRender={() => [
  48. ,
  49. <Button type="primary" key="primary">
  50. <PlusOutlined /> 新建
  51. </Button>,
  52. ]}
  53. />
  54. </PageHeaderWrapper>
  55. );
  56. };
  57. export default connect(({todo}:any)=>({todo}))(PeperWork);

4.添加数据

4.1mock数据

  1. //mock/todo.ts
  2. let list:{}[] =[
  3. { id: 1, title: 'toodlist列表', status: 0 },
  4. { id: 2, title: 'toodlist添加', status: 1 },
  5. { id: 3, title: 'toodlist编辑', status: 2 },
  6. { id: 4, title: 'toodlist修改状态', status: 0 },
  7. { id: 5, title: 'toodlist列表', status: 0 },
  8. { id: 6, title: 'toodlist添加', status: 1 },
  9. ]
  10. export default {
  11. 'GET /api/todo': list,
  12. 'POST /api/posttodo':(req:any,ret:any)=>{
  13. //添加todo
  14. const item={
  15. id:list.length+1,
  16. title:req.body.todoList,
  17. status:0
  18. }
  19. list.push(item)
  20. let res={
  21. code:201,
  22. message:'添加待办事项成功'
  23. }
  24. ret.send(res)
  25. }
  26. }

4.2发送请求

  1. import request from '@/utils/request';
  2. export async function getTodolistDatas(): Promise<any> {
  3. return request('/api/todo');
  4. }
  5. //这里是发送请求
  6. export async function postTodolistDatas(params:any): Promise<any> {
  7. return request.post('/api/posttodo',{data:params});
  8. }

4.3组件使用

  1. //pages/PaperWork/index.tsx
  2. import React, { useState } from 'react';
  3. import { Button, Alert, Modal, message } from 'antd';
  4. import ProTable from '@ant-design/pro-table';
  5. import { PlusOutlined } from '@ant-design/icons';
  6. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  7. import { connect } from 'umi';
  8. import ProForm, { ProFormText} from '@ant-design/pro-form';
  9. import {postTodolistDatas}from "@/services/todo"
  10. const status = [
  11. <Alert message="待办" type="info" showIcon />,
  12. <Alert message="已完成" type="success" showIcon />,
  13. <Alert message="已取消" type="error" showIcon />,
  14. ];
  15. const columns = [
  16. {
  17. title: 'ID',
  18. dataIndex: 'id',
  19. },
  20. {
  21. title: '标题',
  22. dataIndex: 'title',
  23. },
  24. {
  25. title: '状态',
  26. dataIndex: 'status',
  27. render: (_: any, record: any) => {
  28. return status[record.status];
  29. },
  30. },
  31. {
  32. title: '修改状态',
  33. render: () => [<a key={0}>待办</a>, <a key={1}>完成</a>, <a key={2}>取消</a>],
  34. },
  35. ];
  36. const PeperWork = (props: any) => {
  37. const [modelFlag, setModel] = useState<boolean>(false);
  38. const formHandle = () => {
  39. setModel(true);
  40. };
  41. const cancelhandle = () => {
  42. setModel(false);
  43. };
  44. const sumbmithandle=async(val:any)=>{
  45. const res=await postTodolistDatas(val)
  46. if(res.code===201){
  47. props.dispatch({
  48. type:'todo/getTodolist',
  49. payload:null
  50. })
  51. message.success(res.message)
  52. setModel(false);
  53. }
  54. else{
  55. message.error(res.message)
  56. }
  57. }
  58. return (
  59. <PageHeaderWrapper>
  60. <ProTable
  61. columns={columns}
  62. rowKey="key"
  63. pagination={{
  64. showQuickJumper: true,
  65. }}
  66. dataSource={props.todo.todoList}
  67. search={false}
  68. dateFormatter="string"
  69. headerTitle="待办事项列表"
  70. toolBarRender={() => [
  71. ,
  72. <Button type="primary" key="primary" onClick={formHandle}>
  73. <PlusOutlined /> 新建
  74. </Button>,
  75. ]}
  76. />
  77. <Modal footer={null} onCancel={cancelhandle} title="待办事项列表" visible={modelFlag}>
  78. <ProForm onFinish={(values:any)=>{sumbmithandle(values)}}>
  79. <ProFormText name="todoList" label="标题" rules={[{ required: true }]} />
  80. </ProForm>
  81. </Modal>
  82. </PageHeaderWrapper>
  83. );
  84. };
  85. export default connect(({ todo }: any) => ({ todo }))(PeperWork);

5.修改状态

5.1 mock数据

  1. //mock/todo.ts
  2. interface listObjectType{
  3. id:number,
  4. title:string,
  5. status:number
  6. }
  7. let list:listObjectType[] =[
  8. { id: 1, title: 'toodlist列表', status: 0 },
  9. { id: 2, title: 'toodlist添加', status: 1 },
  10. { id: 3, title: 'toodlist编辑', status: 2 },
  11. { id: 4, title: 'toodlist修改状态', status: 0 },
  12. { id: 5, title: 'toodlist列表', status: 0 },
  13. { id: 6, title: 'toodlist添加', status: 1 },
  14. ]
  15. export default {
  16. 'GET /api/todo': list,
  17. 'POST /api/posttodo':(req:any,ret:any)=>{
  18. //添加todo
  19. const item={
  20. id:list.length+1,
  21. title:req.body.todoList,
  22. status:0
  23. }
  24. list.push(item)
  25. let res={
  26. code:201,
  27. message:'添加待办事项成功'
  28. }
  29. ret.send(res)
  30. },
  31. //修改操作
  32. 'PUT /api/modify':(req:any,ret:any)=>{
  33. //筛选操作
  34. const {id,status}=req.body
  35. list.map((item,index)=>{
  36. if(item.id===id){
  37. list[index].status=status
  38. }
  39. })
  40. let res={
  41. code:201,
  42. message:'修改办事项成功'
  43. }
  44. ret.send(res)
  45. }
  46. }

5.2请求数据

  1. //services/todo.ts
  2. import request from '@/utils/request';
  3. export async function getTodolistDatas(): Promise<any> {
  4. return request('/api/todo');
  5. }
  6. export async function postTodolistDatas(params:any): Promise<any> {
  7. return request.post('/api/posttodo',{data:params});
  8. }
  9. //发送修改请求操作
  10. export async function modifyTodolistDatas(params:any): Promise<any> {
  11. return request.put('/api/modify',{data:params});
  12. }

5.3在组件中使用

  1. import React, { useState } from 'react';
  2. import { Button, Alert, Modal, message } from 'antd';
  3. import ProTable from '@ant-design/pro-table';
  4. import { PlusOutlined } from '@ant-design/icons';
  5. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  6. import { connect } from 'umi';
  7. import ProForm, { ProFormText } from '@ant-design/pro-form';
  8. import { postTodolistDatas,modifyTodolistDatas } from '@/services/todo';
  9. const PeperWork = (props: any) => {
  10. const [modelFlag, setModel] = useState<boolean>(false);
  11. const formHandle = () => {
  12. setModel(true);
  13. };
  14. const cancelhandle = () => {
  15. setModel(false);
  16. };
  17. const sumbmithandle = async (val: any) => {
  18. const res = await postTodolistDatas(val);
  19. if (res.code === 201) {
  20. props.dispatch({
  21. type: 'todo/getTodolist',
  22. payload: null,
  23. });
  24. message.success(res.message);
  25. setModel(false);
  26. } else {
  27. message.error(res.message);
  28. }
  29. };
  30. const status = [
  31. <Alert message="待办" type="info" showIcon />,
  32. <Alert message="已完成" type="success" showIcon />,
  33. <Alert message="已取消" type="error" showIcon />,
  34. ];
  35. const columns = [
  36. {
  37. title: 'ID',
  38. dataIndex: 'id',
  39. },
  40. {
  41. title: '标题',
  42. dataIndex: 'title',
  43. },
  44. {
  45. title: '状态',
  46. dataIndex: 'status',
  47. render: (_: any, record: any) => {
  48. return status[record.status];
  49. },
  50. },
  51. {
  52. title: '修改状态',
  53. render: (_: any, record: any) => {
  54. let editOption = [];
  55. //这里是修改中状态
  56. if(record.status !== 0){
  57. editOption.push(<a key={0} style={{ marginRight: 20}} onClick={()=>changeStatus(record.id,0)}>待办</a>);
  58. }
  59. if( record.status !==1){
  60. editOption.push(<a key={1} style={{ marginRight: 20}} onClick={()=>changeStatus(record.id,1)}>完成</a>);
  61. }
  62. if(record.status !== 2){
  63. editOption.push(<a key={2} style={{marginRight: 20}} onClick={()=>changeStatus(record.id,2)}>取消</a>);
  64. }
  65. return editOption
  66. },
  67. },
  68. ];
  69. //这里是发送请求改变状态
  70. const changeStatus=async(id:any,status:any)=>{
  71. // 修改状态,调用seivice
  72. const res=await modifyTodolistDatas({
  73. id,
  74. status
  75. })
  76. if(res.code===201){
  77. props.dispatch({
  78. type: 'todo/getTodolist',
  79. payload: null,
  80. });
  81. message.success(res.message);
  82. }
  83. else{
  84. message.error(res.message);
  85. }
  86. //判断执行结果
  87. }
  88. return (
  89. <PageHeaderWrapper>
  90. <ProTable
  91. columns={columns}
  92. rowKey="key"
  93. pagination={{
  94. showQuickJumper: true,
  95. }}
  96. dataSource={props.todo.todoList}
  97. search={false}
  98. dateFormatter="string"
  99. headerTitle="待办事项列表"
  100. toolBarRender={() => [
  101. ,
  102. <Button type="primary" key="primary" onClick={formHandle}>
  103. <PlusOutlined /> 新建
  104. </Button>,
  105. ]}
  106. />
  107. <Modal footer={null} onCancel={cancelhandle} title="待办事项列表" visible={modelFlag}>
  108. <ProForm
  109. onFinish={(values: any) => {
  110. sumbmithandle(values);
  111. }}
  112. >
  113. <ProFormText name="todoList" label="标题" rules={[{ required: true }]} />
  114. </ProForm>
  115. </Modal>
  116. </PageHeaderWrapper>
  117. );
  118. };
  119. export default connect(({ todo }: any) => ({ todo }))(PeperWork);