(记录笔记10)——10.1 员工管理

一 基础页面部分

Github地址

1.1 easy-mock: user/list 用户列表

{
  "code": 0,
  "msg": "",
  "result": {
    "item_list|100": [{
      "id|+1": 1,
      "username": "@cname",
      "sex|1-2": 1,
      "state|1-5": 1,
      "interest|1-8": 1,
      "isMarried|0-1": 1,
      "birthday": "2000-01-01",
      "address": "北京市海淀区",
      "time": "09:00:00"
    }],
    page: 1,
    page_size: 10,
    total: 30
  }
}

1.2 实例代码

import React from 'react';
import {Card, Button} from "antd";
import axios from './../../axios';
import Utils from './../../utils/utils';
import ETable from './../../components/ETable';
import BaseForm from './../../components/BaseForm';

export default class User extends React.Component {

  params = {
    page: 1
  };

  state = {

  }

  formList = [
    {
      type: 'INPUT',
      label: '用户名',
      field: 'user_name',
      placeholder: '请输入用户名称',
      width: 130,
    }, {
      type: 'INPUT',
      label: '用户手机号',
      field: 'user_mobile',
      placeholder: '请输入用户手机号',
      width: 140,
    }, {
      type: 'DATE',
      label: '请选择入职日期',
      field: 'user_date',
      placeholder: '请输入日期',
    }
  ];

  componentDidMount() {
    this.requestList();
  }

  // 处理表单查询
  handleFilter = (params) => {
    this.params = params; // 从子组件传来的值赋值给 params
    this.requestList();
  };

  requestList = () => {
    axios.requestList(this, '/user/list', this.params, true);
  };


  render() {
    const columns = [
      {
        title: 'id',
        dataIndex: 'id'
      }, {
        title: '用户名',
        dataIndex: 'username'
      }, {
        title: '性别',
        dataIndex: 'sex',
        render(sex) {
          return sex === 1 ? '男' : '女';
        }
      }, {
        title: '状态',
        dataIndex: 'state',
        render(state) {
          let config = {
            '1': "咸?一条",
            '2': '风华浪子',
            '3': '北大才子一枚',
            '4': '百度FE',
            '5': '创业者',
          };
          return config[state];
        }
      }, {
        title: '爱好',
        dataIndex: 'interest',
        render(abc) {
          let config = {
            '1': '?‍',
            '2': '?',
            '3': '⚽',
            '4': '?',
            '5': '?',
            '6': '?',
            '7': '?',
            '8': '?',
          };
          return config[abc];
        }
      }, {
        title: '生日',
        dataIndex: 'birthday'
      }, {
        title: '联系地址',
        dataIndex: 'address'
      }, {
        title: '早起时间',
        dataIndex: 'time'
      },
    ];
    return (
      <div>
        <Card>
          <BaseForm formList={this.formList} filterSubmit={this.handleFilter}/>
        </Card>

        <Card style={{marginTop: 10}}>
          <Button type="primary">创建员工</Button>
          <Button type="primary" style={{marginLeft: 20}}>编辑员工</Button>
          <Button type="primary" style={{marginLeft: 20}}>员工详情</Button>
          <Button type="danger" style={{marginLeft: 20}}>删除员工</Button>
        </Card>

        <div className="content-wrap">
          <ETable
            columns={columns}
            updateSelectedItem={Utils.updateSelectedItem.bind(this)}
            selectedRowKeys={this.state.selectedRowKeys}
            selectedItem={this.state.selectedItem}
            dataSource={this.state.list}
            pagination={this.state.pagination}
          />
        </div>
      </div>
    );
  }
}


二 增加弹框功能

Github

关闭footer footer={null}

使用时,要看接口返回的是什么类型

value = ‘1’ 字符串类型1

value = {1} 数字类型1

2.1 数据接口

easy-mock :user/add 添加员工

{
  "code": 0,
  "result": "Ok"
}

user/edit:用户编辑

{
  "code": 0,
  "result": 'Ok'
}

user/delete:用户删除

{
  "code": 0
}

点击提交清除表单数据:

this.userForm.props.form.resetFields();

2.2 实例代码

import React from 'react';
import {Card, Button, Modal, Form, Input, Radio, Select, DatePicker} from "antd";
import axios from './../../axios';
import Utils from './../../utils/utils';
import ETable from './../../components/ETable';
import BaseForm from './../../components/BaseForm';
import moment from "moment";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;
const Option = Select.Option;
const {TextArea} = Input;

export default class User extends React.Component {

  params = {
    page: 1
  };

  state = {
    isVisible: false
  };

