From 3c1616197bf1ea448ba6651f4ef3790864925c65 Mon Sep 17 00:00:00 2001 From: Klemen Pukl Date: Mon, 17 Nov 2025 16:11:52 +0100 Subject: [PATCH] Show display name in table after add --- .../src/components/form/inputs/df-select.vue | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/vue/dynamicforms/src/components/form/inputs/df-select.vue b/vue/dynamicforms/src/components/form/inputs/df-select.vue index e1aaa753..2ed4130a 100644 --- a/vue/dynamicforms/src/components/form/inputs/df-select.vue +++ b/vue/dynamicforms/src/components/form/inputs/df-select.vue @@ -21,7 +21,7 @@ import { DfSelect } from '@dynamicforms/vuetify-inputs'; /** * TODO: There's no demo for AJAX loading. there is one, though in project-base (Impersonate user) */ -import { computed, ref } from 'vue'; +import { computed, ref, watch } from 'vue'; import { DfForm } from '../namespace'; @@ -40,6 +40,7 @@ interface Emits extends BaseEmits { } const emits = defineEmits(); +const firstQuery = ref(true); const { value, baseBinds } = useInputBase(props, emits); @@ -53,8 +54,38 @@ const options = computed(() => (isAjax.value ? undefined : props.field.choices)) const multiple = computed(() => props.field.renderParams.multiple); const taggable = computed(() => props.field.renderParams.allowTags); +const allOptions = computed(() => { + const optionsTmp = [...loadedChoices.value]; + if (options.value) optionsTmp.push(...options.value); + return optionsTmp; +}); + +const emitValueChange = (newValue: any) => { + if (newValue && allOptions.value) { + const text = multiple.value ? + newValue.map((i:DfForm.ChoicesJSON) => allOptions.value.find((j) => j.id === i)?.text) : + allOptions.value.find((i) => i.id === newValue)?.text; + if (text) { + emits('update:modelValueDisplay', text); + return; + } + } + emits('update:modelValueDisplay', ''); +}; + async function queryOptions(query: string, query_field: string): Promise { if (!isAjax.value) return; + const wasFirst = firstQuery.value; + if (wasFirst) { + firstQuery.value = false; + if (query && query_field === props.field.ajax.value_field) { + await queryOptions('', ''); + if (loadedChoices.value.find((i) => i.id === query)) { + emitValueChange(value.value); + return; + } + } + } /* @adam If field already defines the choices it is wasteful to query options, @@ -74,13 +105,19 @@ async function queryOptions(query: string, query_field: string): Promise { // Pagination was not delivered. We got a plain array loadedData = { results: loadedData, next: null }; } - loadedChoices.value = loadedData.results.map( + const loadedChoicesTmp = loadedData.results.map( (item: { [key: string]: any }): DfForm.ChoicesJSON => ({ id: item[props.field.ajax.value_field], text: item[props.field.ajax.text_field], icon: item[props.field.ajax.icon_field], }), ); + if (wasFirst) { + loadedChoices.value.push(...loadedChoicesTmp); + emitValueChange(value.value); + } else { + loadedChoices.value = loadedChoicesTmp; + } limit.value = loadedData.next ? loadedChoices.value.length : 99999; } @@ -88,4 +125,9 @@ async function fetchChoices(queryValue?: string, idValue?: any | any[]) { await queryOptions(idValue || queryValue, idValue ? props.field.ajax.value_field : props.field.ajax.query_field); return loadedChoices.value; } + +watch(value, (newValue) => { + emitValueChange(newValue); +}); +