fix: Lookup with -1 as default value. (#435)

pull/3759/head
Edwin Betancourt 2020-04-06 13:12:55 -04:00 committed by GitHub
parent 6c62dec4ad
commit 78e2c19468
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 62 deletions

View File

@ -49,10 +49,11 @@ export default {
label: ' ',
key: undefined
}],
blankValues: [null, undefined, -1],
blankOption: {
// label with '' value is assumed to be undefined non-existent
label: ' ',
key: undefined || -1
key: undefined
}
}
},
@ -73,21 +74,10 @@ export default {
}
return styleClass
},
getterLookupItem() {
if (this.isEmptyValue(this.metadata.reference.directQuery)) {
return this.blankOption
}
return this.$store.getters.getLookupItem({
parentUuid: this.metadata.parentUuid,
containerUuid: this.metadata.containerUuid,
directQuery: this.metadata.reference.directQuery,
tableName: this.metadata.reference.tableName,
value: this.value
})
},
getterLookupList() {
if (this.isEmptyValue(this.metadata.reference.query)) {
return this.blankOption
if (this.isEmptyValue(this.metadata.reference.query) ||
!this.metadata.displayed) {
return [this.blankOption]
}
return this.$store.getters.getLookupList({
parentUuid: this.metadata.parentUuid,
@ -106,17 +96,11 @@ export default {
value: this.value
})
if (this.isEmptyValue(allOptions) || !this.blankOptionInfo.hasBlankOption) {
if (this.isEmptyValue(allOptions) || (allOptions.length &&
(!this.blankValues.includes(allOptions[0].key)))) {
allOptions.unshift(this.blankOption)
}
return allOptions
},
blankOptionInfo() {
return {
hasBlankOption: this.options.some(option => option.label === ' '),
index: this.options.findIndex(option => option.label === ' '),
option: this.options.find(option => option.label === ' ')
}
}
},
watch: {
@ -179,10 +163,17 @@ export default {
}
}
}
},
'metadata.displayed'(value) {
if (value) {
this.changeBlankOption()
this.options = this.getterLookupAll
}
}
},
beforeMount() {
if (this.metadata.displayed) {
this.changeBlankOption()
this.options = this.getterLookupAll
if (!this.isEmptyValue(this.value) && !this.metadata.isAdvancedQuery) {
if (!this.findLabel(this.value)) {
@ -194,7 +185,8 @@ export default {
})
} else {
if (!this.isPanelWindow || (this.isPanelWindow &&
(this.isEmptyValue(this.metadata.optionCRUD) || this.metadata.optionCRUD === 'create-new'))) {
(this.isEmptyValue(this.metadata.optionCRUD) ||
this.metadata.optionCRUD === 'create-new'))) {
this.getDataLookupItem()
}
}
@ -203,6 +195,20 @@ export default {
}
},
methods: {
changeBlankOption() {
if (Number(this.metadata.defaultValue) === -1) {
this.blankOption = {
label: ' ',
key: -1
}
}
if (this.value === undefined || this.value === null) {
this.blankOption = {
label: ' ',
key: undefined
}
}
},
preHandleChange(value) {
const label = this.findLabel(this.value)
this.handleChange(value, undefined, label)
@ -237,10 +243,8 @@ export default {
attributeValue: responseLookupItem.label
})
}
this.changeBlankOption()
this.options = this.getterLookupAll
if (this.options.length && !this.options[0].key) {
this.options.unshift(this.blankOption)
}
})
.finally(() => {
this.isLoading = false
@ -252,7 +256,8 @@ export default {
getDataLookupList(isShowList) {
if (isShowList) {
// TODO: Evaluate if length = 1 and this element key = blankOption
if (this.getterLookupList.length === 0) {
const list = this.getterLookupList
if (this.isEmptyValue(list) || (list.length === 1 && this.blankValues.includes(list[0]))) {
this.remoteMethod()
}
}
@ -269,6 +274,7 @@ export default {
query: this.metadata.reference.query
})
.then(responseLookupList => {
this.changeBlankOption()
this.options = this.getterLookupAll
})
.finally(() => {
@ -282,10 +288,16 @@ export default {
tableName: this.metadata.reference.tableName,
query: this.metadata.reference.query,
directQuery: this.metadata.reference.directQuery,
value: this.metadata.value
value: this.value
})
// TODO: Evaluate if is number -1 or string '' (or default value)
this.options = this.getterLookupAll
// set empty list and empty option
this.changeBlankOption()
const list = []
list.push(this.blankOption)
this.options = list
// set empty value
this.value = this.blankOption.key
}
}

View File

@ -1,5 +1,5 @@
import { getLookup, getLookupList } from '@/api/ADempiere/data'
import { getCurrentRole } from '@/utils/ADempiere/auth'
import { getToken as getSession } from '@/utils/auth'
import { isEmptyValue } from '@/utils/ADempiere/valueUtils'
import { parseContext } from '@/utils/ADempiere/contextUtils'
@ -26,6 +26,14 @@ const lookup = {
}
},
actions: {
/**
* Get display column from lookup
* @param {string} parentUuid
* @param {string} containerUuid
* @param {string} tableName
* @param {string} directQuery
* @param {string|number} value identifier or key
*/
getLookupItemFromServer({ commit, rootGetters }, {
parentUuid,
containerUuid,
@ -64,7 +72,7 @@ const lookup = {
value, // isNaN(objectParams.value) ? objectParams.value : parseInt(objectParams.value, 10),
parsedDirectQuery: directQuery,
tableName,
roleUuid: getCurrentRole(),
sessionUuid: getSession(),
clientId: rootGetters.getContextClientId
})
return option
@ -74,8 +82,11 @@ const lookup = {
})
},
/**
* tableName,
* query
* Get display column's list from lookup
* @param {string} parentUuid
* @param {string} containerUuid
* @param {string} tableName
* @param {string} query
*/
getLookupListFromServer({ commit, rootGetters }, {
parentUuid,
@ -100,21 +111,25 @@ const lookup = {
query: parsedQuery
})
.then(lookupListResponse => {
const options = lookupListResponse.recordsList.map(itemLookup => {
return {
const list = []
lookupListResponse.recordsList.forEach(itemLookup => {
const key = itemLookup.values.KeyColumn
if (![null, -1, undefined].includes(key)) {
list.push({
label: itemLookup.values.DisplayColumn,
key: itemLookup.values.KeyColumn
key
})
}
})
commit('addLoockupList', {
list: options,
list,
tableName,
parsedQuery,
roleUuid: getCurrentRole(),
sessionUuid: getSession(),
clientId: rootGetters.getContextClientId
})
return options
return list
})
.catch(error => {
console.warn(`Get Lookup List, Select Base - Error ${error.code}: ${error.message}.`)
@ -141,7 +156,7 @@ const lookup = {
return itemLookup.parsedDirectQuery !== parsedDirectQuery &&
itemLookup.tableName !== tableName &&
itemLookup.value !== value &&
itemLookup.roleUuid !== getCurrentRole()
itemLookup.sessionUuid !== getSession()
})
let parsedQuery = query
@ -156,7 +171,7 @@ const lookup = {
const lookupList = state.lookupList.filter(itemLookup => {
return itemLookup.parsedQuery !== parsedQuery &&
itemLookup.tableName !== tableName &&
itemLookup.roleUuid !== getCurrentRole()
itemLookup.sessionUuid !== getSession()
})
commit('deleteLookupList', {
lookupItem,
@ -184,7 +199,7 @@ const lookup = {
const lookupItem = state.lookupItem.find(itemLookup => {
return itemLookup.parsedDirectQuery === parsedDirectQuery &&
itemLookup.tableName === tableName &&
itemLookup.roleUuid === getCurrentRole() &&
itemLookup.sessionUuid === getSession() &&
itemLookup.clientId === rootGetters.getContextClientId &&
itemLookup.value === value
})
@ -211,16 +226,11 @@ const lookup = {
const lookupList = state.lookupList.find(itemLookup => {
return itemLookup.parsedQuery === parsedQuery &&
itemLookup.tableName === tableName &&
itemLookup.roleUuid === getCurrentRole() &&
itemLookup.sessionUuid === getSession() &&
itemLookup.clientId === rootGetters.getContextClientId
})
if (lookupList) {
const resultLookup = lookupList.list.filter(lookup => {
if (lookup.key !== undefined) {
return lookup
}
})
return resultLookup
return lookupList.list
}
return []
},
@ -235,13 +245,6 @@ const lookup = {
directQuery,
value
}) => {
const item = getters.getLookupItem({
parentUuid,
containerUuid,
tableName,
directQuery,
value
})
const list = getters.getLookupList({
parentUuid,
containerUuid,
@ -249,9 +252,19 @@ const lookup = {
query
})
const allList = list
if (item && !list.find(itemLookup => itemLookup.key === item.key)) {
// set item values getter from server into list
if (isEmptyValue(list)) {
const item = getters.getLookupItem({
parentUuid,
containerUuid,
tableName,
directQuery,
value
})
if (!isEmptyValue(item)) {
allList.push(item)
}
}
return allList
}
}