From cadfd4ed559fb170bb81d5bafbb6ae9775ba1ddc Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期四, 03 三月 2022 18:18:39 +0800
Subject: [PATCH] 2022-03-03

---
 src/tabviews/custom/components/table/edit-table/normalTable/index.jsx |  632 +++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 435 insertions(+), 197 deletions(-)

diff --git a/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx b/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx
index 19a7ed6..987b60d 100644
--- a/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx
+++ b/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx
@@ -1,15 +1,19 @@
 import React, {Component} from 'react'
 import PropTypes from 'prop-types'
 import { is, fromJS } from 'immutable'
-import { Table, Typography, Icon, Switch, Modal, Input, InputNumber, Tooltip, Button, notification, message } from 'antd'
+import { Table, Typography, Switch, Modal, Input, InputNumber, Tooltip, Button, notification, message, Select } from 'antd'
+import { ExclamationCircleOutlined, EditOutlined, PlusOutlined, PlusCircleOutlined, DeleteOutlined } from '@ant-design/icons'
 import moment from 'moment'
 
 import Api from '@/api'
+import options from '@/store/options.js'
 import asyncComponent from '@/utils/asyncComponent'
-import Utils, { getEditTableSql } from '@/utils/utils.js'
+import Utils, { getEditTableSql, getMark } from '@/utils/utils.js'
+import MkIcon from '@/components/mk-icon'
 import MKEmitter from '@/utils/events.js'
 import zhCN from '@/locales/zh-CN/main.js'
 import enUS from '@/locales/en-US/main.js'
+import CusSwitch from './cusSwitch'
 import '@/assets/css/table.scss'
 import './index.scss'
 
@@ -99,77 +103,6 @@
     err: null
   }
 
