1
0
mirror of https://github.com/PanJiaChen/vue-element-admin.git synced 2025-08-10 20:39:48 +08:00

bugfix send values to server from calculator (#380)

* bugfix send values to server from calculator

* bugfix send values to field

* bug fix in allowed values

* support disabled to calculator

* change method of comparing regular expression

* bugfix calculate value from new record

* some bugfix

* bugfix values

* changes for values

Co-authored-by: elsiosanchez <elsiosanches@gmail.com>
Co-authored-by: erp <erp@erp.erp>
This commit is contained in:
Leonel Matos 2020-03-27 16:37:29 -04:00 committed by GitHub
parent c0c758d7ff
commit ee937abbe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 112 additions and 48 deletions

View File

@ -38,7 +38,7 @@ export default {
value: value,
showControls: true,
operation: '',
expression: /[^\d\/.()%\*\+\-]/gim,
expression: /[\d\/.()%\*\+\-]/gim,
valueToDisplay: '',
isShowed: false
}
@ -90,31 +90,55 @@ export default {
return Number(value)
},
calculateValue(event) {
const result = this.calculationValue(this.value, event)
if (!this.isEmptyValue(result)) {
this.valueToDisplay = result
this.isShowed = true
const isAllowed = event.key.match(this.expression)
if (isAllowed) {
const result = this.calculationValue(this.value, event)
if (!this.isEmptyValue(result)) {
this.valueToDisplay = result
this.isShowed = true
} else {
this.valueToDisplay = '...'
this.isShowed = true
}
} else if (!isAllowed && event.key === 'Backspace') {
if (String(this.value).slice(0, -1) > 0) {
event.preventDefault()
const newValue = String(this.value).slice(0, -1)
const result = this.calculationValue(newValue, event)
if (!this.isEmptyValue(result)) {
this.value = this.validateValue(result)
this.valueToDisplay = result
this.isShowed = true
} else {
this.valueToDisplay = '...'
this.isShowed = true
}
}
} else if (!isAllowed && event.key === 'Delete') {
if (String(this.value).slice(-1) > 0) {
event.preventDefault()
const newValue = String(this.value).slice(-1)
const result = this.calculationValue(newValue, event)
if (!this.isEmptyValue(result)) {
this.value = this.validateValue(result)
this.valueToDisplay = result
this.isShowed = true
} else {
this.valueToDisplay = '...'
this.isShowed = true
}
}
} else {
this.valueToDisplay = '...'
this.isShowed = true
event.preventDefault()
}
},
changeValue() {
const result = this.validateValue(this.valueToDisplay)
this.$store.dispatch('notifyFieldChange', {
isAdvancedQuery: this.metadata.isAdvancedQuery,
panelType: this.metadata.panelType,
parentUuid: this.metadata.parentUuid,
containerUuid: this.metadata.containerUuid,
columnName: this.metadata.columnName,
newValue: result,
field: this.metadata,
isChangedOldValue: true
})
.finally(() => {
this.clearVariables()
this.isShowed = false
})
if (!this.isEmptyValue(this.valueToDisplay) && this.valueToDisplay !== '...') {
const result = this.validateValue(this.valueToDisplay)
this.preHandleChange(result)
}
this.clearVariables()
this.isShowed = false
}
}
}

View File

@ -44,7 +44,7 @@
:record-uuid="field.recordUuid"
/>
<calculator
v-if="!isAdvancedQuery && isNumeric"
v-if="!isAdvancedQuery && isNumeric && !field.isReadOnlyFromLogic"
:field-attributes="fieldAttributes"
:field-value="field.value"
/>

View File

