fix: bug#10946

This commit is contained in:
xieyonghong 2023-03-26 21:42:23 +08:00
parent 3810046bee
commit dcd584c9c7
3 changed files with 43 additions and 7 deletions

View File

@ -1,7 +1,7 @@
import type { Slots } from 'vue'
import { TOKEN_KEY } from '@/utils/variable'
import { message } from 'jetlinks-ui-components';
import {cloneDeep} from "lodash-es";
import {cloneDeep, isArray} from "lodash-es";
/**
*
@ -137,12 +137,41 @@ export const handleParamsToString = (terms:SearchItemData[] = []) => {
}
export const treeFilter = (data: any[], value: any, key: string = 'name'): any[] => {
return data?.filter(item => {
if (!data) return []
return data.filter(item => {
if (item.children && item.children.length) {
item.children = treeFilter(item.children || [], value, key)
return !!item.children.length
} else {
return item[key] === value
}
}) || []
})
}
/**
*
* @param data
* @param search
* @param searchKey key
* @param returnKey key
*/
export const openKeysByTree = (data: any[], search: any, searchKey: string = 'id', returnKey: string = 'id'): any[] => {
if (!data || (data && !isArray(data))) return []
const cloneData = cloneDeep(data)
const filterTree = treeFilter(cloneData, search, searchKey)
const openKeys: any[] = []
const findKey = (treeData: any[]) => {
for (let i = 0; i < treeData.length; i++) {
const item = treeData[i]
openKeys.push(item[returnKey])
if (item.children && item.children.length) {
findKey(item.children)
}
}
}
findKey(filterTree)
return openKeys
}

View File

@ -32,11 +32,12 @@
/>
<div style='min-width: 400px' v-else>
<j-tree
v-model:expandedKeys="treeOpenKeys"
:selectedKeys='selectValue ? [selectValue] : []'
:treeData='options'
@select='treeSelect'
:height='450'
:virtual='true'
@select='treeSelect'
>
<template #title="{ name, description }">
<j-space>
@ -61,6 +62,7 @@ import DropMenus from './Menus.vue'
import DropdownTimePicker from './Time.vue'
import { getOption } from './util'
import type { DropdownButtonOptions } from './util'
import {openKeysByTree} from "@/utils/comm";
type LabelType = string | number | boolean | undefined
@ -109,7 +111,7 @@ const emit = defineEmits<Emit>()
const label = ref<LabelType>(props.placeholder)
const selectValue = ref(props.value)
const visible = ref(false)
const treeOpenKeys = ref<(string|number)[]>([])
const visibleChange = (v: boolean) => {
visible.value = v
}
@ -148,8 +150,9 @@ const menuSelect = (v: string, option: any) => {
watchEffect(() => {
const option = getOption(props.options, props.value, props.valueName)
selectValue.value = props.value
if (option) {
if (option) { //
label.value = option[props.labelName] || option.name
treeOpenKeys.value = openKeysByTree(props.options, props.value, props.valueName)
} else {
label.value = props.value !== undefined ? props.value : props.placeholder
}

View File

@ -48,11 +48,12 @@
<template v-else-if='item.component === "tree"'>
<div style='min-width: 400px' v-if='(item.key === "upper" ? metricOptions : options).length'>
<j-tree
v-model:expandedKeys="treeOpenKeys"
:selectedKeys='myValue ? [myValue] : []'
:treeData='item.key === "upper" ? metricOptions : options'
@select='treeSelect'
:height='450'
:virtual='true'
@select='treeSelect'
>
<template #title="{ name, description }">
<j-space>
@ -89,6 +90,7 @@ import { defaultSetting } from './typings'
import { DropdownMenus, DropdownTimePicker} from '../DropdownButton'
import { getOption } from '../DropdownButton/util'
import { isArray } from 'lodash-es'
import {openKeysByTree} from "@/utils/comm";
type Emit = {
(e: 'update:value', data: ValueType): void
@ -106,6 +108,7 @@ const emit = defineEmits<Emit>()
const myValue = ref<ValueType>(props.value)
const mySource = ref<string>(props.source)
const label = ref<any>(props.placeholder)
const treeOpenKeys = ref<(string|number)[]>([])
const visible = ref(false)
nextTick(() => {
@ -161,6 +164,7 @@ watchEffect(() => {
mySource.value = props.source
if (option) {
label.value = option[props.labelName] || option.name
treeOpenKeys.value = openKeysByTree(_options, props.value, props.valueName)
} else {
let doubleNull = false
if (isArray(props.value)) {