-  getMark = (record, marks, style, content) => {
-    marks.some(mark => {
-      let originVal = record[mark.field[0]] + ''
-      let contrastVal = ''
-      let result = false
-
-      if (mark.field[1] === 'static') {
-        contrastVal = mark.contrastValue + ''
-      } else {
-        contrastVal = record[mark.field[2]] + ''
-      }
-
-      if (mark.match === '=') {
-        result = originVal === contrastVal
-      } else if (mark.match === '!=') {
-        result = originVal !== contrastVal
-      } else if (mark.match === 'like') {
-        result = originVal.indexOf(contrastVal) > -1
-      } else if (mark.match === '>') {
-        try {
-          originVal = parseFloat(originVal)
-          contrastVal = parseFloat(contrastVal)
-        } catch (e) {
-          originVal = NaN
-        }
-
-        if (!isNaN(originVal) && !isNaN(contrastVal) && originVal > contrastVal) {
-          result = true
-        }
-      } else if (mark.match === '<') {
-        try {
-          originVal = parseFloat(originVal)
-          contrastVal = parseFloat(contrastVal)
-        } catch (e) {
-          originVal = NaN
-        }
-
-        if (!isNaN(originVal) && !isNaN(contrastVal) && originVal < contrastVal) {
-          result = true
-        }
-      }
-
-      if (result) {
-        if (mark.signType[0] === 'font') {
-          style.color = mark.color
-        } else if (mark.signType[0] === 'background') {
-          style.background = mark.color
-          if (mark.fontColor) {
-            style.color = mark.fontColor
-          }
-        } else if (mark.signType[0] === 'underline') {
-          style.textDecoration = 'underline'
-          style.color = mark.color
-        } else if (mark.signType[0] === 'line-through') {
-          style.textDecoration = 'line-through'
-          style.color = mark.color
-        } else if (mark.signType[0] === 'icon') {
-          let icon = (<Icon style={{color: mark.color}} type={mark.signType[3]} />)
-          if (mark.signType[1] === 'front') {
-            content = <span>{icon} {content}</span>
-          } else {
-            content = <span>{content} {icon}</span>
-          }
-        }
-      }
-      return result
-    })
-
-    return content
-  }
-
   shouldComponentUpdate (nextProps, nextState) {
     return !is(fromJS(this.props.record), fromJS(nextProps.record)) ||
       nextState.editing !== this.state.editing ||
@@ -193,7 +126,7 @@
   tdFocus = (id) => {
     const { col, record } = this.props
 
-    if (id !== col.uuid + record.$Index) return
+    if (id !== col.uuid + record.$$uuid) return
     this.focus()
   }
 
@@ -204,9 +137,9 @@
     this.setState({editing: false})
     setTimeout(() => {
       if (col.enter === '$next') {
-        MKEmitter.emit('nextLine', col, record.$Index)
+        MKEmitter.emit('nextLine', col, record.$$uuid)
       } else {
-        MKEmitter.emit('tdFocus', col.enter + record.$Index)
+        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
       }
     }, 50)
 
@@ -218,27 +151,34 @@
   focus = () => {
     const { col, record } = this.props
 
-    let err = null
-    let val = record[col.field] !== undefined ? record[col.field] : ''
-
-    if (col.type === 'number') {
-      val = +val
-      if (isNaN(val)) {
-        val = 0
+    if (col.editType === 'switch' || col.editType === 'select') {
+      this.setState({editing: true}, () => {
+        let node = document.getElementById(col.uuid + record.$$uuid)
+        node && node.click()
+      })
+    } else {
+      let err = null
+      let val = record[col.field] !== undefined ? record[col.field] : ''
+  
+      if (col.type === 'number') {
+        val = +val
+        if (isNaN(val)) {
+          val = 0
+        }
+        if (typeof(col.max) === 'number' && val > col.max) {
+          err = col.label + '鏈�澶т负' + col.max
+        } else if (typeof(col.min) === 'number' && val < col.min) {
+          err = col.label + '鏈�灏忎负' + col.min
+        }
+      } else if (col.required === 'true' && !val) {
+        err = '璇峰~鍐�' + col.label
       }
-      if (typeof(col.max) === 'number' && val > col.max) {
-        err = col.label + '鏈�澶т负' + col.max
-      } else if (typeof(col.min) === 'number' && val < col.min) {
-        err = col.label + '鏈�灏忎负' + col.min
-      }
-    } else if (col.required === 'true' && !val) {
-      err = '璇峰~鍐�' + col.label
+  
+      this.setState({editing: true, value: val, err}, () => {
+        let node = document.getElementById(col.uuid + record.$$uuid)
+        node && node.select()
+      })
     }
-
-    this.setState({editing: true, value: val, err}, () => {
-      let node = document.getElementById(col.uuid + record.$Index)
-      node && node.select()
-    })
   }
 
   onBlur = () => {
@@ -273,6 +213,62 @@
     this.setState({value: val, err})
   }
 
+  onSwitchChange = (val) => {
+    const { col, record } = this.props
+
+    this.setState({editing: false})
+
+    setTimeout(() => {
+      if (col.enter === '$next') {
+        MKEmitter.emit('nextLine', col, record.$$uuid)
+      } else {
+        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
+      }
+    }, 50)
+
+    MKEmitter.emit('changeRecord', col.tableId, {...record, [col.field]: val})
+  }
+
+  onSelectChange = (val, option) => {
+    const { col, record } = this.props
+
+    let values = {}
+    let _option = col.options.filter(m => m.key === option.key)[0]
+
+    if (_option) {
+      if (col.linkSubField) {
+        col.linkSubField.forEach(m => {
+          values[m] = _option[m] !== undefined ? _option[m] : ''
+        })
+      }
+
+      if (col.editField) {
+        values[col.field] = _option.label
+        values[col.editField] = val
+      } else {
+        values[col.field] = val
+      }
+    }
+
+    this.setState({editing: false})
+
+    setTimeout(() => {
+      if (col.enter === '$next') {
+        MKEmitter.emit('nextLine', col, record.$$uuid)
+      } else {
+        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
+      }
+    }, 50)
+
+    MKEmitter.emit('changeRecord', col.tableId, {...record, ...values})
+  }
+
+  switchBlur = () => {
+    setTimeout(() => {
+      this.setState({editing: false})
+    }, 10)
+  }
+
   render() {
     let { col, config, record, style, className } = this.props
     const { editing, value, err } = this.state
@@ -295,16 +291,46 @@
       }
 
       if (col.marks) {
-        style = style || {}
-        content = this.getMark(record, col.marks, style, content)
+        let mark = getMark(col.marks, record, style)
+
+        style = mark.style
+
+        if (mark.icon) {
+          if (mark.position === 'front') {
+            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
+          } else {
+            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
+          }
+        }
       }
 
       if (col.editable === 'true') {
         if (editing) {
-          return (<td className="editing_table_cell">
-            <Input id={col.uuid + record.$Index} defaultValue={value} onChange={(e) => this.onChange(e.target.value)} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
-            {err ? <Tooltip title={err}><Icon type="exclamation-circle" /></Tooltip> : null}
-          </td>)
+          if (!col.editType || col.editType === 'text') {
+            return (<td className="editing_table_cell">
+              <Input id={col.uuid + record.$$uuid} defaultValue={value} onChange={(e) => this.onChange(e.target.value)} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
+              {err ? <Tooltip title={err}><ExclamationCircleOutlined /></Tooltip> : null}
+            </td>)
+          } else if (col.editType === 'switch') {
+            let _value = record[col.field] !== undefined ? record[col.field] : ''
+            return (<td className="editing_table_cell">
+              <CusSwitch config={col} defaultValue={_value} onChange={this.onSwitchChange} onBlur={this.switchBlur}/>
+            </td>)
+          } else {
+            let _value = record[col.field] !== undefined ? record[col.field] : ''
+            return (<td className="editing_table_cell">
+              <Select
+                showSearch
+                defaultValue={_value}
+                id={col.uuid + record.$$uuid}
+                onBlur={() => this.setState({editing: false})}
+                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
+                onSelect={this.onSelectChange}
+              >
+                {col.options.map((item, i) => (<Select.Option key={item.key} disabled={item.$disabled} value={item.value}>{item.label}</Select.Option>))}
+              </Select>
+            </td>)
+          }
         } else {
           return (<td className={className + ' pointer'} style={style}><div className="mk-mask" onClick={this.focus}></div>{content}</td>)
         }
@@ -327,6 +353,8 @@
         if (col.format === 'percent') {
           content = content * 100
           decimal = decimal > 2 ? decimal - 2 : 0
+        } else if (col.format === 'abs') {
+          content = Math.abs(content)
         }
   
         content = content.toFixed(decimal)
@@ -339,15 +367,24 @@
       }
 
       if (col.marks) {
-        style = style || {}
-        content = this.getMark(record, col.marks, style, content)
+        let mark = getMark(col.marks, record, style)
+
+        style = mark.style
+
+        if (mark.icon) {
+          if (mark.position === 'front') {
+            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
+          } else {
+            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
+          }
+        }
       }
 
       if (col.editable === 'true') {
         if (editing) {
           return (<td className="editing_table_cell">
-            <InputNumber id={col.uuid + record.$Index} defaultValue={value} onChange={(val) => this.onChange(val)} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
-            {err ? <Tooltip title={err}><Icon type="exclamation-circle" /></Tooltip> : null}
+            <InputNumber id={col.uuid + record.$$uuid} defaultValue={value} onChange={(val) => this.onChange(val)} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
+            {err ? <Tooltip title={err}><ExclamationCircleOutlined /></Tooltip> : null}
           </td>)
         } else {
           return (<td className={className + ' pointer'} style={style}><div className="mk-mask" onClick={this.focus}></div>{content}</td>)
@@ -398,8 +435,17 @@
       }
 
       if (col.marks) {
-        style = style || {}
-        content = this.getMark(record, col.marks, style, content)
+        let mark = getMark(col.marks, record, style)
+
+        style = mark.style
+
+        if (mark.icon) {
+          if (mark.position === 'front') {
+            content = <span><MkIcon style={{color: mark.color}} type={mark.icon} /> {content}</span>
+          } else {
+            content = <span>{content} <MkIcon style={{color: mark.color}} type={mark.icon} /></span>
+          }
+        }
       }
 
       children = content
@@ -424,7 +470,8 @@
       )
     }
 
-    return (<td className={className} style={style}>{children}</td>)
+    return (<td className={className} style={style}>{col.addable ? <PlusCircleOutlined onClick={() => MKEmitter.emit('addRecord', col.tableId, {...record})} className="mk-editable mk-plus"/> : null}{children}{col.delable ? <DeleteOutlined onClick={() => MKEmitter.emit('delRecord', col.tableId, {...record})} className="mk-editable mk-del"/> : null}</td>)
+    // return (<td className={className} style={style}>{children}</td>)
   }
 }
 
