前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录。最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star。


一、创建员工表单子组件

  • 创建员工、编辑员工、员工详情、删除员工共用一个Modal弹框表单

    <Modal
    title={this.state.title}
    visible={this.state.isVisible}
    onOk={this.handleSubmit}
    onCancel={() => {
    this.userForm.props.form.resetFields();
    this.setState({
    isVisible: false
    })
    }}
    width={600}
    {...footer}
    >
    <UserForm type={this.state.type} userInfo={this.state.userInfo} wrappedComponentRef={(inst) => {this.userForm = inst;}}/>
    </Modal>  
  • 创建、编辑员工提交:共用handleSubmit(),判断type,通过axios.ajax()调用不同的Easy Mock数据接口请求
    //创建编辑员工提交
    handleSubmit = () => {
    let type = this.state.types;
    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
    })
    this.requestList();
    }
    })
    }    
  • 关键:
  1. getFieldDecorator实现表单数据双向绑定

     const {getFieldDecorator} = this.props.form;  
  2. 获取this.props.userInfo:【编辑员工】中设置表单默认数据、【员工信息】中显示员工信息
  3. 判断this.props.type:当type == 'detail'时,直接渲染员工信息userInfo,不再渲染Form表单
  4. 判断this.state.type:当type == 'detail'时,不显示Modal的footer按钮
    let footer = {};
    if(this.state.type == 'detail'){
    footer = {
    footer: null
    }
    }  
  • 组件实现

    //子组件:创建员工表单
    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 {getFieldDecorator} = this.props.form;
    const formItemLayout= {
    labelCol:{span: 5},
    wrapperCol:{span: 19}
    }
    return (
    <Form layout="horizontal">
    <FormItem label="用户名" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.userName :
    getFieldDecorator('user_name',{
    initialValue: userInfo.userName
    })(
    <Input type="text" placeholder="请输入用户名"/>
    )
    }
    </FormItem>
    <FormItem label="性别" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.sex == 1 ? '男' : '女' :
    getFieldDecorator('sex',{
    initialValue: userInfo.sex
    })(
    <RadioGroup>
    <Radio value={1}>男</Radio>
    <Radio value={2}>女</Radio>
    </RadioGroup>
    )
    }
    </FormItem>
    <FormItem label="状态" {...formItemLayout}>
    {
    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}>
    {
    type == 'detail' ? userInfo.birthday :
    getFieldDecorator('birthday',{
    initialValue: moment(userInfo.birthday)
    })(
    <DatePicker format="YYYY-MM-DD"/>
    )
    }
    </FormItem>
    <FormItem label="联系地址" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.address :
    getFieldDecorator('address',{
    initialValue: userInfo.address
    })(
    <TextArea rows={3} placeholder="请输入联系地址"/>
    )
    }
    </FormItem>
    </Form>
    )
    }
    }
    UserForm = Form.create({})(UserForm);

      

二、功能区操作

  • 创建员工、编辑员工、员工详情、删除员工共用一个功能操作函数handleOperate()

     <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="primary" icon="delete" onClick={() => this.handleOperate('delete')}>删除员工</Button>
    </Card>
  • 关键:传入不同的参数[type],通过判断type的值,执行不同的操作

     //功能区操作
    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(type == 'delete'){
    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
    })
    _this.requestList();
    }
    })
    }
    })
    }
    } 
  • 实例代码:

    import React from 'react'
    import {Card, Button, Form, Input, Select,Radio, Icon, Modal, DatePicker} from 'antd'
    import axios from './../../axios'
    import Utils from './../../utils/utils'
    import BaseForm from './../../components/BaseForm'
    import ETable from './../../components/ETable'
    import moment from 'moment'
    const FormItem = Form.Item;
    const RadioGroup = Radio.Group;
    const TextArea = Input.TextArea;
    const Option = Select.Option; export default class User extends React.Component{ state = {
    list:[],
    isVisible: false
    } params = {
    page: 1
    } formList = [
    {
    type: 'INPUT',
    label: '用户名',
    field: 'user_name',
    placeholder: '请输入名称',
    width: 130
    },
    {
    type: 'INPUT',
    label: '手机号',
    field: 'user_mobile',
    placeholder: '请输入手机号',
    width: 130
    },
    {
    type: 'DATE',
    label: '入职日期',
    field: 'user_date',
    placeholder: '请输入日期'
    }
    ] componentDidMount(){
    this.requestList();
    } handleFilter = (params) => {
    this.params = params;
    this.requestList();
    } requestList = () => {
    axios.requestList(this, '/table/list1', this.params);
    } //功能区操作
    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(type == 'delete'){
    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
    })
    _this.requestList();
    }
    })
    }
    })
    }
    } //创建编辑员工提交
    handleSubmit = () => {
    let type = this.state.types;
    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
    })
    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: 'isMarried',
    render(isMarried){
    return isMarried == 1 ?'已婚':'未婚'
    }
    },{
    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="primary" 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
    })
    }}
    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 {getFieldDecorator} = this.props.form;
    const formItemLayout= {
    labelCol:{span: 5},
    wrapperCol:{span: 19}
    }
    return (
    <Form layout="horizontal">
    <FormItem label="用户名" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.userName :
    getFieldDecorator('user_name',{
    initialValue: userInfo.userName
    })(
    <Input type="text" placeholder="请输入用户名"/>
    )
    }
    </FormItem>
    <FormItem label="性别" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.sex == 1 ? '男' : '女' :
    getFieldDecorator('sex',{
    initialValue: userInfo.sex
    })(
    <RadioGroup>
    <Radio value={1}>男</Radio>
    <Radio value={2}>女</Radio>
    </RadioGroup>
    )
    }
    </FormItem>
    <FormItem label="状态" {...formItemLayout}>
    {
    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}>
    {
    type == 'detail' ? userInfo.birthday :
    getFieldDecorator('birthday',{
    initialValue: moment(userInfo.birthday)
    })(
    <DatePicker format="YYYY-MM-DD"/>
    )
    }
    </FormItem>
    <FormItem label="联系地址" {...formItemLayout}>
    {
    type == 'detail' ? userInfo.address :
    getFieldDecorator('address',{
    initialValue: userInfo.address
    })(
    <TextArea rows={3} placeholder="请输入联系地址"/>
    )
    }
    </FormItem>
    </Form>
    )
    }
    }
    UserForm = Form.create({})(UserForm);

