后台管理系统常用到表单的增删改,这里也做了个封装

例如:user/index.js

import React from 'react'
import { Card, Button, Table, Form, Input, Checkbox,Select,Radio, Icon, message, Modal, DatePicker } from 'antd'
import axios from '../../axios/index'
import Utils from '../../utils/utils'
import ETable from '../../components/ETable/index'
import Moment from 'moment'
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
export default class User extends React.Component{ state = {
list:[]
} params = {
page:1
} requestList = ()=>{
axios.ajax({
url:'/table/list1',
data:{
params:{
page:this.params.page
}
}
}).then((res)=>{
let _this = this;
this.setState({
list:res.result.list.map((item,index)=>{
item.key=index
return item;
}),
pagination:Utils.pagination(res,(current)=>{
_this.params.page = current;
_this.requestList();
})
})
})
} componentDidMount(){
this.requestList();
} // 操作员工
handleOperator = (type)=>{
let item = this.state.selectedItem;
if(type =='create'){
this.setState({
title:'创建员工',
isVisible:true,
type
})
}else if(type=="edit" || type=='detail'){
if(!item){
Modal.info({
title: '信息',
content: '请选择一个用户'
})
return;
}
this.setState({
title:type=='edit'?'编辑用户':'查看详情',
isVisible:true,
userInfo:item,
type
})
}else if(type=="delete"){
if(!item){
Modal.info({
title: '信息',
content: '请选择一个用户'
})
return;
}
Utils.ui.confirm({
text:'确定要删除此用户吗?',
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.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.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: 'interest',
render(interest){
let config = {
'1':'游泳',
'2':'打篮球',
'3':'踢足球',
'4':'跑步',
'5':'爬山',
'6':'骑行',
'7':'桌球',
'8':'麦霸'
}
return config[interest];
}
},{
title: '婚姻',
dataIndex: 'isMarried',
render(isMarried){
return isMarried?'已婚':'未婚'
}
},{
title: '生日',
dataIndex: 'birthday'
},{
title: '联系地址',
dataIndex: 'address'
},{
title: '早起时间',
dataIndex: 'time'
}
];
return (
<div>
<Card>
<Form layout="inline">
<FormItem>
<Input placeholder="请输入用户名"/>
</FormItem>
<FormItem>
<Input type="password" placeholder="请输入密码"/>
</FormItem>
<FormItem>
<Button type="primary">登 录</Button>
</FormItem>
</Form>
</Card>
<Card style={{marginTop:10}}>
<Button type="primary" icon="plus" onClick={()=>this.handleOperator('create')}>创建员工</Button>
<Button icon="edit" onClick={()=>this.handleOperator('edit')}>编辑员工</Button>
<Button onClick={()=>this.handleOperator('detail')}>员工详情</Button>
<Button type="danger" icon="delete" onClick={()=>this.handleOperator('delete')}>删除员工</Button>
</Card>
<div className="content-wrap">
<ETable
columns={columns}
updateSelectedItem={Utils.updateSelectedItem.bind(this)}
selectedRowKeys={this.state.selectedRowKeys}
dataSource={this.state.list}
pagination={this.state.pagination}
/>
</div>
<Modal
title={this.state.title}
visible={this.state.isVisible}
onOk={this.handleSubmit}
width={800}
onCancel={()=>{
this.userForm.props.form.resetFields();
this.setState({
isVisible:false,
userInfo:''
})
}}
>
<UserForm userInfo={this.state.userInfo} type={this.state.type} wrappedComponentRef={(inst) => this.userForm = inst }/>
</Modal>
</div>
);
}
}
class UserForm extends React.Component{ getState = (state)=>{
return {
'1':'咸鱼一条',
'2':'风华浪子',
'3':'北大才子一枚',
'4':'百度FE',
'5':'创业者'
}[state]
} render(){
const { getFieldDecorator } = this.props.form;
const formItemLayout = {
labelCol: {span: 5},
wrapperCol: {span: 16}
};
const userInfo = this.props.userInfo || {};
const type = this.props.type;
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 />
)}
</FormItem>
<FormItem label="联系地址" {...formItemLayout}>
{
userInfo && type=='detail'?userInfo.address:
getFieldDecorator('address',{
initialValue:userInfo.address
})(
<Input.TextArea rows={3} placeholder="请输入联系地址"/>
)}
</FormItem>
</Form>
);
}
}
UserForm = Form.create({})(UserForm);