@@ -465,31 +512,21 @@
     let orderfields = {}
     let initEditLine = null
     let edColumns = []
-    let tableId = (() => {
-      let uuid = []
-      let _options = 'abcdefghigklmnopqrstuv'
-      for (let i = 0; i < 19; i++) {
-        uuid.push(_options.substr(Math.floor(Math.random() * 0x20), 1))
-      }
-      return uuid.join('')
-    }) ()
 
     let _columns = []
+    let deForms = []
     columns.forEach(item => {
-      if (item.Hide === 'true') return
-      if (item.type === 'index') {
-        item.field = '$Index'
-        item.type = 'text'
-      }
-
-      item.tableId = tableId
-
       if (!initEditLine && item.editable === 'true') {
         initEditLine = item
       }
 
-      if (item.marks && item.marks.length === 0) {
-        item.marks = ''
+      if (item.type === 'text' && item.editable === 'true' && item.editType === 'select' && item.resourceType === '1') {
+        let _option = Utils.getSelectQueryOptions(item)
+
+        item.base_sql = window.btoa(window.encodeURIComponent(_option.sql))
+        item.arr_field = _option.field
+
+        deForms.push(item)
       }
 
       if (item.field) {
@@ -514,14 +551,14 @@
         _copy.sorter = false
 
         if (item.editable === 'true') {
-          _copy.title = <span>{item.label}<Icon className="system-color" style={{position: 'absolute', bottom: 0, right: 0}} type="edit" /></span>
+          _copy.title = <span>{item.label}<EditOutlined className="system-color" style={{position: 'absolute', bottom: '2px', right: '5px'}}/></span>
         }
         edColumns.push(_copy)
       }
       _columns.push(_item)
     })
 
-    if (setting.delable !== 'false') {
+    if (setting.delable !== 'false' && setting.operType !== 'buoyMode') {
       edColumns.push({
         align: 'center',
         dataIndex: 'mkoperation',
@@ -530,13 +567,13 @@
         width: 100,
         onCell: record => ({
           record,
-          col: {type: 'operation', tableId: tableId},
+          col: {type: 'operation', tableId: setting.tableId},
         })
       })
     }
 
     if (setting.borderColor) { // 杈规棰滆壊
-      let style = `#${tableId} table, #${tableId} tr, #${tableId} th, #${tableId} td {border-color: ${setting.borderColor}}`
+      let style = `#${setting.tableId} table, #${setting.tableId} tr, #${setting.tableId} th, #${setting.tableId} td {border-color: ${setting.borderColor}}`
       let ele = document.createElement('style')
       ele.innerHTML = style
       document.getElementsByTagName('head')[0].appendChild(ele)
@@ -556,10 +593,14 @@
       data,
       columns: _columns,
       edColumns,
-      tableId,
+      tableId: setting.tableId,
       orderfields,
       initEditLine,
       editable: setting.editable
+    }, () => {
+      if (deForms.length > 0) {
+        this.improveActionForm(deForms)
+      }
     })
   }
 