@ -1,10 +1,11 @@
<template>
<el-dropdown trigger="click">
<el-button type="text">
<el-button type="text" :disabled="fieldAttributes.readonly" @click="focusCalc">
<i class="el-icon-s-operation el-icon--right" />
</el-button>
<el-dropdown-menu slot="dropdown" class="dropdown-calc">
<el-input
ref="calculatorInput"
v-model="calcValue"
v-shortkey="['enter']"
class="calc-input"
@ -119,15 +120,15 @@ export default {
}, {
row1: {
type: 'value',
value: 7
value: '7'
},
row2: {
type: 'value',
value: 8
value: '8'
},
row3: {
type: 'value',
value: 9
value: '9'
},
row4: {
type: 'operator',
@ -140,15 +141,15 @@ export default {
}, {
row1: {
type: 'value',
value: 4
value: '4'
},
row2: {
type: 'value',
value: 5
value: '5'
},
row3: {
type: 'value',
value: 7
value: '7'
},
row4: {
type: 'operator',
@ -161,15 +162,15 @@ export default {
}, {
row1: {
type: 'value',
value: 1
value: '1'
},
row2: {
type: 'value',
value: 2
value: '2'
},
row3: {
type: 'value',
value: 3
value: '3'
},
row4: {
type: 'result',
@ -182,7 +183,7 @@ export default {
}, {
row1: {
type: 'value',
value: 0
value: '0'
},
row2: {
type: 'operator',
@ -212,13 +213,20 @@ export default {
sendValue(row, column) {
const isAcceptedType = ['result', 'clear'].includes(row[column.property].type)
if (!isAcceptedType && !this.isDisabled(row, column)) {
this.calcValue += row[column.property].value
this.isEmptyValue(this.calcValue) ? this.calcValue = row[column.property].value : this.calcValue += row[column.property].value
const result = this.calculationValue(this.calcValue, event)
if (!this.isEmptyValue(result)) {
this.valueToDisplay = result
} else {
this.valueToDisplay = '...'
}
}
if (row[column.property].type === 'clear') {
if (row[column.property].value === 'C') {
this.calcValue = this.calcValue.slice(0, -1)
} else if (row[column.property].value === 'AC') {
this.calcValue = ''
this.valueToDisplay = ''
}
}
if (row[column.property].value === '=') {
@ -226,19 +234,32 @@ export default {
}
},
changeValue() {
const result = Number(this.valueToDisplay)
this.$store.dispatch('notifyFieldChange', {
isAdvancedQuery: this.fieldAttributes.isAdvancedQuery,
panelType: this.fieldAttributes.panelType,
const newValue = Number(this.valueToDisplay)
let isSendCallout = true
const isSendToServer = true
const isChangedOldValue = false
if (this.fieldAttributes.isAdvancedQuery) {
isSendCallout = false
}
const sendParameters = {
parentUuid: this.fieldAttributes.parentUuid,
containerUuid: this.fieldAttributes.containerUuid,
columnName: this.fieldAttributes.columnName,
newValue: result,
field: this.fieldAttributes,
isChangedOldValue: true
panelType: this.fieldAttributes.panelType,
columnName: this.fieldAttributes.columnName,
newValue,
isAdvancedQuery: this.fieldAttributes.isAdvancedQuery,
isSendToServer,
isSendCallout,
isChangedOldValue
}
this.$store.dispatch('notifyFieldChange', {
...sendParameters
})
.finally(() => {
this.clearVariables()
this.$children[0].visible = false
})
},
spanMethod({ row, column, rowIndex, columnIndex }) {
@ -264,7 +285,7 @@ export default {
}
}
} else if (rowIndex === 4) {
if (row[column.property].value === 0) {
if (row[column.property].value === '0') {
return {
rowspan: 1,
colspan: 2
@ -292,6 +313,9 @@ export default {
} else {
this.valueToDisplay = '...'
}
},
focusCalc() {
this.$refs.calculatorInput.focus()
}
}
}

View File

@ -388,10 +388,12 @@ export function tagStatus(tag) {
let partialValue = ''
export function calculationValue(value, event) {
const VALIDATE_EXPRESSION = /[^\d\/.()%\*\+\-]/gim
if (!this.isEmptyValue(event) && !VALIDATE_EXPRESSION.test(event.key)) {
const isZero = Number(value) === 0
const VALIDATE_EXPRESSION = /[\d\/.()%\*\+\-]/gim
const isValidKey = VALIDATE_EXPRESSION.test(event.key)
if (event.type === 'keydown' && isValidKey) {
partialValue += event.key
const operation = String(value) + partialValue
const operation = isEmptyValue(value) || isZero ? partialValue : String(value) + partialValue
if (!isEmptyValue(operation)) {
try {
// eslint-disable-next-line no-eval
@ -400,9 +402,23 @@ export function calculationValue(value, event) {
return null
}
}
} else if (event.type === 'click') {
if (!isEmptyValue(value)) {
try {
// eslint-disable-next-line no-eval
return eval(value) + ''
} catch (error) {
return null
}
}
} else {
if (value.key === 'Backspace') {
partialValue = partialValue.slice(0, -1)
if ((event.key === 'Backspace' || event.key === 'Delete') && !isEmptyValue(value)) {
try {
// eslint-disable-next-line no-eval
return eval(value) + ''
} catch (error) {
return null
}
} else {
return null
}