diff --git a/src/components/ADempiere/Field/FieldSelect.vue b/src/components/ADempiere/Field/FieldSelect.vue index 48161285..f6ff57c5 100644 --- a/src/components/ADempiere/Field/FieldSelect.vue +++ b/src/components/ADempiere/Field/FieldSelect.vue @@ -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 } } diff --git a/src/store/modules/ADempiere/lookup.js b/src/store/modules/ADempiere/lookup.js index 6eb8e983..5abef39e 100644 --- a/src/store/modules/ADempiere/lookup.js +++ b/src/store/modules/ADempiere/lookup.js @@ -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,9 +82,12 @@ 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, containerUuid, @@ -100,21 +111,25 @@ const lookup = { query: parsedQuery }) .then(lookupListResponse => { - const options = lookupListResponse.recordsList.map(itemLookup => { - return { - label: itemLookup.values.DisplayColumn, - key: itemLookup.values.KeyColumn + const list = [] + lookupListResponse.recordsList.forEach(itemLookup => { + const key = itemLookup.values.KeyColumn + if (![null, -1, undefined].includes(key)) { + list.push({ + label: itemLookup.values.DisplayColumn, + 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,8 +252,18 @@ const lookup = { query }) const allList = list - if (item && !list.find(itemLookup => itemLookup.key === item.key)) { - allList.push(item) + // 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 }