@@ -568,24 +609,11 @@
   }
 
   componentDidMount () {
-    const { fields, columns } = this.props
+    const { fields, setting } = this.props
     const { data, editable } = this.state
 
-    let _fields = []
-
-    let fieldType = {}
-    fields.forEach(item => {
-      fieldType[item.field] = item.datatype
-    })
-
-    columns.forEach(col => {
-      if (!col.field || col.type === 'index') return
-
-      _fields.push({...col, datatype: fieldType[col.field] || 'Nvarchar(50)'})
-    })
-
     this.setState({
-      fields: _fields,
+      fields: fields.filter(item => item.field !== setting.primaryKey),
     })
 
     if (editable === 'true' && data && data.length > 0) {
@@ -595,6 +623,7 @@
     }
 
     MKEmitter.addListener('nextLine', this.nextLine)
+    MKEmitter.addListener('addRecord', this.addLine)
     MKEmitter.addListener('delRecord', this.delRecord)
     MKEmitter.addListener('resetTable', this.resetTable)
     MKEmitter.addListener('changeRecord', this.changeRecord)
@@ -608,6 +637,7 @@
       return
     }
     MKEmitter.removeListener('nextLine', this.nextLine)
+    MKEmitter.removeListener('addRecord', this.addLine)
     MKEmitter.removeListener('delRecord', this.delRecord)
     MKEmitter.removeListener('resetTable', this.resetTable)
     MKEmitter.removeListener('changeRecord', this.changeRecord)