  formList = [
    {
      type: 'INPUT',
      label: '用户名',
      field: 'user_name',
      placeholder: '请输入用户名称',
      width: 130,
    }, {
      type: 'INPUT',
      label: '用户手机号',
      field: 'user_mobile',
      placeholder: '请输入用户手机号',
      width: 140,
    }, {
      type: 'DATE',
      label: '请选择入职日期',
      field: 'user_date',
      placeholder: '请输入日期',
    }
  ];

  componentDidMount() {
    this.requestList();
  }

  // 处理表单查询
  handleFilter = (params) => {
    this.params = params; // 从子组件传来的值赋值给 params
    this.requestList();
  };

  requestList = () => {
    axios.requestList(this, '/user/list', this.params, true);
  };

  // 功能区操作
  handleOperate = (type) => {
    let item = this.state.selectedItem;
    if (type == 'create') {
      this.setState({
        type,
        isVisible: true,
        title: '创建员工'
      });
    } else if (type == 'edit') {
      if (!item) {
        Modal.info({
          title: '提示',
          content: '请选择一个用户'
        });
        return;
      }
      this.setState({
        type,
        isVisible: true,
        title: '编辑员工',
        userInfo: item
      });
    } else if (type == 'detail') {
      if (!item) {
        Modal.info({
          title: '提示',
          content: '请选择一个用户'
        });
        return;
      }

      this.setState({
        type,
        isVisible: true,
        title: '员工详情',
        userInfo: item
      });
    } else {
      if (!item) {
        Modal.info({
          title: '提示',
          content: '请选择一个用户'
        });
        return;
      }
      let _this = this;
      Modal.confirm({
        title: '确认删除',
        content: '是否要删除当前选中的员工' + item.id,
        onOk() {
          axios.ajax({
            url: '/user/delete',
            data: {
              params: {
                id: item.id
              }
            }
          }).then((res) => {
            if (res.code == 0) {
              _this.setState({
                isVisible: false,
                // selectedRowKeys:'' // 点击删除,单选框失去焦点:  空: null ''   参考网址https://blog.csdn.net/oscar999/article/details/9353713
              });
              _this.requestList();
            }
          });
        }
      });
    }
  };

  // 创建员工提交
  handleSubmit = () => {
    let type = this.state.type;
    let data = this.userForm.props.form.getFieldsValue();
    axios.ajax({
      url: type == 'create' ? '/user/add' : '/user/edit',
      data: {
        params: data
      }
    }).then((res) => {
      if (res.code == 0) {
        this.userForm.props.form.resetFields();
        this.setState({
          isVisible: false,
          // selectedRowKeys:'' // 查询完后,单选框失去焦点
        });
        this.requestList();
      }
    });
  };

  render() {
    const columns = [
      {
        title: 'id',
        dataIndex: 'id'
      }, {
        title: '用户名',
        dataIndex: 'username'
      }, {
        title: '性别',
        dataIndex: 'sex',
        render(sex) {
          return sex == 1 ? '男' : '女';
        }
      }, {
        title: '状态',
        dataIndex: 'state',
        render(state) {
          let config = {
            '1': "咸?一条",
            '2': '风华浪子',
            '3': '北大才子一枚',
            '4': '百度FE',
            '5': '创业者',
          };
          return config[state];
        }
      }, {
        title: '爱好',
        dataIndex: 'interest',
        render(abc) {
          let config = {
            '1': '?‍',
            '2': '?',
            '3': '⚽',
            '4': '?',
            '5': '?',
            '6': '?',
            '7': '?',
            '8': '?',
          };
          return config[abc];
        }
      }, {
        title: '生日',
        dataIndex: 'birthday'
      }, {
        title: '联系地址',
        dataIndex: 'address'
      }, {
        title: '早起时间',
        dataIndex: 'time'
      },
    ];

    let footer = {};

    if (this.state.type == 'detail') {
      footer = {
        footer: null
      };
    }

    return (
      <div>
        <Card>
          <BaseForm formList={this.formList} filterSubmit={this.handleFilter}/>
        </Card>

        <Card style={{marginTop: 10}} className="operate-wrap">
          <Button type="primary" icon="plus" onClick={() => this.handleOperate('create')}>创建员工</Button>
          <Button type="primary" icon="edit" onClick={() => this.handleOperate('edit')}>编辑员工</Button>
          <Button type="primary" onClick={() => this.handleOperate('detail')}>员工详情</Button>
          <Button type="danger" icon="delete" onClick={() => this.handleOperate('delete')}>删除员工</Button>
        </Card>

        <div className="content-wrap">
          <ETable
            columns={columns}
            updateSelectedItem={Utils.updateSelectedItem.bind(this)}
            selectedRowKeys={this.state.selectedRowKeys}
            selectedItem={this.state.selectedItem}
            dataSource={this.state.list}
            pagination={this.state.pagination}
          />
        </div>
        <Modal
          title={this.state.title}
          visible={this.state.isVisible}
          onOk={this.handleSubmit}
          onCancel={() => {
            this.userForm.props.form.resetFields();
            this.setState({
              isVisible: false,
              userInfo: ''
            });
          }}
          width={600}
          {...footer}
        >
          <UserForm type={this.state.type} userInfo={this.state.userInfo}
                    wrappedComponentRef={(inst) => this.userForm = inst}/>
        </Modal>
      </div>
    );
  }
}

