mirror of
https://github.com/dataease/dataease.git
synced 2026-05-14 12:22:10 +08:00
refactor: 优化外部参数过滤类型时默认值处理 (#17807)
This commit is contained in:
@@ -25,7 +25,11 @@ import { viewFieldTimeTrans } from '@/utils/viewUtils'
|
||||
import { useAppearanceStoreWithOut } from '@/store/modules/appearance'
|
||||
import { ElMessage } from 'element-plus-secondary'
|
||||
import { useI18n } from '@/hooks/web/useI18n'
|
||||
import { filterEnumParams, filterEnumParamsReduce } from '@/utils/componentUtils'
|
||||
import {
|
||||
filterEnumParams,
|
||||
filterEnumParamsReduce,
|
||||
filterParamsOptions
|
||||
} from '@/utils/componentUtils'
|
||||
import { formatterItem } from '@/views/chart/components/js/formatter'
|
||||
const { t } = useI18n()
|
||||
|
||||
@@ -1276,6 +1280,18 @@ export const dvMainStore = defineStore('dataVisualization', {
|
||||
if (targetMatchMode === 'filter') {
|
||||
// do filter
|
||||
filterItem['optionFilter'] = queryParams
|
||||
if (filterItem.defaultValueCheck) {
|
||||
const result = filterParamsOptions(
|
||||
deepCopy(filterItem['selectValue']),
|
||||
deepCopy(queryParams)
|
||||
)
|
||||
if (result) {
|
||||
filterItem['selectValue'] = queryParams
|
||||
filterItem['defaultValue'] = queryParams
|
||||
} else {
|
||||
filterItem.defaultValueCheck = false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!['1', '7'].includes(filterItem.displayType)) {
|
||||
// 查询组件除了时间组件 其他入参只支持文本 这里全部转为文本
|
||||
|
||||
@@ -60,3 +60,74 @@ export const filterEnumMapSync = async componentData => {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function filterParamsOptions(params, paramsOption) {
|
||||
// 如果 params 为空,直接返回 null
|
||||
if (!params || (Array.isArray(params) && params.length === 0)) {
|
||||
return null
|
||||
}
|
||||
// 如果 paramsOption 为空,直接返回 null
|
||||
if (!paramsOption || paramsOption.length === 0) {
|
||||
return null
|
||||
}
|
||||
// 创建 paramsOption 集合和前缀集合用于快速查找
|
||||
const optionSet = new Set(paramsOption)
|
||||
const prefixSet = new Set()
|
||||
// 收集所有可能的父级前缀
|
||||
paramsOption.forEach(option => {
|
||||
if (option.includes('-de-')) {
|
||||
const parts = option.split('-de-')
|
||||
// 收集所有前缀:父级、祖父级等
|
||||
for (let i = 1; i < parts.length; i++) {
|
||||
const prefix = parts.slice(0, i).join('-de-')
|
||||
prefixSet.add(prefix)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 检查一个值是否在 paramsOption 中存在(考虑层级关系)
|
||||
function checkValueExists(value) {
|
||||
// 直接存在
|
||||
if (optionSet.has(value)) {
|
||||
return true
|
||||
}
|
||||
// 如果是层级结构,检查所有父级前缀
|
||||
if (value.includes('-de-')) {
|
||||
const parts = value.split('-de-')
|
||||
|
||||
// 检查所有可能的父级前缀
|
||||
for (let i = 1; i < parts.length; i++) {
|
||||
const prefix = parts.slice(0, i).join('-de-')
|
||||
if (optionSet.has(prefix)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查该值是否是某个选项的父级
|
||||
// 如:paramsOption 中有 "香橙店-de-浓郁椰奶",传入 "香橙店" 也应该匹配
|
||||
if (
|
||||
Array.from(optionSet).some(option => option.startsWith(value + '-de-') || option === value)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
// 检查该值是否是某个选项的前缀(通过 prefixSet)
|
||||
if (prefixSet.has(value)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
// 处理单值情况(字符串)
|
||||
if (typeof params === 'string') {
|
||||
return checkValueExists(params) ? params : null
|
||||
}
|
||||
// 处理数组情况
|
||||
if (Array.isArray(params)) {
|
||||
// 过滤出存在的值
|
||||
const filtered = params.filter(value => typeof value === 'string' && checkValueExists(value))
|
||||
// 如果过滤后为空,返回 null,否则返回过滤后的数组
|
||||
return filtered.length > 0 ? filtered : null
|
||||
}
|
||||
// 其他类型返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user