Browse Source

选择用户实现单选多选兼容

lwx_v27
liwenxuan 2 weeks ago
parent
commit
2c06ce7920
  1. 120
      src/components/DesignForm/public/expand/rangedUserTree.vue

120
src/components/DesignForm/public/expand/rangedUserTree.vue

@ -26,35 +26,64 @@ const isDataLoaded = ref(false) // 标记数据是否已加载
const loading = ref(false) //
const treeSelectRef = ref() //
watch(value, (newValue) => {
console.log(newValue)
if (newValue.length > 0) {
let str = ""
let userAry = new Array
if(Array.isArray(newValue)){
newValue.forEach(item => {
userAry.push(item)
})
str = userAry.join(',')
emits('update:modelValue', str)
}else{
let a = new Array
a.push(newValue)
a.forEach(item => {
userAry.push(item)
})
str = userAry.join(',')
emits('update:modelValue', str)
}
//
const lastSelectedValue = ref(null)
//
const handleValueChange = (newValue) => {
if (!multiSelect.value) {
//
if (newValue.length === 0) {
//
emits('update:modelValue', '');
lastSelectedValue.value = null;
} else if (newValue.length === 1) {
//
const currentValue = newValue[0];
emits('update:modelValue', currentValue);
lastSelectedValue.value = currentValue;
} else {
//
const newSelections = newValue.filter(item => item !== lastSelectedValue.value);
if (newSelections.length > 0) {
//
const latestSelection = newSelections[newSelections.length - 1];
value.value = [latestSelection];
emits('update:modelValue', latestSelection);
lastSelectedValue.value = latestSelection;
} else {
//
value.value = newValue;
}
}
} else {
let str = ""
emits('update:modelValue', str)
//
if (newValue.length > 0) {
let str = ""
let userAry = new Array
if(Array.isArray(newValue)){
newValue.forEach(item => {
userAry.push(item)
})
str = userAry.join(',')
emits('update:modelValue', str)
}else{
let a = new Array
a.push(newValue)
a.forEach(item => {
userAry.push(item)
})
str = userAry.join(',')
emits('update:modelValue', str)
}
} else {
let str = ""
emits('update:modelValue', str)
}
}
}, { deep: true })
}
watch(value, handleValueChange, { deep: true })
const multiSelect = computed(()=>{
if(props.data.control.multiSelect && props.data.control.multiSelect == "1"){
@ -79,10 +108,33 @@ function parseStringToArray(str: string) {
onBeforeMount(() => {
setTimeout(() => {
value.value = parseStringToArray(props.modelValue)
const initialValue = parseStringToArray(props.modelValue)
value.value = initialValue;
if (initialValue.length > 0 && !multiSelect.value) {
lastSelectedValue.value = initialValue[initialValue.length - 1];
}
}, 500)
})
//
const processTreeData = (data: any[]) => {
return data.map(node => {
const newNode = { ...node };
// multiSelectfalseid5
if (!multiSelect.value && node.id && node.id.toString().length < 5) {
newNode.disabled = true;
}
//
if (newNode.children && Array.isArray(newNode.children)) {
newNode.children = processTreeData(newNode.children);
}
return newNode;
});
}
//
const loadFullData = async () => {
if (isDataLoaded.value) return treeData.value;
@ -94,9 +146,13 @@ const loadFullData = async () => {
await new Promise(resolve => setTimeout(resolve, 800));
const result = await checkorgAndManTree1();
treeData.value = result;
// multiSelect
const processedData = !multiSelect.value ? processTreeData(result) : result;
treeData.value = processedData;
isDataLoaded.value = true;
return result;
return processedData;
} catch (error) {
console.error('加载组织数据失败:', error);
return [];
@ -178,8 +234,6 @@ function hasNodesInTree(tree) {
</script>
<template>
<div class="tree-select-wrapper" >{{ multiSelect }}
<el-tree-select
ref="treeSelectRef"
@ -209,7 +263,6 @@ function hasNodesInTree(tree) {
</div>
</template>
<style lang='scss' scoped>
.tree-select-wrapper {
position: relative;
min-height: 40px;
@ -252,5 +305,4 @@ function hasNodesInTree(tree) {
color: #909399;
font-size: 14px;
}
</style>
</style>
Loading…
Cancel
Save