class UserForm extends React.Component {

  getState = (state) => {
    let config = {
      '1': "咸?一条",
      '2': '风华浪子',
      '3': '北大才子一枚',
      '4': '百度FE',
      '5': '创业者',
    };
    return config[state];
  };

  render() {
    let type = this.props.type;
    let userInfo = this.props.userInfo || {};
    const formItemLayout = {
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 19
      }
    };

    const {getFieldDecorator} = this.props.form;
    return (
      <Form layout="horizontal">
        <FormItem label="用户名" {...formItemLayout}>
          {
            userInfo && type == 'detail' ? userInfo.username :
              getFieldDecorator('user_name', {
                initialValue: userInfo.username
              })(
                <Input type="text" placeholder="请输入用户名"/>
              )
          }
        </FormItem>
        <FormItem label="性别" {...formItemLayout}>
          {
            userInfo && type == 'detail' ? userInfo.sex == 1 ? '男' : '女' :
              getFieldDecorator('sex', {
                initialValue: userInfo.sex
              })(
                <RadioGroup>
                  <Radio value={1}></Radio>
                  <Radio value={2}></Radio>
                </RadioGroup>
              )
          }
        </FormItem>
        <FormItem label="状态" {...formItemLayout}>
          {
            userInfo && type == 'detail' ? this.getState(userInfo.state) :
              getFieldDecorator('state', {
                initialValue: userInfo.state
              })(
                <Select>
                  <Option value={1}>?一条</Option>
                  <Option value={2}>风华浪子</Option>
                  <Option value={3}>北大才子一枚</Option>
                  <Option value={4}>百度FE</Option>
                  <Option value={5}>创业者</Option>
                </Select>
              )
          }
        </FormItem>

        <FormItem label="生日" {...formItemLayout}>
          {
            userInfo && type == 'detail' ? userInfo.birthday :
              getFieldDecorator('birthday', {
                initialValue: moment(userInfo.birthday)
              })(
                <DatePicker format="YYYY-MM-DD"/>
              )}
        </FormItem>

        <FormItem label="联系地址" {...formItemLayout}>
          {
            userInfo && type == 'detail' ? userInfo.address :
              getFieldDecorator('address', {
                initialValue: userInfo.address
              })(
                <TextArea rows={3} placeholder="请输入联系地址"/>
              )}
        </FormItem>
      </Form>
    );
  }
}

UserForm = Form.create({})(UserForm);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目前大家比较熟悉共享单车的使用。请编制一个共享单车的管理程序,实现如下基本功能。假设有5种品牌的共享单车(品牌内容自定)。 针对该5种品牌的共享单车,自行设计一套包含每种单车的品牌名称、投放量、投放点、某一时间点的在用数量、每辆车的每天骑行次数及单次里程和总里程、开锁过程中发现的损坏次数等信息(所有相关数据均自行设计)的数据结构; 随着骑行活动的开展,待使用单车的数量将发生变化。要求能对每种单车的使用数量及待使用的数量进行查询统计并输出; 对于某一投放点的某一品牌的单车,如果无备用车(待使用的车均为备用车),或备用车均为损坏的车,系统应能给出信息提示; 对于损坏报修的车辆,系统能够进行及时的统计,并能在投放数量中削减损坏车辆的数量,形成真实的有效投放量; 能够对客户信息进行处理,包括注册的用户名、电话号码、骑行里程、骑行习惯(比如70%以上的出行时间集中在某个时间段,时间段按时钟整点划分)、每天平均的骑行时间等; 该系统能进行当日使用状况的统计,请用链表排序(排序算法不限)提示交易使用次数排在前三名的单车品牌; 假设每种单车的使用是收费的,如第一个小时是免费的,第二个小时开始每小时收费0.5元,各品牌可各自推出优惠收费条件(优惠条件请自定义),然后根据假设的使用情况,统计出各种品牌的日营业额,并对各品牌的受欢迎程度进行排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小李科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值