king
2022-03-04 67a307d8f0fa25fbc56ee9f84c313d521280925f
src/tabviews/custom/components/table/edit-table/normalTable/index.jsx
@@ -138,7 +138,9 @@
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
@@ -213,7 +215,7 @@
    this.setState({value: val, err})
  }
  onSwitchChange = (val) => {
  onSwitchChange = (val, label) => {
    const { col, record } = this.props
    this.setState({editing: false})
@@ -221,12 +223,22 @@
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
    MKEmitter.emit('changeRecord', col.tableId, {...record, [col.field]: val})
    let values = {}
    if (col.editField) {
      values[col.field] = label
      values[col.editField] = val
    } else {
      values[col.field] = val
    }
    MKEmitter.emit('changeRecord', col.tableId, {...record, ...values})
  }
  onSelectChange = (val, option) => {
@@ -255,7 +267,9 @@
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
@@ -314,16 +328,28 @@
              {err ? <Tooltip title={err}><ExclamationCircleOutlined /></Tooltip> : null}
            </td>)
          } else if (col.editType === 'switch') {
            let _value = record[col.field] !== undefined ? record[col.field] : ''
            let _value = ''
            if (col.editField) {
              _value = record[col.editField] !== undefined ? record[col.editField] : ''
            } else {
              _value = record[col.field] !== undefined ? record[col.field] : ''
            }
            return (<td className="editing_table_cell">
              <CusSwitch config={col} defaultValue={_value} autoFocus={true} onChange={this.onSwitchChange} onBlur={this.switchBlur}/>
            </td>)
          } else {
            let _value = record[col.field] !== undefined ? record[col.field] : ''
            let _value = ''
            if (col.editField) {
              _value = record[col.editField] !== undefined ? record[col.editField] : ''
            } else {
              _value = record[col.field] !== undefined ? record[col.field] : ''
            }
            return (<td className="editing_table_cell">
              <Select
                showSearch
                defaultValue={_value}
                dropdownClassName="edit-table-dropdown"
                dropdownMatchSelectWidth={col.dropdown === 'fixed'}
                id={col.uuid + record.$$uuid}
                onBlur={() => this.setState({editing: false})}
                filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
@@ -522,7 +548,9 @@
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
@@ -581,18 +609,28 @@
    MKEmitter.emit('changeRecord', col.tableId, {...record, [col.field]: val})
  }
  onSwitchChange = (val) => {
  onSwitchChange = (val, label) => {
    const { col, record } = this.props
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
    MKEmitter.emit('changeRecord', col.tableId, {...record, [col.field]: val})
    let values = {}
    if (col.editField) {
      values[col.field] = label
      values[col.editField] = val
    } else {
      values[col.field] = val
    }
    MKEmitter.emit('changeRecord', col.tableId, {...record, ...values})
  }
  onSelectChange = (val, option) => {
@@ -619,7 +657,9 @@
    setTimeout(() => {
      if (col.enter === '$next') {
        MKEmitter.emit('nextLine', col, record.$$uuid)
      } else {
      } else if (col.enter === '$sub') {
        MKEmitter.emit('subLine', col, record)
      } else if (col.enter !== '$noAct') {
        MKEmitter.emit('tdFocus', col.enter + record.$$uuid)
      }
    }, 50)
@@ -634,7 +674,13 @@
    let children = null
    if (col.type === 'text') {
      if (col.editable === 'true') {
        let _value = record[col.field] !== undefined ? record[col.field] : ''
        let _value = ''
        if (col.editField) {
          _value = record[col.editField] !== undefined ? record[col.editField] : ''
        } else {
          _value = record[col.field] !== undefined ? record[col.field] : ''
        }
        if (!col.editType || col.editType === 'text') {
          children = (<>
            <Input id={col.uuid + record.$$uuid} defaultValue={_value} onChange={(e) => this.onChange(e.target.value)} onPressEnter={this.enterPress} onBlur={this.onBlur}/>
@@ -648,9 +694,10 @@
          children = (<>
            <Select
              showSearch
              dropdownClassName="edit-table-dropdown"
              dropdownMatchSelectWidth={col.dropdown === 'fixed'}
              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}
            >
@@ -830,6 +877,8 @@
    total: PropTypes.any,            // 总数
    loading: PropTypes.bool,         // 表格加载中
    refreshdata: PropTypes.func,     // 表格中排序列、页码的变化时刷新
    changeLock: PropTypes.func,
    chgSelectData: PropTypes.func,
  }
  state = {
@@ -846,12 +895,12 @@
    pickup: false,        // 收起未选择项
    orderfields: {},      // 排序id与field转换
    loading: false,
    editable: 'false',
    editable: false,
    pageOptions: []
  }
  UNSAFE_componentWillMount () {
    const { setting, fields, columns, data } = this.props
    const { setting, fields, columns } = this.props
    let orderfields = {}
    let initEditLine = null
    let edColumns = []
@@ -933,13 +982,12 @@
    this.setState({
      pageSize: setting.pageSize || 10,
      pageOptions,
      data,
      columns: _columns,
      edColumns,
      tableId: setting.tableId,
      orderfields,
      initEditLine,
      editable: setting.editable
      editable: setting.editable === 'true'
    }, () => {
      if (deForms.length > 0) {
        this.improveActionForm(deForms)
@@ -953,23 +1001,17 @@
  componentDidMount () {
    const { fields, setting } = this.props
    const { data, editable } = this.state
    this.setState({
      fields: fields.filter(item => item.field !== setting.primaryKey),
    })
    if (editable === 'true' && data && data.length > 0) {
      this.setState({editable: 'false'})
      setTimeout(() => {
        this.pickupChange()
      }, 200)
    }
    MKEmitter.addListener('subLine', this.subLine)
    MKEmitter.addListener('nextLine', this.nextLine)
    MKEmitter.addListener('addRecord', this.addLine)
    MKEmitter.addListener('delRecord', this.delRecord)
    MKEmitter.addListener('resetTable', this.resetTable)
    MKEmitter.addListener('transferData', this.transferData)
    MKEmitter.addListener('changeRecord', this.changeRecord)
  }
@@ -980,22 +1022,44 @@
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('subLine', this.subLine)
    MKEmitter.removeListener('nextLine', this.nextLine)
    MKEmitter.removeListener('addRecord', this.addLine)
    MKEmitter.removeListener('delRecord', this.delRecord)
    MKEmitter.removeListener('resetTable', this.resetTable)
    MKEmitter.removeListener('transferData', this.transferData)
    MKEmitter.removeListener('changeRecord', this.changeRecord)
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (!is(fromJS(this.props.data), fromJS(nextProps.data))) {
      this.setState({data: nextProps.data || []})
      if (this.state.editable === 'true' && !this.state.pickup) {
        this.setState({editable: 'false'})
  transferData = (menuid, data, type) => {
    if (menuid !== this.props.MenuID) return
    if (type !== 'line') {
      this.setState({data: data || []})
      if (this.state.editable && !this.state.pickup) {
        this.setState({editable: false})
        setTimeout(() => {
          this.pickupChange()
        }, 200)
      }
    } else if (type === 'line' && data.$$uuid) {
      let _data = this.state.data.map(item => {
        if (item.$$uuid === data.$$uuid) {
          return data
        } else {
          return item
        }
      })
      let _edData = this.state.edData.map(item => {
        if (item.$$uuid === data.$$uuid) {
          return data
        } else {
          return item
        }
      })
      this.setState({edData: _edData, data: _data})
    }
  }
@@ -1172,15 +1236,82 @@
    if (next && initEditLine) {
      MKEmitter.emit('tdFocus', initEditLine.uuid + next.$$uuid)
    } else if (col.footEnter === 'add' && setting.addable === 'true') {
    } else if (setting.addable === 'true') {
      setTimeout(() => {
        this.plusLine()
      }, 10)
    } else if (col.footEnter === 'sub') {
      setTimeout(() => {
        this.checkData()
      }, 10)
    }
  }
  subLine = (col, record) => {
    const { tableId, fields, edData } = this.state
    if (col && col.tableId !== tableId) return
    if (edData.filter(item => !item.$origin).length > 1) {
      setTimeout(() => {
        this.submit(edData)
      }, 10)
      return
    }
    setTimeout(() => {
      let item = fromJS(record).toJS()
      let line = []
      fields.forEach(col => {
        if (col.editable !== 'true' || item.$deleted) {
          if (col.type === 'number') {
            item[col.field] = +item[col.field]
            if (isNaN(item[col.field])) {
              item[col.field] = 0
            }
          } else {
            item[col.field] = item[col.field] !== undefined ? (item[col.field] + '') : ''
          }
          return
        }
        if (col.type === 'text') {
          let val = item[col.field] !== undefined ? (item[col.field] + '') : ''
          if (col.required === 'true' && !val) {
            line.push(`${col.label}不可为空`)
          }
          item[col.field] = val
        } else if (col.type === 'number') {
          let val = item[col.field]
          if (!val && val !== 0) {
            line.push(`${col.label}不可为空`)
            return
          }
          val = +val
          if (isNaN(val)) {
            line.push(`${col.label}数据格式错误`)
            return
          }
          val = +val.toFixed(col.decimal || 0)
          if (typeof(col.max) === 'number' && val > col.max) {
            line.push(`${col.label}不可大于${col.max}`)
          } else if (typeof(col.min) === 'number' && val < col.min) {
            line.push(`${col.label}不可小于${col.min}`)
          }
          item[col.field] = val
        }
      })
      let err = line.join(',')
      if (err) {
        notification.warning({
          top: 92,
          message: err,
          duration: 5
        })
      } else {
        this.submit([item], 'simple')
      }
    }, 10)
  }
  plusLine = () => {
@@ -1213,6 +1344,7 @@
  }
  delRecord = (id, record) => {
    const { setting } = this.props
    const { tableId, edData } = this.state
    if (id !== tableId) return
@@ -1232,6 +1364,10 @@
          return item
        }
      })
      if (setting.commit === 'simple' && record.$deleted) {
        this.subLine(null, record)
      }
    }
    this.setState({edData: _data})
@@ -1401,11 +1537,11 @@
    }
  }
  submit = (data) => {
  submit = (data, type) => {
    const { submit, BID, setting } = this.props
    const { fields } = this.state
    if (setting.commit === 'change') {
    if (type !== 'simple' && (setting.commit === 'change' || setting.commit === 'simple')) {
      data = data.filter(item => !item.$origin)
    }
@@ -1457,7 +1593,12 @@
      Api.genericInterface(param).then((res) => {
        if (res.status) {
          this.execSuccess(res)
          if (type === 'simple') {
            this.updataLine(data[0])
            this.execSuccess(res, type)
          } else {
            this.execSuccess(res)
          }
        } else {
          this.execError(res)
        }
@@ -1469,7 +1610,12 @@
      Api.genericInterface(param).then((res) => {
        if (res.status) {
          this.execSuccess(res)
          if (type === 'simple') {
            this.updataLine(data[0])
            this.execSuccess(res, type)
          } else {
            this.execSuccess(res)
          }
        } else {
          this.execError(res)
        }
@@ -1479,7 +1625,26 @@
    }
  }
  execSuccess = (res) => {
  updataLine = (item) => {
    if (item.$type === 'del') {
      let _data = this.state.edData.filter(m => m.$$uuid !== item.$$uuid)
      this.setState({edData: _data})
    } else {
      let _data = this.state.edData.map(m => {
        if (m.$$uuid === item.$$uuid) {
          item.$origin = true
          return item
        }
        return m
      })
      this.setState({edData: _data})
    }
    MKEmitter.emit('reloadData', this.props.MenuID, item.$$uuid, item)
  }
  execSuccess = (res, type) => {
    const { submit } = this.props
    if (res && res.ErrCode === 'S') { // 执行成功
@@ -1499,6 +1664,8 @@
    this.setState({
      loading: false
    })
    if (type === 'simple') return
    if (submit.closetab === 'true') {
      MKEmitter.emit('popclose')
@@ -1544,8 +1711,14 @@
  }
  repick = () => {
    const { setting } = this.props
    const { data } = this.state
    if (setting.submittal === 'true') {
      this.setState({editable: true})
    }
    this.props.changeLock(false)
    this.setState({
      data: [],
      edData: [],
@@ -1692,6 +1865,7 @@
      })
    } else {
      pickup && MKEmitter.emit('resetSelectLine', MenuID, '', '')
      pickup && this.props.chgSelectData([])
      this.setState({
        data: [],
        edData: [],
@@ -1705,6 +1879,7 @@
        })
      })
    }
    this.props.changeLock(pickup)
  }
  render() {