@@ -616,12 +646,168 @@
   UNSAFE_componentWillReceiveProps(nextProps) {
     if (!is(fromJS(this.props.data), fromJS(nextProps.data))) {
       this.setState({data: nextProps.data || []})
-      if (this.state.editable === 'true') {
+      if (this.state.editable === 'true' && !this.state.pickup) {
         setTimeout(() => {
           this.pickupChange()
         }, 200)
       }
     }
+  }
+
+  improveActionForm = (deForms) => {
+    const { BID, setting } = this.props
+
+    let deffers = []
+    let mainItems = []  // 浜戠鎴栧崟鐐规暟鎹�
+    let localItems = [] // 鏈湴鏁版嵁
+    let cache = setting.cache !== 'false'
+
+    deForms.forEach(item => {
+      if (item.database === 'sso') {
+        mainItems.push(`select '${item.uuid}' as obj_name,'${item.arr_field}' as arr_field,'${item.base_sql}' as LText`)
+      } else {
+        localItems.push(`select '${item.uuid}' as obj_name,'${item.arr_field}' as arr_field,'${item.base_sql}' as LText`)
+      }
+    })
+    
+    if (options.sysType !== 'local') {
+      localItems = [...localItems, ...mainItems]
+      mainItems = []
+    }
+
+    // 鏈湴璇锋眰
+    let param = {
+      func: 'sPC_Get_SelectedList',
+      LText: localItems.join(' union all '),
+      obj_name: '',
+      arr_field: '',
+      table_type: 'Y',
+      BID: BID || ''
+    }
+
+    if (param.LText) {
+      param.LText = Utils.formatOptions(param.LText)
+      param.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
+      param.secretkey = Utils.encrypt(param.LText, param.timestamp)
+
+      deffers.push(
+        new Promise(resolve => {
+          Api.getSystemCacheConfig(param, cache).then(res => {
+            if (!res.status) {
+              notification.warning({
+                top: 92,
+                message: res.message,
+                duration: 5
+              })
+            }
+            resolve(res)
+          })
+        })
+      )
+    }
+
+    // 绯荤粺璇锋眰
+    let mainparam = {
+      func: 'sPC_Get_SelectedList',
+      LText: mainItems.join(' union all '),
+      obj_name: '',
+      arr_field: '',
+      table_type: 'Y',
+      BID: BID || ''
+    }
+
+    if (mainparam.LText) {
+      mainparam.LText = Utils.formatOptions(mainparam.LText)
+      mainparam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
+      mainparam.secretkey = Utils.encrypt(mainparam.LText, mainparam.timestamp)
+
+      if (window.GLOB.mainSystemApi) {
+        mainparam.rduri = window.GLOB.mainSystemApi
+      }
+
+      deffers.push(
+        new Promise(resolve => {
+          Api.getSystemCacheConfig(mainparam, cache).then(res => {
+            if (!res.status) {
+              notification.warning({
+                top: 92,
+                message: res.message,
+                duration: 5
+              })
+            }
+            resolve(res)
+          })
+        })
+      )
+    }
+
+    Promise.all(deffers).then(response => {
+      let result = {...response[0], ...(response[1] || {})}
+
+      delete result.ErrCode
+      delete result.ErrMesg
+      delete result.message
+      delete result.status
+
+      this.resetFormList(result)
+    })
+  }
+
+  resetFormList = (result) => {
+    const { columns } = this.props
+    const { edColumns } = this.state
+
+    let _edColumns = []
+
+    let reCols = {}
+    columns.forEach(item => {
+      if (item.resourceType === '1' && result[item.uuid] && result[item.uuid].length > 0) {
+        let options = []
+        let _map = new Map()
+        result[item.uuid].forEach(cell => {
+          let _cell = {key: Utils.getuuid()}
+
+          _cell.value = cell[item.valueField]
+          _cell.label = cell[item.valueText]
+
+          if (!_cell.label && _cell.label !== 0) return
+
+          if (_map.has(_cell.value)) return
+          _map.set(_cell.value, true)
+
+          if (item.linkSubField) {
+            item.linkSubField.forEach(m => {
+              _cell[m] = cell[m] === undefined ? '' : cell[m]
+            })
+          }
+
+          if (item.disableField && cell[item.disableField] && /^true$/ig.test(cell[item.disableField])) {
+            _cell.$disabled = true
+          }
+  
+          options.push(_cell)
+        })
+
+        item.options = options
+
+        reCols[item.uuid] = item
+      }
+    })
+
+    _edColumns = edColumns.map(item => {
+      if (reCols[item.dataIndex]) {
+        item.onCell = record => ({
+          record,
+          col: reCols[item.dataIndex]
+        })
+      }
+
+      return item
+    })
+
+    this.setState({edColumns: []}, () => {
+      this.setState({edColumns: _edColumns})
+    })
   }
   
   nextLine = (col, index) => {
@@ -636,7 +822,7 @@
       MKEmitter.emit('tdFocus', initEditLine.uuid + (index + 1))
     } else if (col.footEnter === 'add' && setting.addable === 'true') {
       setTimeout(() => {
-        this.plusLine(initEditLine)
+        this.plusLine()
       }, 10)
     } else if (col.footEnter === 'sub') {
       setTimeout(() => {
@@ -645,18 +831,16 @@
     }
   }
 
-  plusLine = (initEditLine) => {
-    const { edData, fields } = this.state
+  plusLine = () => {
+    const { edData, fields, initEditLine } = this.state
 
     let item = {...edData[edData.length - 1]}
 
-    item.key = item.key + 1
-    item.$$uuid = '$new'
-    item.$Index = item.key + 1 + ''
+    item.$$uuid = Utils.getguid()
+    item.$type = 'add'
+    item.$Index = ''
 
     fields.forEach(col => {
-      item[col.field] = item[col.field] !== undefined ? item[col.field] : ''
-
       if (col.initval !== '$copy') {
         item[col.field] = col.initval
       }
@@ -666,10 +850,13 @@
           item[col.field] = 0
         }
       }
+      if (item[col.field] === undefined) {
+        item[col.field] = ''
+      }
     })
 
     this.setState({edData: [...edData, item]}, () => {
-      MKEmitter.emit('tdFocus', initEditLine.uuid + item.$Index)
+      MKEmitter.emit('tdFocus', initEditLine.uuid + item.$$uuid)
     })
   }
 
@@ -680,17 +867,14 @@
 
     let _data = []
 
-    if (record.$$uuid === '$new') {
-      _data = edData.filter(item => item.$Index !== record.$Index)
-      _data = _data.map((item, index) => {
-        item.key = index
-        item.$Index = 1 + index + ''
-        return item
-      })
+    if (record.$type === 'add') {
+      _data = edData.filter(item => item.$$uuid !== record.$$uuid)
     } else {
       _data = edData.map(item => {
-        if (item.$Index === record.$Index) {
+        if (item.$$uuid === record.$$uuid) {
           record.$deleted = true
+          record.$origin = false
+          record.$type = 'del'
           return record
         } else {
           return item
@@ -707,7 +891,8 @@
     if (id !== tableId) return
 
     let _data = this.state.edData.map(item => {
-      if (item.$Index === record.$Index) {
+      if (item.$$uuid === record.$$uuid) {
+        record.$origin = false
         return record
       } else {
         return item
@@ -717,38 +902,73 @@
     this.setState({edData: _data})
   }
 
-  addLine = () => {
+  addLine = (id, record) => {
     const { BID } = this.props
-    const { edData, fields } = this.state
+    const { edData, fields, tableId } = this.state
 
-    let item = {}
-    if (edData.length > 0) {
-      item = {...edData[edData.length - 1]}
-      item.key = item.key + 1
-      item.$$uuid = '$new'
-      item.$Index = item.key + 1 + ''
-    } else {
-      item.key = 0
-      item.$$uuid = '$new'
-      item.$Index = item.key + 1 + ''
+    if (id) {
+      if (id !== tableId) return
+      let _edData = fromJS(edData).toJS()
+      let index = _edData.findIndex(item => record.$$uuid === item.$$uuid)
+
+      let item = {}
+
+      item.$$uuid = Utils.getguid()
+      item.$type = 'add'
+      item.$Index = ''
       item.$$BID = BID || ''
-    }
-
-    fields.forEach(col => {
-      item[col.field] = item[col.field] !== undefined ? item[col.field] : ''
-
-      if (col.initval !== '$copy') {
-        item[col.field] = col.initval
-      }
-      if (col.type === 'number') {
-        item[col.field] = +item[col.field]
-        if (isNaN(item[col.field])) {
-          item[col.field] = 0
+  
+      fields.forEach(col => {
+        if (col.initval !== '$copy') {
+          item[col.field] = col.initval
         }
-      }
-    })
+        if (col.type === 'number') {
+          item[col.field] = +item[col.field]
+          if (isNaN(item[col.field])) {
+            item[col.field] = 0
+          }
+        }
+  
+        if (item[col.field] === undefined) {
+          item[col.field] = ''
+        }
+      })
 
-    this.setState({edData: [...edData, item]})
+      _edData.splice(index, 0, item)
+
+      this.setState({edData: _edData})
+    } else {
+      let item = {}
+      if (edData.length > 0) {
+        item = {...edData[edData.length - 1]}
+        item.$$uuid = Utils.getguid()
+        item.$type = 'add'
+        item.$Index = ''
+      } else {
+        item.$$uuid = Utils.getguid()
+        item.$type = 'add'
+        item.$Index = ''
+        item.$$BID = BID || ''
+      }
+  
+      fields.forEach(col => {
+        if (col.initval !== '$copy') {
+          item[col.field] = col.initval
+        }
+        if (col.type === 'number') {
+          item[col.field] = +item[col.field]
+          if (isNaN(item[col.field])) {
+            item[col.field] = 0
+          }
+        }
+  
+        if (item[col.field] === undefined) {
+          item[col.field] = ''
+        }
+      })
+  
+      this.setState({edData: [...edData, item]})
+    }
   }
 
   checkData = () => {
@@ -763,6 +983,7 @@
       return
     }
     let err = ''
+    let Index = 1
     let data = fromJS(edData).toJS().map(item => {
       let line = []
       fields.forEach(col => {
@@ -808,7 +1029,10 @@
       })
 
       if (line.length > 0) {
-        err += `绗�${item.$Index}琛岋細` + line.join('锛�') + '锛�'
+        err += `绗�${Index}琛岋細` + line.join('锛�') + '锛�'
+      }
+      if (!item.$deleted) {
+        Index++
       }
 
       return item
@@ -826,8 +1050,21 @@
   }
 
   submit = (data) => {
-    const { submit, BID } = this.props
+    const { submit, BID, setting } = this.props
     const { fields } = this.state
+
+    if (setting.commit === 'change') {
+      data = data.filter(item => !item.$origin)
+    }
+
+    if (data.length === 0) {
+      notification.warning({
+        top: 92,
+        message: '鏁版嵁鏈慨鏀癸紝涓嶅彲鎻愪氦锛�',
+        duration: 5
+      })
+      return
+    }
 
     let result = getEditTableSql(submit, data, fields)
 
@@ -1084,10 +1321,11 @@
     let height = setting.height || false
 
     return (
-      <div className={`edit-custom-table ${pickup ? 'editable' : ''} ${setting.tableHeader || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}>
-        <Switch title="缂栬緫" className="main-pickup" checkedChildren="寮�" unCheckedChildren="鍏�" checked={pickup} onChange={this.pickupChange} />
+      <div className={`edit-custom-table ${pickup ? 'editable' : ''} ${setting.tableHeader || ''} ${setting.operType || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}>
+        <Switch title="缂栬緫" className="main-pickup" checkedChildren="寮�" unCheckedChildren="鍏�" disabled={loading || this.props.loading} checked={pickup} onChange={this.pickupChange} />
         {pickup ? <Button style={submit.style} onClick={() => setTimeout(() => {this.checkData()}, 10)} loading={loading} className="submit-table" type="link">鎻愪氦</Button> : null}
         <Table
+          rowKey="$$uuid"
           components={components}
           style={setting.style}
           size={setting.size || 'middle'}
@@ -1106,7 +1344,7 @@
           pagination={_pagination}
         />
         {_footer ? <div className={'normal-table-footer ' + (_pagination ? 'pagination' : '')}>{_footer}</div> : null}
-        {pickup && setting.addable === 'true' ? <Button onClick={this.addLine} style={{display: 'block', width: '100%', color: '#26C281', border: '1px solid #dddddd', borderRadius: 0}} icon="plus" type="link"></Button> : null}
+        {pickup && setting.addable === 'true' ? <Button className="mk-add-line" onClick={() => this.addLine()} disabled={this.props.loading} type="link"><PlusOutlined /></Button> : null}
       </div>
     )
   }

--
Gitblit v1.8.0