add tree 可以不选中父子

This commit is contained in:
2023-05-17 08:56:58 +08:00
parent a19040aa60
commit adc3f9f7d3
6 changed files with 126 additions and 95 deletions

View File

@@ -24,6 +24,7 @@ export interface TreeData {
isLeaf: boolean;
hasNextSibling: boolean;
parentNode: Nullable<TreeData>;
selectParent:boolean;
}
export interface TreeNodeProps {
@@ -36,6 +37,7 @@ export interface TreeNodeProps {
collapseTransition: boolean;
onlyIconControl: boolean;
hideicon?: boolean;
selectParent:boolean;
}
interface TreeNodeEmits {
@@ -81,7 +83,7 @@ function recursiveNodeClick(node: TreeData) {
}
function handleChange(checked: boolean, node: TreeData) {
props.tree.setCheckedKeys(checked, props.checkStrictly, node);
props.tree.setCheckedKeys(checked, props.checkStrictly || props.selectParent, node);
}
function handleIconClick(node: TreeData) {
@@ -106,15 +108,14 @@ function handleRowClick(node: TreeData) {
//判断是否半选
const isChildAllSelected = computed(() => {
function _isChildAllSelected(node: TreeData): boolean {
if (!props.showCheckbox) {
return false;
}
let childSelectNum = 0;
let res = false; // true为半选 false为全选
for (const item of node.children) {
if (item.isChecked) childSelectNum++;
}
if (childSelectNum > 0) node.isChecked = true; //此处的处理与 checkedKeys 有关联
if(!props.selectParent){
if (childSelectNum > 0) node.isChecked = true; //此处的处理与 checkedKeys 有关联
}
if (childSelectNum == node.children.length) {
//继续递归向下判断
for (const item of node.children) {
@@ -128,11 +129,14 @@ const isChildAllSelected = computed(() => {
}
return (node: TreeData): boolean => {
console.log(props.selectParent)
if (props.checkStrictly) {
return false;
} else {
} else if(props.selectParent) {
let res = _isChildAllSelected(node);
return res;
}else{
return false
}
};
});
@@ -198,6 +202,7 @@ const isChildAllSelected = computed(() => {
:checkStrictly="checkStrictly"
:only-icon-control="onlyIconControl"
:hideicon="props.hideicon"
:select-parent="props.selectParent"
@node-click="recursiveNodeClick"
>
<template v-if="$slots.title" v-slot:title="slotProp: { data: any }">

View File

@@ -39,6 +39,7 @@ export interface TreeProps {
showCheckbox?: boolean;
replaceFields?: ReplaceFieldsOptions;
hideicon?: boolean;
selectParent?: boolean;
}
interface TreeEmits {
@@ -65,6 +66,7 @@ const props = withDefaults(defineProps<TreeProps>(), {
title: "title",
};
},
selectParent:false
});
const slots = useSlots();
@@ -146,6 +148,7 @@ function handleClick(node: TreeData) {
:only-icon-control="onlyIconControl"
@node-click="handleClick"
:hideicon="props.hideicon"
:selectParent="props.selectParent"
>
<template v-if="$slots.title" v-slot:title="{ data }">
<slot name="title" :data="data"></slot>

View File

@@ -94,7 +94,7 @@ class Tree {
checkStrictly,
replaceFields: { children, id, title },
} = this.config;
console.log(origin,"origin")
const nodeKey = Reflect.get(origin, id);
const nodeTitle = Reflect.get(origin, title);
const nodeChildren = Reflect.get(origin, children);
@@ -112,7 +112,7 @@ class Tree {
isChecked: false,
isLeaf: false,
hasNextSibling: hasNextSibling,
parentNode: parentNode || null,
parentNode: parentNode || null
});
node.isDisabled = nodeDisabled;
@@ -137,6 +137,7 @@ class Tree {
}
setChildrenChecked(checked: boolean, nodes: TreeData[]) {
console.log("走赋值子节点了")
var ableCount = 0;
var checkCount = 0;
const len = nodes.length;
@@ -163,6 +164,7 @@ class Tree {
}
setParentChecked(checked: boolean, parent: TreeData) {
console.log("走赋值父节点了")
if (!parent) {
return;
}
@@ -183,6 +185,7 @@ class Tree {
node: TreeData
) {
node.isChecked = checked;
console.log(node,checkStrictly,186)
if (!checkStrictly) {
if (node.parentNode) {
this.setParentChecked(checked, node.parentNode);