| | |
| | | import React, {Component} from 'react' |
| | | import { is, fromJS } from 'immutable' |
| | | import { Table, Input, Button, Popconfirm, Form, Icon } from 'antd' |
| | | import { Table, Input, Button, Popconfirm, Form, Icon, Radio } from 'antd' |
| | | import { formRule } from '@/utils/option.js' |
| | | import Utils from '@/utils/utils.js' |
| | | import './index.scss' |
| | | |
| | |
| | | } |
| | | |
| | | save = e => { |
| | | const { record, handleSave } = this.props |
| | | const { record, handleSave, datatype } = this.props |
| | | this.form.validateFields((error, values) => { |
| | | if (datatype === 'number') { |
| | | Object.keys(values).forEach(key => { |
| | | values[key] = parseFloat(values[key]) |
| | | }) |
| | | } |
| | | handleSave({ ...record, ...values }) |
| | | if (error && error[e.currentTarget.id]) { |
| | | return |
| | |
| | | |
| | | renderCell = form => { |
| | | this.form = form |
| | | const { children, dataIndex, record } = this.props |
| | | const { children, dataIndex, record, datatype } = 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 }}> |
| | |
| | | required: dataIndex === 'Value' || dataIndex === 'Text', |
| | | message: 'NOT NULL.', |
| | | }, |
| | | ...rules |
| | | ], |
| | | initialValue: record[dataIndex] |
| | | })(<Input ref={node => (this.input = node)} autoComplete="off" onPressEnter={this.save} onBlur={this.save} />)} |
| | |
| | | |
| | | let _width = '40%' |
| | | let fields = [] |
| | | let dataItem = props.data ? props.data[0] : '' |
| | | |
| | | if (props.type === 'link') { |
| | | _width = '27%' |
| | |
| | | title: field.label, |
| | | dataIndex: field.field, |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem[field.field]) === 'number' ? 'number' : 'string' |
| | | } |
| | | }) |
| | | } |
| | | |
| | | |
| | | |
| | | let columns = [ |
| | | { |
| | | title: 'Value', |
| | | dataIndex: 'Value', |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.Value) === 'number' ? 'number' : 'string' |
| | | }, |
| | | { |
| | | title: 'Text', |
| | | dataIndex: 'Text', |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.Text) === 'number' ? 'number' : 'string' |
| | | }, |
| | | ...fields, |
| | | { |
| | |
| | | title: 'ParentID', |
| | | dataIndex: 'ParentID', |
| | | width: '27%', |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.ParentID) === 'number' ? 'number' : 'string' |
| | | }) |
| | | } |
| | | |
| | | this.state = { |
| | | columns: columns, |
| | | columns: columns.map(col => { |
| | | if (col.dataIndex !== 'operation') { |
| | | col = {...col, ...this.getColumnSearchProps(col)} |
| | | } |
| | | return col |
| | | }), |
| | | dataSource: props.data, |
| | | count: props.data.length, |
| | | type: props.type, |
| | | linkSubFields: props.linkSubFields |
| | | } |
| | | } |
| | | |
| | | 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"> |
| | | String |
| | | </Radio> |
| | | <Radio style={{display: 'block', height: '30px', lineHeight: '30px'}} value="number"> |
| | | Number |
| | | </Radio> |
| | | </Radio.Group> |
| | | </div> |
| | | ), |
| | | filterIcon: () => ( |
| | | <Icon type="swap" style={{ color: column.datatype === 'number' ? '#1890ff' : undefined}} /> |
| | | ) |
| | | }) |
| | | |
| | | changeDatatype = (column, e) => { |
| | | const { columns, dataSource } = this.state |
| | | let value = e.target.value |
| | | |
| | | this.setState({ |
| | | dataSource: dataSource.map(item => { |
| | | let val = item[column.dataIndex] |
| | | if (value === 'number') { |
| | | try { |
| | | val = parseFloat(val) |
| | | if (isNaN(val)) { |
| | | val = '' |
| | | } |
| | | } catch { |
| | | val = '' |
| | | } |
| | | } else { |
| | | val = '' + val |
| | | } |
| | | |
| | | item[column.dataIndex] = val |
| | | |
| | | return item |
| | | }), |
| | | columns: columns.map(col => { |
| | | if (col.dataIndex === column.dataIndex) { |
| | | col.datatype = value |
| | | } |
| | | |
| | | if (col.dataIndex !== 'operation') { |
| | | col = {...col, ...this.getColumnSearchProps(col)} |
| | | } |
| | | |
| | | return col |
| | | }) |
| | | }) |
| | | } |
| | | |
| | | handleUpDown = (record, direction) => { |
| | |
| | | }) |
| | | } |
| | | |
| | | let dataItem = dataSource ? dataSource[0] : '' |
| | | |
| | | if (type === 'link') { |
| | | _width = '27%' |
| | | } else if (type === 'select') { |
| | |
| | | title: field.label, |
| | | dataIndex: field.field, |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem[field.field]) === 'number' ? 'number' : 'string' |
| | | } |
| | | }) |
| | | } |
| | |
| | | title: 'Value', |
| | | dataIndex: 'Value', |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.Value) === 'number' ? 'number' : 'string' |
| | | }, |
| | | { |
| | | title: 'Text', |
| | | dataIndex: 'Text', |
| | | width: _width, |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.Text) === 'number' ? 'number' : 'string' |
| | | }, |
| | | ...fields, |
| | | { |
| | |
| | | title: 'ParentID', |
| | | dataIndex: 'ParentID', |
| | | width: '27%', |
| | | editable: true |
| | | editable: true, |
| | | datatype: dataItem && typeof(dataItem.ParentID) === 'number' ? 'number' : 'string' |
| | | }) |
| | | } |
| | | |
| | | this.setState({ |
| | | columns: columns, |
| | | columns: columns.map(col => { |
| | | if (col.dataIndex !== 'operation') { |
| | | col = {...col, ...this.getColumnSearchProps(col)} |
| | | } |
| | | return col |
| | | }), |
| | | dataSource: dataSource, |
| | | type: type |
| | | }) |
| | |
| | | editable: col.editable, |
| | | dataIndex: col.dataIndex, |
| | | title: col.title, |
| | | datatype: col.datatype, |
| | | handleSave: this.handleSave, |
| | | }) |
| | | } |