fix(orderCatalogFilter): fix searchByTag
gitea/salix-front/pipeline/pr-master This commit looks good Details

This commit is contained in:
Alex Moreno 2024-11-18 15:06:09 +01:00
parent 00fdbfa2e4
commit c2db7be8cd
1 changed files with 116 additions and 181 deletions

View File

@ -1,5 +1,5 @@
<script setup> <script setup>
import { computed, ref } from 'vue'; import { computed, ref, onMounted } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { useRoute } from 'vue-router'; import { useRoute } from 'vue-router';
import axios from 'axios'; import axios from 'axios';
@ -9,10 +9,9 @@ import VnSelect from 'components/common/VnSelect.vue';
import VnFilterPanelChip from 'components/ui/VnFilterPanelChip.vue'; import VnFilterPanelChip from 'components/ui/VnFilterPanelChip.vue';
import VnInput from 'src/components/common/VnInput.vue'; import VnInput from 'src/components/common/VnInput.vue';
import getParamWhere from 'src/filters/getParamWhere'; import getParamWhere from 'src/filters/getParamWhere';
// import CatalogFilterValueDialog from 'src/pages/Order/Card/CatalogFilterValueDialog.vue';
import { useArrayData } from 'composables/useArrayData';
const { t } = useI18n();
const route = useRoute();
const props = defineProps({ const props = defineProps({
dataKey: { dataKey: {
type: String, type: String,
@ -27,13 +26,19 @@ const props = defineProps({
required: true, required: true,
}, },
}); });
const { t } = useI18n();
const route = useRoute();
const arrayData = useArrayData(props.dataKey);
const currentParams = ref({});
const categoryList = ref(null); const categoryList = ref(null);
const selectedCategoryFk = ref(getParamWhere(route, 'categoryFk')); const selectedCategoryFk = ref(null);
const typeList = ref([]); const typeList = ref([]);
const selectedTypeFk = ref(null); const selectedTypeFk = ref(null);
const selectedTag = ref(null); const searchByTag = ref(null);
const tagValues = ref([{}]);
const tagOptions = ref([]);
const vnFilterPanelRef = ref(); const vnFilterPanelRef = ref();
const orderByList = ref([ const orderByList = ref([
{ id: 'relevancy DESC, name', name: t('params.relevancy'), priority: 999 }, { id: 'relevancy DESC, name', name: t('params.relevancy'), priority: 999 },
@ -48,32 +53,24 @@ const orderWayList = ref([
const orderBySelected = ref('relevancy DESC, name'); const orderBySelected = ref('relevancy DESC, name');
const orderWaySelected = ref('ASC'); const orderWaySelected = ref('ASC');
const createValue = (val, done) => { const resetCategory = (params, search) => {
if (val.length > 2) {
if (!tagOptions.value.includes(val)) {
done(tagOptions.value, 'add-unique');
}
tagValues.value.push({ value: val });
}
};
const resetCategory = () => {
selectedCategoryFk.value = null; selectedCategoryFk.value = null;
typeList.value = null; typeList.value = null;
}; params.categoryFk = null;
params.typeFk = null;
const clearFilter = (key) => { arrayData.store.userFilter = null;
if (key === 'categoryFk') { removeTagGroupParam(params, search);
resetCategory();
}
}; };
const selectCategory = (params, category, search) => { const selectCategory = (params, category, search) => {
if (params.categoryFk === category?.id) { if (params.categoryFk === category?.id) {
resetCategory(); resetCategory(params, search);
params.categoryFk = null; params.categoryFk = null;
} else { } else {
selectedCategoryFk.value = category?.id; selectedCategoryFk.value = category?.id;
params.categoryFk = category?.id; params.categoryFk = category?.id;
params.typeFk = null;
selectedTypeFk.value = null;
loadTypes(category?.id); loadTypes(category?.id);
} }
search(); search();
@ -86,19 +83,12 @@ const loadTypes = async (categoryFk = selectedCategoryFk.value) => {
typeList.value = data; typeList.value = data;
}; };
const selectedCategory = computed(() => const selectedCategory = computed(() => {
(categoryList.value || []).find( return (categoryList.value || []).find(
(category) => category?.id === selectedCategoryFk.value (category) => category?.id === selectedCategoryFk.value
) );
); });
function filterFn(val, update) {
update(() => {
const needle = val.toLowerCase();
tagOptions.value = props.tagValue.filter(
(v) => v.toLowerCase().indexOf(needle) > -1
);
});
}
const selectedType = computed(() => { const selectedType = computed(() => {
return (typeList.value || []).find((type) => type?.id === selectedTypeFk.value); return (typeList.value || []).find((type) => type?.id === selectedTypeFk.value);
}); });
@ -114,37 +104,38 @@ function exprBuilder(param, value) {
} }
} }
const applyTagFilter = (params, search) => { const applyTags = (tagInfo, params, search) => {
if (!tagValues.value?.length) { if (!tagInfo || !tagInfo.values.length) {
params.tagGroups = null; params.tagGroups = null;
search(); search();
return; return;
} }
if (!params.tagGroups) {
params.tagGroups = []; if (!params.tagGroups) params.tagGroups = [];
} params.tagGroups.push(tagInfo);
params.tagGroups.push(
JSON.stringify({
values: tagValues.value.filter((obj) => Object.keys(obj).length > 0),
tagSelection: {
...selectedTag.value,
orgShowField: selectedTag?.value?.name,
},
tagFk: selectedTag?.value?.tagFk,
})
);
search(); search();
selectedTag.value = null;
tagValues.value = [{}];
}; };
const removeTagChip = (selection, params, search) => { async function onSearchByTag(value) {
if (params.tagGroups) { if (!value.target.value) return;
params.tagGroups = (params.tagGroups || []).filter( if (!currentParams.value?.tagGroups) {
(value) => value !== selection currentParams.value.tagGroups = [];
); }
currentParams.value.tagGroups.push({
values: [{ value: value.target.value }],
});
searchByTag.value = null;
}
const removeTagGroupParam = (params, search, valIndex = null) => {
if (!valIndex) {
params.tagGroups = null;
search();
} else {
params.tagGroups.splice(valIndex, 1);
search();
} }
search();
}; };
const setCategoryList = (data) => { const setCategoryList = (data) => {
@ -171,26 +162,27 @@ function addOrder(value, field, params) {
params.orderBy = JSON.stringify(orderBy); params.orderBy = JSON.stringify(orderBy);
vnFilterPanelRef.value.search(); vnFilterPanelRef.value.search();
} }
onMounted(() => {
selectedCategoryFk.value = getParamWhere(route, 'categoryFk');
selectedTypeFk.value = getParamWhere(route, 'typeFk');
});
</script> </script>
<template> <template>
<FetchData url="ItemCategories" limit="30" auto-load @on-fetch="setCategoryList" /> <FetchData url="ItemCategories" auto-load @on-fetch="setCategoryList" />
<VnFilterPanel <VnFilterPanel
ref="vnFilterPanelRef" ref="vnFilterPanelRef"
:data-key="props.dataKey" :data-key="props.dataKey"
:hidden-tags="['orderFk', 'orderBy']" :hidden-tags="['orderFk', 'orderBy']"
:un-removable-params="['orderFk', 'orderBy']" :un-removable-params="['orderFk', 'orderBy']"
:expr-builder="exprBuilder" :expr-builder="exprBuilder"
:custom-tags="['tagGroups']" :custom-tags="['tagGroups', 'categoryFk']"
@remove="clearFilter"
:redirect="false" :redirect="false"
search-url="params" v-model="currentParams"
> >
<template #tags="{ tag, formatFn }"> <template #tags="{ tag, formatFn }">
<strong v-if="tag.label === 'categoryFk'"> <strong v-if="tag.label === 'typeFk'">
{{ t(selectedCategory?.name || '') }}
</strong>
<strong v-else-if="tag.label === 'typeFk'">
{{ t(selectedType?.name || '') }} {{ t(selectedType?.name || '') }}
</strong> </strong>
<div v-else class="q-gutter-x-xs"> <div v-else class="q-gutter-x-xs">
@ -199,25 +191,37 @@ function addOrder(value, field, params) {
</div> </div>
</template> </template>
<template #customTags="{ tags: customTags, params, searchFn }"> <template #customTags="{ tags: customTags, params, searchFn }">
<template v-for="tag in customTags" :key="tag.label"> <template v-for="customTag in customTags" :key="customTag.label">
<template v-if="tag.label === 'tagGroups'"> <VnFilterPanelChip
<VnFilterPanelChip v-for="(tag, valIndex) in Array.isArray(customTag.value)
v-for="chip in tag.value" ? customTag.value
:key="chip" : 1"
removable :key="valIndex"
@remove="removeTagChip(chip, params, searchFn)" removable
> @remove="
<strong> {{ JSON.parse(chip).tagSelection?.name }}: </strong> customTag.label === 'categoryFk'
<span>{{ ? resetCategory(params, searchFn)
(JSON.parse(chip).values || []) : removeTagGroupParam(params, searchFn, valIndex)
.map((item) => item.value) "
.join(' | ') >
}}</span> <strong v-if="customTag.label === 'categoryFk'">
</VnFilterPanelChip> {{ t(selectedCategory?.name || '') }}
</template> </strong>
<strong v-if="tag?.tagSelection?.name" class="q-mr-xs">
{{ tag.tagSelection.name }}:
</strong>
<span>
{{
(tag?.values || [])
.map((item) => `"${item.value}"`)
.join(', ')
}}
</span>
</VnFilterPanelChip>
</template> </template>
</template> </template>
<template #body="{ params, searchFn }"> <template #body="{ params, searchFn }">
{{ params }}
<QItem class="category-filter q-mt-md"> <QItem class="category-filter q-mt-md">
<div <div
v-for="category in categoryList" v-for="category in categoryList"
@ -298,91 +302,39 @@ function addOrder(value, field, params) {
</QItemSection> </QItemSection>
</QItem> </QItem>
<QSeparator /> <QSeparator />
<QItem class="q-mt-md"> <QItem class="q-mt-lg q-pa-none">
<QItemSection>
<VnSelect
:label="t('params.tag')"
v-model="selectedTag"
:options="props.tags || []"
option-value="id"
option-label="name"
dense
outlined
rounded
:emit-value="false"
use-input
/>
</QItemSection>
</QItem>
<QItem
v-for="(value, index) in tagValues"
:key="value"
class="q-mt-md filter-value"
>
<FetchData
v-if="selectedTag"
:url="`Tags/${selectedTag}/filterValue`"
limit="30"
auto-load
@on-fetch="(data) => (tagOptions = data)"
/>
<VnSelect
v-if="!selectedTag"
:label="t('params.value')"
v-model="value.value"
:options="tagOptions || []"
option-value="value"
option-label="value"
dense
outlined
rounded
emit-value
use-input
class="filter-input"
@new-value="createValue"
@filter="filterFn"
@update:model-value="applyTagFilter(params, searchFn)"
/>
<VnSelect
v-else-if="selectedTag === 1"
:label="t('params.value')"
v-model="value.value"
:options="tagOptions || []"
option-value="value"
option-label="value"
dense
outlined
rounded
emit-value
use-input
class="filter-input"
@new-value="createValue"
@update:model-value="applyTagFilter(params, searchFn)"
/>
<VnInput <VnInput
v-else :label="t('components.itemsFilterPanel.value')"
:label="t('params.value')"
v-model="value.value"
dense dense
outlined outlined
rounded rounded
class="filter-input" :is-clearable="false"
@keyup.enter="applyTagFilter(params, searchFn)" v-model="searchByTag"
/> @keyup.enter="(val) => onSearchByTag(val, params)"
<QIcon >
name="delete" <template #prepend>
class="filter-icon" <QIcon name="search" />
@click="(tagValues || []).splice(index, 1)" </template>
/> <template #append>
</QItem> <QBtn
<QItem class="q-mt-lg"> icon="add_circle"
<QBtn shortcut="+"
icon="add_circle" flat
shortcut="+" color="primary"
flat size="md"
class="filter-icon" dense
@click="tagValues.push({})" />
/> <QPopupProxy>
<!-- <CatalogFilterValueDialog
style="display: inline-block"
:tags="tags"
@apply-tags="
($event) => applyTags($event, params, searchFn)
"
/> -->
</QPopupProxy>
</template>
</VnInput>
</QItem> </QItem>
<QSeparator /> <QSeparator />
</template> </template>
@ -417,23 +369,6 @@ function addOrder(value, field, params) {
cursor: pointer; cursor: pointer;
} }
} }
.filter-icon {
font-size: 24px;
color: $primary;
padding: 0 4px;
cursor: pointer;
}
.filter-input {
flex-shrink: 1;
min-width: 0;
}
.filter-value {
display: flex;
align-items: center;
}
</style> </style>
<i18n> <i18n>