注:项目来自慕课网

【共享单车】—— React后台管理系统开发手记:员工管理之增删改查的更多相关文章

  1. 【共享单车】—— React后台管理系统开发手记:主页面架构设计

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  2. 【共享单车】—— React后台管理系统开发手记:Redux集成开发

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  3. 【共享单车】—— React后台管理系统开发手记:项目工程化开发

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  4. 【共享单车】—— React后台管理系统开发手记:城市管理和订单管理

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  5. 【共享单车】—— React后台管理系统开发手记:AntD Form基础组件

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  6. 【共享单车】—— React后台管理系统开发手记:Router 4.0路由实战演练

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  7. 【共享单车】—— React后台管理系统开发手记:权限设置和菜单调整(未完)

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  8. 【共享单车】—— React后台管理系统开发手记:AntD Table高级表格

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

  9. 【共享单车】—— React后台管理系统开发手记:AntD Table基础表格

    前言:以下内容基于React全家桶+AntD实战课程的学习实践过程记录.最终成果github地址:https://github.com/66Web/react-antd-manager,欢迎star. ...

随机推荐

  1. AirPlay、DLNA、Miracast三大无线技术介绍

    小米盒子之AirPlay.DLNA.Miracast三大无线技术介绍 米官方称小米盒子的米联功能可以将小米手机或iPhone.iPad上的图片.音乐.视频等精彩内容投射到电视上,让你感受大屏的刺激.而 ...

  2. 走近Docker

    一个容器实际上就是运行在宿主机上的一个进程,这个进程以及子进程会认为自己运行在一个独立的世界里. Docker相对于其他虚拟化技术的优势在于:创建.删除容器速度快,容器运行占用开销非常小.而相对于其他 ...

  3. 结构型设计模式之装饰模式(Decorator)

    结构 意图 动态地给一个对象添加一些额外的职责.就增加功能来说,D e c o r a t o r 模式相比生成子类更为灵活. 适用性 在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. ...

  4. [译] 如何像 Python 高手一样编程?

    转自:http://www.liuhaihua.cn/archives/23475.html Harries 发布于 7天前 分类:编程技术 阅读(15) 评论(0) 最近在网上看到一篇介绍Pytho ...

  5. VIM使用系列: 复制并移动文本

    1 5. 复制并移动文本 *copy-move* 2 3 *quote* 4 "{a-zA-Z0-9.%#:-"} 指定下次的删除.抽出和放置命令使用的寄存器 5 {a-zA-Z0 ...

  6. Fiddler抓包5-接口测试(Composer)【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/p/6754560.html 前言 Fiddler最大的优势在于抓包,我们大部分使用的功能也在抓 ...

  7. hdu 5146(水题)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. 【原创】Oracle 11g R2 Client安装配置说明(多图详解)

    1. 准备工作 安装Oracle11gR2client的时候,如果刚从网上下载的Oracle client,可能无法再2008 R2或者2012 R2的服务器上面运行. 报错:[INS-13001]环 ...

  9. 天猫首页迷思之-jquery实现整个div的懒加载(1)

    懒加载是众所周知的减少网页负载,提高性能的方法,不少大型用图片用的多的网站都用到了. 于是我网上一搜,得到一插件:jquery.lazyload    网址:http://www.appelsiini ...

  10. Tomcat配置连接池

    Tomcat配置DBCP连接池 配置tomcat服务器的时候,使用到jndi;通过Context配置文件实现配置池对象,通过new initialConext()对象的lookup()获取到数据池对象 ...