| | |
| | | import React, {Component} from 'react' |
| | | import PropTypes from 'prop-types' |
| | | import { is, fromJS } from 'immutable' |
| | | import { Table, Typography, 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 } 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, 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' |
| | | |
| | |
| | | focus = () => { |
| | | const { col, record } = this.props |
| | | |
| | | if (col.editType === 'switch' || col.editType === 'select') { |
| | | this.setState({editing: true}, () => { |
| | | let node = document.getElementById(col.uuid + record.$Index) |
| | | node && node.click() |
| | | }) |
| | | } else { |
| | | let err = null |
| | | let val = record[col.field] !== undefined ? record[col.field] : '' |
| | | |
| | |
| | | let node = document.getElementById(col.uuid + record.$Index) |
| | | node && node.select() |
| | | }) |
| | | } |
| | | } |
| | | |
| | | onBlur = () => { |
| | |
| | | err = '请填写' + col.label |
| | | } |
| | | 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.$Index) |
| | | } else { |
| | | MKEmitter.emit('tdFocus', col.enter + record.$Index) |
| | | } |
| | | }, 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.$Index) |
| | | } else { |
| | | MKEmitter.emit('tdFocus', col.enter + record.$Index) |
| | | } |
| | | }, 50) |
| | | |
| | | MKEmitter.emit('changeRecord', col.tableId, {...record, ...values}) |
| | | } |
| | | |
| | | switchBlur = () => { |
| | | setTimeout(() => { |
| | | this.setState({editing: false}) |
| | | }, 10) |
| | | } |
| | | |
| | | render() { |
| | |
| | | |
| | | if (col.editable === 'true') { |
| | | if (editing) { |
| | | if (!col.editType || col.editType === 'text') { |
| | | 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}><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.$Index} |
| | | 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>) |
| | | } |
| | |
| | | ) |
| | | } |
| | | |
| | | // return (<td className={className} style={style}>{col.addable ? <PlusCircleOutlined className="mk-editable mk-plus"/> : null}{children}{col.delable ? <DeleteOutlined className="mk-editable mk-del"/> : null}</td>) |
| | | return (<td className={className} style={style}>{children}</td>) |
| | | } |
| | | } |
| | |
| | | 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) { |
| | |
| | | _columns.push(_item) |
| | | }) |
| | | |
| | | if (setting.delable !== 'false') { |
| | | edColumns.push({ |
| | | align: 'center', |
| | | dataIndex: 'mkoperation', |
| | | title: '操作', |
| | | sorter: false, |
| | | width: 100, |
| | | onCell: record => ({ |
| | | record, |
| | | col: {type: 'operation', tableId: tableId}, |
| | | }) |
| | | }) |
| | | } |
| | | // if (setting.delable !== 'false') { |
| | | // edColumns.push({ |
| | | // align: 'center', |
| | | // dataIndex: 'mkoperation', |
| | | // title: '操作', |
| | | // sorter: false, |
| | | // width: 100, |
| | | // onCell: record => ({ |
| | | // record, |
| | | // 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) |
| | |
| | | data, |
| | | columns: _columns, |
| | | edColumns, |
| | | tableId, |
| | | tableId: setting.tableId, |
| | | orderfields, |
| | | initEditLine, |
| | | editable: setting.editable |
| | | }, () => { |
| | | if (deForms.length > 0) { |
| | | this.improveActionForm(deForms) |
| | | } |
| | | }) |
| | | } |
| | | |
| | |
| | | componentDidMount () { |
| | | 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.filter(item => item.field !== setting.primaryKey), |
| | |
| | | } |
| | | } |
| | | |
| | | 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) => { |
| | | const { setting } = this.props |
| | | const { edData, initEditLine, tableId } = this.state |