ETable/index.js

import React from 'react'
import Utils from '../../utils/utils'
import {Table} from 'antd'
import "./index.less"
export default class ETable extends React.Component { state = {}
//处理行点击事件
onRowClick = (record, index) => {
let rowSelection = this.props.rowSelection;
if(rowSelection == 'checkbox'){
let selectedRowKeys = this.props.selectedRowKeys;
let selectedIds = this.props.selectedIds;
let selectedItem = this.props.selectedItem || [];
if (selectedIds) {
const i = selectedIds.indexOf(record.id);
if (i == -1) {//避免重复添加
selectedIds.push(record.id)
selectedRowKeys.push(index);
selectedItem.push(record);
}else{
selectedIds.splice(i,1);
selectedRowKeys.splice(i,1);
selectedItem.splice(i,1);
}
} else {
selectedIds = [record.id];
selectedRowKeys = [index]
selectedItem = [record];
}
this.props.updateSelectedItem(selectedRowKeys,selectedItem || {},selectedIds);
}else{
let selectKey = [index];
const selectedRowKeys = this.props.selectedRowKeys;
if (selectedRowKeys && selectedRowKeys[0] == index){
return;
}
this.props.updateSelectedItem(selectKey,record || {});
}
}; // 选择框变更
onSelectChange = (selectedRowKeys, selectedRows) => {
let rowSelection = this.props.rowSelection;
const selectedIds = [];
if(rowSelection == 'checkbox'){
selectedRows.map((item)=>{
selectedIds.push(item.id);
});
this.setState({
selectedRowKeys,
selectedIds:selectedIds,
selectedItem: selectedRows[0]
});
}
this.props.updateSelectedItem(selectedRowKeys,selectedRows[0],selectedIds);
}; onSelectAll = (selected, selectedRows, changeRows) => {
let selectedIds = [];
let selectKey = [];
selectedRows.forEach((item,i)=> {
selectedIds.push(item.id);
selectKey.push(i);
});
this.props.updateSelectedItem(selectKey,selectedRows[0] || {},selectedIds);
} getOptions = () => {
let p = this.props;
const name_list = {
"订单编号":170,
"车辆编号":80,
"手机号码":96,
"用户姓名":70,
"密码":70,
"运维区域":300,
"车型":42,
"故障编号":76,
"代理商编码":97,
"角色ID":64
};
if (p.columns && p.columns.length > 0) {
p.columns.forEach((item)=> {
//开始/结束 时间
if(!item.title){
return
}
if(!item.width){
if(item.title.indexOf("时间") > -1 && item.title.indexOf("持续时间") <){
item.width = 132
}else if(item.title.indexOf("图片") > -1){
item.width = 86
}else if(item.title.indexOf("权限") > -1 || item.title.indexOf("负责城市") > -1){
item.width = '40%';
item.className = "text-left";
}else{
if(name_list[item.title]){
item.width = name_list[item.title];
}
}
}
item.bordered = true;
});
}
const { selectedRowKeys } = this.props;
const rowSelection = {
type: 'radio',
selectedRowKeys,
onChange: this.onSelectChange,
onSelect:(record, selected, selectedRows)=>{
console.log('...')
},
onSelectAll:this.onSelectAll
};
let row_selection = this.props.rowSelection;
// 当属性未false或者null时,说明没有单选或者复选列
if(row_selection===false || row_selection === null){
row_selection = false;
}else if(row_selection == 'checkbox'){
//设置类型未复选框
rowSelection.type = 'checkbox';
}else{
//默认未单选
row_selection = 'radio';
}
return <Table
className="card-wrap page-table"
bordered
{...this.props}
rowSelection={row_selection?rowSelection:null}
onRow={(record,index) => ({
onClick: ()=>{
if(!row_selection){
return;
}
this.onRowClick(record,index)
}
})}
/>
};
render = () => {
return (
<div>
{this.getOptions()}
</div>
)
}
}

