getFieldDecorator用法(三)——Table增删改
后台管理系统常用到表单的增删改,这里也做了个封装
例如: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增删改的更多相关文章
- Jquery Easy UI初步学习(三)数据增删改
第二篇只是学了加载用datagrid加载数据,数据的增删改还没有做,今天主要是解决这个问题了. 在做增删改前需要弹出对应窗口,这就需要了解一下EasyUi的弹窗控件. 摘自:http://philoo ...
- MySQL在DOS界面对database和table增删改查
昨天新接触MySQL,学习了一些内容,今天过来复习一下.(吐槽一下:安装个MySQL耗费老子半天时间!!) 学习了一下,大概知道了对数据库基本的增删改查,增add,删drop,改alter,查show ...
- JavaJDBC【三、增删改查】
获取数据库连接后,可进行增删改查操作 语句生成: Statement s = con.createStatement(sql); //生成语句 PreparedStatement ps = (Prep ...
- Maybatis的一些总结(三:增删改查)
回顾一个点 之前不懂这句: UserMapper userMapper = sqlSession.getMapper(UserMapper.class); 现在理解了一点点,相当于实现了userMap ...
- 2017年12月13日 LinQ用法基本的增删改查
LinQ是什么? LinQ是语言集成的查询,是用于C#跟Vb的扩展语言 LinQ的用法 新建一个App_Code文件夹,在文件夹下添加一个数据LinQ to SQL类,可以直接直接点击服务器管理器然后 ...
- table增删改查操作--jq
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- java连接mysql数据库 三 实现增删改查操作
同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...
- MyBatis数据持久化(三)增删改查
上篇文章中我们使用mybatis成功建立数据库会话,并从表中查询出相应的数据,本文在此基础上介绍MyBatis另外几种操作,即插入.修改.删除记录. 1.修改User.xml文件,增加几条sql语句: ...
- 【讲义提纲】以一个实战新闻cms增删改查demo为例,给学院国创队伍培训php
PHP实战基础——以一个新闻cms的增删改查为例 一. 环境配置 二. 数据库创建 三. 增删改查demo 连接数据库 <?php $link=mysq ...
随机推荐
- WAV格式文件无损合并&帧头数据体解析(python)(原创)
一,百度百科 WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范,用于保存Windows平台的音频 ...
- windows安装npm教程
1.在使用之前,先类掌握3个东西,明白它们是用来干什么的: npm: nodejs 下的包管理器. webpack: 它主要用途是通过CommonJS 的语法把所有浏览器端需要发布的静态资源作相应的 ...
- 客户端相关知识学习(八)之Android“.9.png”
参考 Android中.9图片的含义及制作教程 .9.png Android .9.png 的介绍
- ubuntu根目录下空间不足,syslog占用很大空间,如何清理?
一激动差点儿删除,以下清理方式是对的 cat /dev/null > /var/log/syslog
- Samba编码设置方法
弟管理學校的網頁伺服器,該伺服器也同時是大家的分享檔案集散中心,是以Linux架設起來的,該伺服器以 Unicode 作為系統編碼,而其他Windows系統則是big5(MS950)編碼,最近我要讓另 ...
- Delphi 使用数据库浏览器
樊伟胜
- Oracle【二维表的维护】
二维表的维护 --添加新的字段:alter table 表名 add 字段名 类型 [一般不加约束条件] ) 原表:新增字段后的表:修改原有的字段:[修改字段类型.修改字段名.删除字段] --修改字段 ...
- servlet遇到的问题
1 创建web项目没有xml自动生成 2 servlet 忽然报奇怪500错误 出现的BUG原因 JAVA bean没有设置 自动导入了其他User包
- 下拉菜单 Spinner 简单纯字符串版
下拉菜单 Spinner 简单纯字符串版 public class MainActivity extends Activity implements AdapterView.OnItemSelecte ...
- 内核模式构造-Mutex构造(RecursiveAutoResetEvent)
internal sealed class RecursiveAutoResetEvent : IDisposable { private AutoResetEvent m_lock = new Au ...