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 ...
随机推荐
- squoosh
谷歌在线压缩图片
- 汉明码(hamming code)
hamming code用于磁盘RAID 2中, 关于汉明码的讲解可以看这篇博文,介绍的很详细.最重要是最后的结论: 汉明码属于分组奇偶校验,P4P2P1=000,说明接收方生成的校验位和收到的校验位 ...
- 用php连接数据库,并执行数据库操作
1,建立与数据库之间的连接 (能通过php代码执行一个SQL语句得到查询的结果) <?php mysqli_connect(' , 'demo01'); 这里要注意两个问题: ①mysqli 是 ...
- iframe标签(页面嵌套)
本文链接:https://blog.csdn.net/weixin_44540236/article/details/92760494 两个不同的页面但是它们的基本框架都是一样,每点击一次左边的导航栏 ...
- 阿里云 elasticsearch 增删改查
kibana 控制台 # 查询所有数据 GET /yixiurds_dev/_search { "query": { "match_all": { } } } ...
- 第三篇.python编辑器和集成环境01
修改python的镜像源 使用pip可以提高网速 \Lib\site-packages\pip\models\index.py文件,将PYPI的值改为你所需要的镜像源即可,例如改为豆瓣镜像源: #Py ...
- Linux计划任务与压缩归档
计划任务分为两种形式 第一种:定时性的:也就是例行,每隔一定的周期就要重复来做这个任务. 第二种:突发性的:临时决定,只执行一次的任务. 用到的命令有两个 at:它是一个可以处理仅执行一次的任务就结束 ...
- linux基础—课堂随笔010_系统启动和内核管理
系统启动和内核管理 Linux: kernel+rootfs kernel: 进程管理.内存管理.网络管理.驱动程序.文件系统.安全功能 rootfs:程序和glibc 库:函数集合, functio ...
- 版本控制工具 svn 一
一.svn 概述 1).svn的作用 1.多人协作开发:2.远程控制:3.版本控制 2).软件控制管理工具发展之路 SCM:软件配置管理,所谓的软件配置管理实际就是软件源代码的 控制与管理. CVS: ...
- nagios-调用脚本
在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控.检测功能都是通过各种插件来完成的. 启动Nagios ...