From 67d4471f42eb2d125103ad42f8c65d3e44e4fe70 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 12 Jan 2024 10:57:50 +0100 Subject: [PATCH] refs #6280 feat: improve VnLocation --- src/components/common/VnLocation.vue | 7 +++-- src/components/common/VnSelectFilter.vue | 38 ++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/components/common/VnLocation.vue b/src/components/common/VnLocation.vue index 70e818ade..3a296e168 100644 --- a/src/components/common/VnLocation.vue +++ b/src/components/common/VnLocation.vue @@ -135,16 +135,19 @@ function showLabel(data){ auto-load url="Countries" /> + [], + }, + filterRules: { + type: [Array, Function], default: () => [], }, isClearable: { @@ -47,16 +51,46 @@ function setOptions(data) { myOptions.value = JSON.parse(JSON.stringify(data)); myOptionsOriginal.value = JSON.parse(JSON.stringify(data)); } +function deepFind(obj, path) { + var paths = path.split('.') + , current = obj + , i; + + for (i = 0; i < paths.length; ++i) { + if (current[paths[i]] == undefined) { + return undefined; + } else { + current = current[paths[i]]; + } + } + return current; +} setOptions(options.value); const filter = (val, options) => { const search = val.toString().toLowerCase(); if (!search) return options; + if($props.filterRules.length) { + const passSomeRule = $props.filterRules.some((rule) => { + if(typeof rule === 'object') return true + const cond = eval(rule) + return cond; + }); + if(!passSomeRule) return options + } return options.filter((row) => { if ($props.filterOptions.length) { return $props.filterOptions.some((prop) => { - const propValue = String(row[prop]).toLowerCase(); + const passRules = $props.filterRules + .filter(rule=>typeof rule === 'object') + .every(rule=>{ + const propExists = Object.keys(rule).includes(prop); + if(!propExists) return false; + return eval(prop.concat(rule[prop])) + }); + const propValue = String(deepFind(row,prop)).toLowerCase(); + if(passRules) return propValue.includes(search); }); }