king
2023-08-27 da64ab0923bf8817fc8599a6e37b953ce38f64c8
src/templates/zshare/modalform/modaleditable/index.jsx
@@ -1,10 +1,9 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Table, Input, Popconfirm, Form, Radio, message } from 'antd'
import { Table, Input, Popconfirm, Form, message } from 'antd'
import { ArrowUpOutlined, ArrowDownOutlined, DeleteOutlined, PlusOutlined, SwapOutlined } from '@ant-design/icons'
import { formRule } from '@/utils/option.js'
import Utils from '@/utils/utils.js'
import './index.scss'
@@ -35,16 +34,8 @@
  }
  save = e => {
    const { record, handleSave, datatype } = this.props
    const { record, handleSave } = this.props
    this.form.validateFields((error, values) => {
      if (datatype === 'number') {
        Object.keys(values).forEach(key => {
          values[key] = parseFloat(values[key])
          if (isNaN(values[key])) {
            values[key] = 0
          }
        })
      }
      handleSave({ ...record, ...values })
      if (error && error[e.currentTarget.id]) {
        return
@@ -55,16 +46,8 @@
  renderCell = form => {
    this.form = form
    const { children, dataIndex, record, datatype } = this.props
    const { children, dataIndex, record } = this.props
    const { editing } = this.state
    let rules = []
    if (datatype === 'number') {
      rules.push({
        pattern: /^(-?\d+)(\.\d+)?$/,
        message: formRule.input.numbermsg
      })
    }
    return editing ? (
      <Form.Item style={{ margin: '0 -5px 0 -5px' }}>
@@ -73,8 +56,7 @@
            {
              required: dataIndex === 'Text',
              message: '不可为空.',
            },
            ...rules
            }
          ],
          initialValue: record[dataIndex]
        })(<Input ref={node => (this.input = node)} autoComplete="off" onPressEnter={this.save} onBlur={this.save} />)}
@@ -137,61 +119,6 @@
      columns: columns,
      dataSource: data,
      count: data.length
    })
  }
  getColumnSearchProps = column => ({
    filterDropdown: () => (
      <div style={{ padding: 8 }}>
        <Radio.Group onChange={(e) => this.changeDatatype(column, e)} value={column.datatype}>
          <Radio style={{display: 'block', height: '30px', lineHeight: '30px'}} value="string">
            字符串
          </Radio>
          <Radio style={{display: 'block', height: '30px', lineHeight: '30px'}} value="number">
            数字
          </Radio>
        </Radio.Group>
      </div>
    ),
    filterIcon: () => (
      <SwapOutlined style={{ color: column.datatype === 'number' ? '#1890ff' : ''}} />
    )
  })
  changeDatatype = (column, e) => {
    const { columns, dataSource } = this.state
    let value = e.target.value
    let _data = dataSource.map(item => {
      let val = item[column.dataIndex]
      if (value === 'number') {
        val = parseFloat(val)
        if (isNaN(val)) {
          val = 0
        }
      } else {
        val = '' + val
      }
      item[column.dataIndex] = val
      return item
    })
    this.setState({
      dataSource: _data,
      columns: columns.map(col => {
        if (col.dataIndex === column.dataIndex) {
          col.datatype = value
        }
        if (col.dataIndex !== 'operation') {
          col = {...col, ...this.getColumnSearchProps(col)}
        }
        return col
      })
    }, () => {
      this.props.onChange(_data)
    })
  }
@@ -301,7 +228,6 @@
    let _dataSource = fromJS(dataSource).toJS()
    let fields = []
    let dataItem = ''
    let subFields = linkSubFields.filter(m => m !== 'Value' && m !== 'Text')
    if (subFields.length > 0) {
@@ -313,32 +239,28 @@
        return data
      })
      dataItem = _dataSource ? _dataSource[0] : ''
      fields = subFields.map(field => {
        return {
          title: transfield[field] || field,
          $title: transfield[field] || field,
          dataIndex: field,
          editable: true,
          datatype: dataItem && typeof(dataItem[field]) === 'number' ? 'number' : 'string'
        }
      })
    } else {
      dataItem = _dataSource ? _dataSource[0] : ''
    }
    let columns = [
      {
        title: 'Value',
        $title: 'Value',
        dataIndex: 'Value',
        editable: true,
        datatype: dataItem && typeof(dataItem.Value) === 'number' ? 'number' : 'string'
        editable: true
      },
      {
        title: 'Text',
        $title: 'Text',
        dataIndex: 'Text',
        editable: true,
        datatype: dataItem && typeof(dataItem.Text) === 'number' ? 'number' : 'string'
        editable: true
      },
      ...fields,
      {
@@ -367,16 +289,18 @@
    if (type === 'link') {
      columns.unshift({
        title: 'ParentID',
        $title: 'ParentID',
        dataIndex: 'ParentID',
        editable: true,
        datatype: dataItem && typeof(dataItem.ParentID) === 'number' ? 'number' : 'string'
        editable: true
      })
    }
    return {
      columns: columns.map(col => {
        if (col.dataIndex !== 'operation') {
          col = {...col, ...this.getColumnSearchProps(col)}
          col.title = <div>
            {col.$title}
          </div>
        }
        return col
      }),
@@ -456,8 +380,7 @@
          editable: col.editable,
          dataIndex: col.dataIndex,
          title: col.title,
          datatype: col.datatype,
          handleSave: this.handleSave,
          handleSave: this.handleSave
        })
      }
    })