Commit d170037 1 parent b5df611 commit d170037 Copy full SHA for d170037
File tree 1 file changed +34
-0
lines changed
packages/components/tree-select
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,40 @@ export default defineComponent({
77
77
} ,
78
78
) ;
79
79
80
+ /**
81
+ * 递归查找指定节点的所有父节点的 value。
82
+ * @param targetValue 要查找的目标节点 value
83
+ * @param nodes 树形数据源
84
+ * @param ancestors 父节点 value 累加数组
85
+ * @returns 则返回父节点 value 数组
86
+ */
87
+ function findParentValues ( options : TreeOptionData [ ] , targetValue : string | number ) : ( string | number ) [ ] {
88
+ function helper ( nodes : TreeOptionData [ ] , parentValues : ( string | number ) [ ] ) : ( string | number ) [ ] | null {
89
+ for ( const node of nodes ) {
90
+ // 找到目标节点,返回当前记录的父节点值数组
91
+ if ( node . value === targetValue ) {
92
+ return parentValues ;
93
+ }
94
+ // 递归处理子节点,将当前节点的 value 加入父节点路径
95
+ if ( Array . isArray ( node . children ) ) {
96
+ const found = helper ( node . children , [ ...parentValues , node . value ] ) ;
97
+ if ( found ) return found ;
98
+ }
99
+ }
100
+ return null ;
101
+ }
102
+
103
+ const result = helper ( options , [ ] ) ;
104
+ return result || [ ] ;
105
+ }
106
+ watch ( actived , ( ) => {
107
+ if ( actived . value . length > 0 ) {
108
+ const activedExpanded = actived . value . map ( ( val ) => findParentValues ( props . data , val ) ) . flat ( ) ;
109
+ expanded . value = Array . from ( new Set ( [ ...expanded . value , ...activedExpanded ] ) ) ;
110
+ popupVisible . value = true ;
111
+ }
112
+ } ) ;
113
+
80
114
// computed
81
115
/** filterByText keep pace with innerInputValue */
82
116
const filterByText = computed ( ( ) => {
You can’t perform that action at this time.
0 commit comments