Browse Source

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

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

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

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