From 2b45840e53d250f517874bea495bffaeda172807 Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期四, 03 三月 2022 12:52:16 +0800 Subject: [PATCH] 2022-03-03 --- src/tabviews/custom/components/table/edit-table/normalTable/index.jsx | 364 ++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 294 insertions(+), 70 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 1464bfa..59d6bd5 100644 --- a/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx +++ b/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx @@ -1,17 +1,19 @@ 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' @@ -149,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.$Index) + 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.$Index) + node && node.select() + }) } - - this.setState({editing: true, value: val, err}, () => { - let node = document.getElementById(col.uuid + record.$Index) - node && node.select() - }) } onBlur = () => { @@ -202,6 +211,62 @@ 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() { @@ -241,10 +306,31 @@ 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}><ExclamationCircleOutlined /></Tooltip> : null} - </td>) + 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>) } @@ -384,6 +470,7 @@ ) } + // 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>) } } @@ -425,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) { @@ -481,22 +558,22 @@ _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) @@ -516,10 +593,14 @@ data, columns: _columns, edColumns, - tableId, + tableId: setting.tableId, orderfields, initEditLine, editable: setting.editable + }, () => { + if (deForms.length > 0) { + this.improveActionForm(deForms) + } }) } @@ -530,19 +611,6 @@ 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), @@ -583,6 +651,162 @@ } } } + + 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 -- Gitblit v1.8.0