ETable/index.scss

@import '../../style/default';
.ant-table{
&-thead > tr > th,
&-tbody > tr > td{
padding:14px 6px;
text-align:center;
}
.ant-table-selection-column{
min-width:42px!important;
width:42px!important;;
} .text-center {
text-align: center;
}
.text-left {
text-align: left;
}
&.ant-table-middle{
&-thead > tr > th,
&-tbody > tr > td{
padding:10px 6px;
}
}
&.ant-table-small{
&-thead > tr > th,
&-tbody > tr > td{
padding:8px 6px;
}
}
}
.ant-table-pagination{
padding:0 20px;
}

utils/utils.js

import React from 'react';
import { Select } from 'antd'
const Option = Select.Option;
export default {
formateDate(time){
if(!time)return '';
let date = new Date(time);
return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+' '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
},
pagination(data,callback){
return {
onChange:(current)=>{
callback(current)
},
current:data.result.page,
pageSize:data.result.page_size,
total: data.result.total_count,
showTotal:()=>{
return `共${data.result.total_count}条`
},
showQuickJumper:true
}
},
// 格式化金额,单位:分(eg:430分=4.30元)
formatFee(fee, suffix = '') {
if (!fee) {
return 0;
}
return Number(fee).toFixed(2) + suffix;
},
// 格式化公里(eg:3000 = 3公里)
formatMileage(mileage, text) {
if (!mileage) {
return 0;
}
if (mileage >= 1000) {
text = text || " km";
return Math.floor(mileage / 100) / 10 + text;
} else {
text = text || " m";
return mileage + text;
}
},
// 隐藏手机号中间4位
formatPhone(phone) {
phone += '';
return phone.replace(/(\d{3})\d*(\d{4})/g, '$1***$2')
},
// 隐藏身份证号中11位
formatIdentity(number) {
number += '';
return number.replace(/(\d{3})\d*(\d{4})/g, '$1***********$2')
},
getOptionList(data){
if(!data){
return [];
}
let options = [] //[<Option value="0" key="all_key">全部</Option>];
data.map((item)=>{
options.push(<Option value={item.id} key={item.id}>{item.name}</Option>)
})
return options;
},
/**
* ETable 行点击通用函数
* @param {*选中行的索引} selectedRowKeys
* @param {*选中行对象} selectedItem
*/
updateSelectedItem(selectedRowKeys, selectedRows, selectedIds) {
if (selectedIds) {
this.setState({
selectedRowKeys,
selectedIds: selectedIds,
selectedItem: selectedRows
})
} else {
this.setState({
selectedRowKeys,
selectedItem: selectedRows
})
}
},
}

axios/index.js

import JsonP from 'jsonp'
import axios from 'axios'
import { Modal } from 'antd'
export default class Axios {
static jsonp(options) {
return new Promise((resolve, reject) => {
JsonP(options.url, {
param: 'callback'
}, function (err, response) {
if (response.status == 'success') {
resolve(response);
} else {
reject(response.messsage);
}
})
})
} static ajax(options){
let loading;
if (options.data && options.data.isShowLoading !== false){
loading = document.getElementById('ajaxLoading');
loading.style.display = 'block';
}
let baseApi = 'https://www.easy-mock.com/mock/5a7278e28d0c633b9c4adbd7/api';
return new Promise((resolve,reject)=>{
axios({
url:options.url,
method:'get',
baseURL:baseApi,
timeout:5000,
params: (options.data && options.data.params) || ''
}).then((response)=>{
if (options.data && options.data.isShowLoading !== false) {
loading = document.getElementById('ajaxLoading');
loading.style.display = 'none';
}
if (response.status == '200'){
let res = response.data;
if (res.code == '0'){
resolve(res);
}else{
Modal.info({
title:"提示",
content:res.msg
})
}
}else{
reject(response.data);
}
})
});
}
}

效果

