From cadfd4ed559fb170bb81d5bafbb6ae9775ba1ddc Mon Sep 17 00:00:00 2001 From: king <18310653075@163.com> Date: 星期四, 03 三月 2022 18:18:39 +0800 Subject: [PATCH] 2022-03-03 --- src/tabviews/custom/components/table/edit-table/normalTable/index.jsx | 207 ++++++++++++++++++++++++++++++++------------------- 1 files changed, 129 insertions(+), 78 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 59d6bd5..987b60d 100644 --- a/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx +++ b/src/tabviews/custom/components/table/edit-table/normalTable/index.jsx @@ -2,7 +2,7 @@ import PropTypes from 'prop-types' import { is, fromJS } from 'immutable' import { Table, Typography, Switch, Modal, Input, InputNumber, Tooltip, Button, notification, message, Select } from 'antd' -import { ExclamationCircleOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons' +import { ExclamationCircleOutlined, EditOutlined, PlusOutlined, PlusCircleOutlined, DeleteOutlined } from '@ant-design/icons' import moment from 'moment' import Api from '@/api' @@ -126,7 +126,7 @@ tdFocus = (id) => { const { col, record } = this.props - if (id !== col.uuid + record.$Index) return + if (id !== col.uuid + record.$$uuid) return this.focus() } @@ -137,9 +137,9 @@ this.setState({editing: false}) setTimeout(() => { if (col.enter === '$next') { - MKEmitter.emit('nextLine', col, record.$Index) + MKEmitter.emit('nextLine', col, record.$$uuid) } else { - MKEmitter.emit('tdFocus', col.enter + record.$Index) + MKEmitter.emit('tdFocus', col.enter + record.$$uuid) } }, 50) @@ -153,7 +153,7 @@ if (col.editType === 'switch' || col.editType === 'select') { this.setState({editing: true}, () => { - let node = document.getElementById(col.uuid + record.$Index) + let node = document.getElementById(col.uuid + record.$$uuid) node && node.click() }) } else { @@ -175,7 +175,7 @@ } this.setState({editing: true, value: val, err}, () => { - let node = document.getElementById(col.uuid + record.$Index) + let node = document.getElementById(col.uuid + record.$$uuid) node && node.select() }) } @@ -220,9 +220,9 @@ setTimeout(() => { if (col.enter === '$next') { - MKEmitter.emit('nextLine', col, record.$Index) + MKEmitter.emit('nextLine', col, record.$$uuid) } else { - MKEmitter.emit('tdFocus', col.enter + record.$Index) + MKEmitter.emit('tdFocus', col.enter + record.$$uuid) } }, 50) @@ -254,9 +254,9 @@ setTimeout(() => { if (col.enter === '$next') { - MKEmitter.emit('nextLine', col, record.$Index) + MKEmitter.emit('nextLine', col, record.$$uuid) } else { - MKEmitter.emit('tdFocus', col.enter + record.$Index) + MKEmitter.emit('tdFocus', col.enter + record.$$uuid) } }, 50) @@ -308,7 +308,7 @@ 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}/> + <Input id={col.uuid + record.$$uuid} 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') { @@ -322,7 +322,7 @@ <Select showSearch defaultValue={_value} - id={col.uuid + record.$Index} + id={col.uuid + record.$$uuid} onBlur={() => this.setState({editing: false})} filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0} onSelect={this.onSelectChange} @@ -383,7 +383,7 @@ if (col.editable === 'true') { if (editing) { return (<td className="editing_table_cell"> - <InputNumber id={col.uuid + record.$Index} defaultValue={value} onChange={(val) => this.onChange(val)} onPressEnter={this.enterPress} onBlur={this.onBlur}/> + <InputNumber id={col.uuid + record.$$uuid} defaultValue={value} onChange={(val) => this.onChange(val)} onPressEnter={this.enterPress} onBlur={this.onBlur}/> {err ? <Tooltip title={err}><ExclamationCircleOutlined /></Tooltip> : null} </td>) } else { @@ -470,8 +470,8 @@ ) } - // 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>) + return (<td className={className} style={style}>{col.addable ? <PlusCircleOutlined onClick={() => MKEmitter.emit('addRecord', col.tableId, {...record})} className="mk-editable mk-plus"/> : null}{children}{col.delable ? <DeleteOutlined onClick={() => MKEmitter.emit('delRecord', col.tableId, {...record})} className="mk-editable mk-del"/> : null}</td>) + // return (<td className={className} style={style}>{children}</td>) } } @@ -558,19 +558,19 @@ _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: setting.tableId}, - // }) - // }) - // } + if (setting.delable !== 'false' && setting.operType !== 'buoyMode') { + 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 = `#${setting.tableId} table, #${setting.tableId} tr, #${setting.tableId} th, #${setting.tableId} td {border-color: ${setting.borderColor}}` @@ -623,6 +623,7 @@ } MKEmitter.addListener('nextLine', this.nextLine) + MKEmitter.addListener('addRecord', this.addLine) MKEmitter.addListener('delRecord', this.delRecord) MKEmitter.addListener('resetTable', this.resetTable) MKEmitter.addListener('changeRecord', this.changeRecord) @@ -636,6 +637,7 @@ return } MKEmitter.removeListener('nextLine', this.nextLine) + MKEmitter.removeListener('addRecord', this.addLine) MKEmitter.removeListener('delRecord', this.delRecord) MKEmitter.removeListener('resetTable', this.resetTable) MKEmitter.removeListener('changeRecord', this.changeRecord) @@ -644,7 +646,7 @@ UNSAFE_componentWillReceiveProps(nextProps) { if (!is(fromJS(this.props.data), fromJS(nextProps.data))) { this.setState({data: nextProps.data || []}) - if (this.state.editable === 'true') { + if (this.state.editable === 'true' && !this.state.pickup) { setTimeout(() => { this.pickupChange() }, 200) @@ -820,7 +822,7 @@ MKEmitter.emit('tdFocus', initEditLine.uuid + (index + 1)) } else if (col.footEnter === 'add' && setting.addable === 'true') { setTimeout(() => { - this.plusLine(initEditLine) + this.plusLine() }, 10) } else if (col.footEnter === 'sub') { setTimeout(() => { @@ -829,14 +831,14 @@ } } - plusLine = (initEditLine) => { - const { edData, fields } = this.state + plusLine = () => { + const { edData, fields, initEditLine } = this.state let item = {...edData[edData.length - 1]} - item.key = item.key + 1 - item.$$uuid = '$new' - item.$Index = item.key + 1 + '' + item.$$uuid = Utils.getguid() + item.$type = 'add' + item.$Index = '' fields.forEach(col => { if (col.initval !== '$copy') { @@ -854,7 +856,7 @@ }) this.setState({edData: [...edData, item]}, () => { - MKEmitter.emit('tdFocus', initEditLine.uuid + item.$Index) + MKEmitter.emit('tdFocus', initEditLine.uuid + item.$$uuid) }) } @@ -865,17 +867,14 @@ let _data = [] - if (record.$$uuid === '$new') { - _data = edData.filter(item => item.$Index !== record.$Index) - _data = _data.map((item, index) => { - item.key = index - item.$Index = 1 + index + '' - return item - }) + if (record.$type === 'add') { + _data = edData.filter(item => item.$$uuid !== record.$$uuid) } else { _data = edData.map(item => { - if (item.$Index === record.$Index) { + if (item.$$uuid === record.$$uuid) { record.$deleted = true + record.$origin = false + record.$type = 'del' return record } else { return item @@ -892,7 +891,8 @@ if (id !== tableId) return let _data = this.state.edData.map(item => { - if (item.$Index === record.$Index) { + if (item.$$uuid === record.$$uuid) { + record.$origin = false return record } else { return item @@ -902,40 +902,73 @@ this.setState({edData: _data}) } - addLine = () => { + addLine = (id, record) => { const { BID } = this.props - const { edData, fields } = this.state + const { edData, fields, tableId } = this.state - let item = {} - if (edData.length > 0) { - item = {...edData[edData.length - 1]} - item.key = item.key + 1 - item.$$uuid = '$new' - item.$Index = item.key + 1 + '' - } else { - item.key = 0 - item.$$uuid = '$new' - item.$Index = item.key + 1 + '' + if (id) { + if (id !== tableId) return + let _edData = fromJS(edData).toJS() + let index = _edData.findIndex(item => record.$$uuid === item.$$uuid) + + let item = {} + + item.$$uuid = Utils.getguid() + item.$type = 'add' + item.$Index = '' item.$$BID = BID || '' - } - - fields.forEach(col => { - if (col.initval !== '$copy') { - item[col.field] = col.initval - } - if (col.type === 'number') { - item[col.field] = +item[col.field] - if (isNaN(item[col.field])) { - item[col.field] = 0 + + fields.forEach(col => { + if (col.initval !== '$copy') { + item[col.field] = col.initval } - } + if (col.type === 'number') { + item[col.field] = +item[col.field] + if (isNaN(item[col.field])) { + item[col.field] = 0 + } + } + + if (item[col.field] === undefined) { + item[col.field] = '' + } + }) - if (item[col.field] === undefined) { - item[col.field] = '' - } - }) + _edData.splice(index, 0, item) - this.setState({edData: [...edData, item]}) + this.setState({edData: _edData}) + } else { + let item = {} + if (edData.length > 0) { + item = {...edData[edData.length - 1]} + item.$$uuid = Utils.getguid() + item.$type = 'add' + item.$Index = '' + } else { + item.$$uuid = Utils.getguid() + item.$type = 'add' + item.$Index = '' + item.$$BID = BID || '' + } + + fields.forEach(col => { + if (col.initval !== '$copy') { + item[col.field] = col.initval + } + if (col.type === 'number') { + item[col.field] = +item[col.field] + if (isNaN(item[col.field])) { + item[col.field] = 0 + } + } + + if (item[col.field] === undefined) { + item[col.field] = '' + } + }) + + this.setState({edData: [...edData, item]}) + } } checkData = () => { @@ -950,6 +983,7 @@ return } let err = '' + let Index = 1 let data = fromJS(edData).toJS().map(item => { let line = [] fields.forEach(col => { @@ -995,7 +1029,10 @@ }) if (line.length > 0) { - err += `绗�${item.$Index}琛岋細` + line.join('锛�') + '锛�' + err += `绗�${Index}琛岋細` + line.join('锛�') + '锛�' + } + if (!item.$deleted) { + Index++ } return item @@ -1013,8 +1050,21 @@ } submit = (data) => { - const { submit, BID } = this.props + const { submit, BID, setting } = this.props const { fields } = this.state + + if (setting.commit === 'change') { + data = data.filter(item => !item.$origin) + } + + if (data.length === 0) { + notification.warning({ + top: 92, + message: '鏁版嵁鏈慨鏀癸紝涓嶅彲鎻愪氦锛�', + duration: 5 + }) + return + } let result = getEditTableSql(submit, data, fields) @@ -1271,10 +1321,11 @@ let height = setting.height || false return ( - <div className={`edit-custom-table ${pickup ? 'editable' : ''} ${setting.tableHeader || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}> - <Switch title="缂栬緫" className="main-pickup" checkedChildren="寮�" unCheckedChildren="鍏�" checked={pickup} onChange={this.pickupChange} /> + <div className={`edit-custom-table ${pickup ? 'editable' : ''} ${setting.tableHeader || ''} ${setting.operType || ''} ${height ? 'fixed-height' : ''} ${setting.mode || ''}`} id={tableId}> + <Switch title="缂栬緫" className="main-pickup" checkedChildren="寮�" unCheckedChildren="鍏�" disabled={loading || this.props.loading} checked={pickup} onChange={this.pickupChange} /> {pickup ? <Button style={submit.style} onClick={() => setTimeout(() => {this.checkData()}, 10)} loading={loading} className="submit-table" type="link">鎻愪氦</Button> : null} <Table + rowKey="$$uuid" components={components} style={setting.style} size={setting.size || 'middle'} @@ -1293,7 +1344,7 @@ pagination={_pagination} /> {_footer ? <div className={'normal-table-footer ' + (_pagination ? 'pagination' : '')}>{_footer}</div> : null} - {pickup && setting.addable === 'true' ? <Button onClick={this.addLine} style={{display: 'block', width: '100%', color: '#26C281', border: '1px solid #dddddd', borderRadius: 0}} type="link"><PlusOutlined /></Button> : null} + {pickup && setting.addable === 'true' ? <Button className="mk-add-line" onClick={() => this.addLine()} disabled={this.props.loading} type="link"><PlusOutlined /></Button> : null} </div> ) } -- Gitblit v1.8.0