getFieldDecorator用法(三)——Table增删改的更多相关文章

  1. Jquery Easy UI初步学习(三)数据增删改

    第二篇只是学了加载用datagrid加载数据,数据的增删改还没有做,今天主要是解决这个问题了. 在做增删改前需要弹出对应窗口,这就需要了解一下EasyUi的弹窗控件. 摘自:http://philoo ...

  2. MySQL在DOS界面对database和table增删改查

    昨天新接触MySQL,学习了一些内容,今天过来复习一下.(吐槽一下:安装个MySQL耗费老子半天时间!!) 学习了一下,大概知道了对数据库基本的增删改查,增add,删drop,改alter,查show ...

  3. JavaJDBC【三、增删改查】

    获取数据库连接后,可进行增删改查操作 语句生成: Statement s = con.createStatement(sql); //生成语句 PreparedStatement ps = (Prep ...

  4. Maybatis的一些总结(三:增删改查)

    回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...

  5. 2017年12月13日 LinQ用法基本的增删改查

    LinQ是什么? LinQ是语言集成的查询,是用于C#跟Vb的扩展语言 LinQ的用法 新建一个App_Code文件夹,在文件夹下添加一个数据LinQ to SQL类,可以直接直接点击服务器管理器然后 ...

  6. table增删改查操作--jq

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. java连接mysql数据库 三 实现增删改查操作

    同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...

  8. MyBatis数据持久化(三)增删改查

    上篇文章中我们使用mybatis成功建立数据库会话,并从表中查询出相应的数据,本文在此基础上介绍MyBatis另外几种操作,即插入.修改.删除记录. 1.修改User.xml文件,增加几条sql语句: ...

  9. 【讲义提纲】以一个实战新闻cms增删改查demo为例,给学院国创队伍培训php

    PHP实战基础——以一个新闻cms的增删改查为例 一.        环境配置 二.        数据库创建 三.        增删改查demo 连接数据库 <?php $link=mysq ...

随机推荐

  1. 【原创】大叔问题定位分享(34)Spring的RestTemplate请求json数据后内容被修改

    先看代码 org.springframework.web.client.RestTemplate public RestTemplate() { this.messageConverters = ne ...

  2. Swagger学习(一、入门)

    简单 入门(效果) SwaggerConfig.class @Configuration //变成配置文件 @EnableSwagger2 //开启swagger2 public class Swag ...

  3. centos 配置rsync+inotify数据实时同步2

    一.Rsync服务简介 1. 什么是Rsync 它是一个远程数据同步工具,它在同步文件的同时,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“rsync算法”来使本地和远程两个主机 ...

  4. 深入理解hadoop数据倾斜

    深入理解hadoop之数据倾斜 1.什么是数据倾斜 我们在用map /reduce程序执行时,有时候会发现reduce节点大部分执行完毕,但是有一个或者几个reduce节点运行很慢,导致整个程序的处理 ...

  5. URL的 ? 和 # (hash),如何将参数保存在URL中,用于刷新获取之前的变量?

    URL中会带上参数,假如是?开头的,那这个是会被加入到ajax请求中的,#(hash)相当于书签锚点,用于定位页面,不会加入到ajax请求中,所以有些时候,我们可以把一些参数放在#后面 如何获取URL ...

  6. 图片上传(3)(组件 -- 图片大小宽高限制)base64图片宽高读取

    1.上传组件(可上传多个文件) <template> <div class="attachmentN"> <span class="btnS ...

  7. C# 基础 字符串 集合 文件操作

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. linux命令详解——iostat

    简介 iostat主要用于监控系统设备的IO负载情况,iostat首次运行时显示自系统启动开始的各项统计信息,之后运行iostat将显示自上次运行该命令以后的统计信息.用户可以通过指定统计的次数和时间 ...

  9. 以tomcat镜像为基础部署war包后再做成镜像

    #以交互的方式启动本地的镜像tomcat:hps,并且将本地目录/mnt/iso挂在到容器中的/tmp/repositories目录,方便从本地获取一些安装文件并进行一些操作 docker run - ...

  10. mysql提示错误[Error Code] 1290 - The MySQL server is running with the --secure-file-priv option解决办法

    1.进入mysql查看secure_file_prive的值 $mysql -u root -p mysql>SHOW VARIABLES LIKE "secure_file_priv ...