@sy-common/organize-select-help 1.0.0-beta.5 → 1.0.0-beta.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,158 +1,498 @@
1
1
  <template>
2
2
  <div class="content">
3
3
  <Spin fix v-if="loading"></Spin>
4
- <Tree :data="data" ref="tree" :load-data="loadData" :render="renderContent" check-directly
5
- @on-select-change ="handleChange" multiple></Tree>
4
+ <div class="tree-inner">
5
+ <Tree
6
+ :data="data"
7
+ ref="tree"
8
+ :load-data="loadData"
9
+ :render="renderContent"
10
+ multiple
11
+ :check-strictly="true"
12
+ :cascade-check="false"
13
+ @on-select-change="handleChange"
14
+ ></Tree>
15
+ </div>
6
16
  </div>
7
17
  </template>
18
+
8
19
  <script>
9
- import ajax from '@lambo-design/shared/utils/ajax'
20
+ import { deepCopy } from '@lambo-design/core/src/utils/assist';
21
+ import ajax from '@lambo-design/shared/utils/ajax';
22
+
10
23
  export default {
11
- props:{
12
- disabled:{
24
+ props: {
25
+ disabled: {
26
+ type: Boolean,
27
+ default: false
28
+ },
29
+ treeList: {
30
+ type: Array,
31
+ default: () => []
32
+ },
33
+ isSingleSelect: {
13
34
  type: Boolean,
14
35
  default: false
15
36
  }
16
37
  },
17
- data(){
38
+ data() {
18
39
  return {
19
- data:[],
20
- manageUnitId:'',
21
- loading:true
22
- }
40
+ data: [],
41
+ manageUnitId: '',
42
+ loading: true
43
+ };
23
44
  },
24
45
  mounted() {
25
- this.initData()
46
+ this.initData();
26
47
  },
27
- methods:{
28
- async initData(){
29
- let data = await this.getOrgChildren()
30
- this.loading = false
48
+ methods: {
49
+ async initData() {
50
+ let data = await this.getOrgChildren();
51
+ this.loading = false;
31
52
  this.data = data;
32
53
  },
33
- handleChange(data){
34
- this.$emit('handleChange',data)
54
+
55
+ // 树组件内的 handleChange 方法修改(仅保留核心逻辑,移除冗余触发)
56
+ handleChange(data) {
57
+ // 仅处理复选框触发的选中数据,过滤空数据
58
+ if (!data || (Array.isArray(data) && data.length === 0)) {
59
+ this.$emit('handleChange', []);
60
+ return;
61
+ }
62
+
63
+ let checkedData = Array.isArray(data)
64
+ ? data.filter(item => item.checked)
65
+ : (data.checked ? [data] : []);
66
+
67
+ // 单选逻辑:仅保留最后一个选中的节点
68
+ if (this.isSingleSelect) {
69
+ checkedData = checkedData.length > 0 ? [checkedData[checkedData.length - 1]] : [];
70
+ // 清空其他节点的选中状态
71
+ this.clearOtherCheckedNodes(checkedData.map(item => item.orgUnitId));
72
+ }
73
+
74
+ this.syncAllNodeCheckedState(checkedData);
75
+ // 仅当有有效选中节点时才触发父组件事件
76
+ this.$emit('handleChange', checkedData);
35
77
  },
36
- async loadData(item, callback){
37
- // 模拟异步获取数据
38
- let children = await this.getOrgChildren(item.orgUnitId)
39
- callback(children); // 调用callback传入子节点数据
78
+ clearOtherCheckedNodes(keepIds) {
79
+ if (!Array.isArray(this.data) || !Array.isArray(keepIds)) return;
80
+
81
+ const clearNodeState = (nodeList) => {
82
+ nodeList.forEach(node => {
83
+ if (!keepIds.includes(node.orgUnitId) && node.checked) {
84
+ this.$set(node, 'checked', false);
85
+ }
86
+ // 递归处理子节点(单选时也需要清空子节点的选中状态)
87
+ const childNodes = node.children || node.orgChildrenList || [];
88
+ if (childNodes.length) {
89
+ clearNodeState(childNodes);
90
+ }
91
+ });
92
+ };
93
+
94
+ clearNodeState(this.data);
95
+ },
96
+
97
+ syncAllNodeCheckedState(checkedData) {
98
+ if (!Array.isArray(this.data) || !Array.isArray(checkedData)) return;
99
+
100
+ const checkedIds = checkedData.map(item => item.orgUnitId);
101
+ const updateNodeState = (nodeList) => {
102
+ nodeList.forEach(node => {
103
+ // 仅更新当前节点,跳过子节点
104
+ const shouldBeChecked = checkedIds.includes(node.orgUnitId);
105
+ if (node.checked !== shouldBeChecked) {
106
+ this.$set(node, 'checked', shouldBeChecked);
107
+ }
108
+ // 彻底删除子节点递归逻辑
109
+ });
110
+ };
111
+ updateNodeState(this.data);
40
112
  },
41
- checkNode(data){
42
- // console.log(data,'data')
113
+
114
+ async loadData(item, callback) {
115
+ let children = await this.getOrgChildren(item.orgUnitId);
116
+ callback(children);
43
117
  },
44
- getOrgChildren(orgUnitId){
45
- return new Promise((resolve,reject)=>{
46
- ajax.get('/pub-manage-server/pub/personHelpBox/q/getOrgUnitList', { params:{
47
- containsCurLevel:true,
48
- orgUnitId:orgUnitId,
118
+
119
+ getOrgChildren(orgUnitId = '') {
120
+ return new Promise((resolve, reject) => {
121
+ ajax.get('/pub-manage-server/pub/personHelpBox/q/getOrgUnitList', {
122
+ params: {
123
+ containsCurLevel: true,
124
+ orgUnitId: orgUnitId
49
125
  }
50
- }).then((res)=>{
51
- if(res.data.code === 1){
52
- let treeList = res.data.data
53
- this.initTree(treeList)
54
- resolve(treeList)
55
- }else{
56
- resolve([])
126
+ }).then((res) => {
127
+ if (res.data.code === 1) {
128
+ let treeList = res.data.data;
129
+ this.initTree(treeList);
130
+ resolve(treeList);
131
+ } else {
132
+ resolve([]);
57
133
  }
58
- }).catch(err=>{
59
- console.log(err)
60
- })
61
- })
62
- },
63
- initTree(treeList){
64
- const defineTree = function(list){
65
- if(!list)return
134
+ }).catch(err => {
135
+ console.log(err);
136
+ resolve([]);
137
+ });
138
+ });
139
+ },
140
+
141
+ setCheckedNodes(targets) {
142
+ if (!this.$refs.tree || !Array.isArray(targets)) {
143
+ this.clearAllChecked(this.data);
144
+ this.$emit('handleChange', []);
145
+ this.$nextTick(() => this.$refs.tree.$forceUpdate());
146
+ return;
147
+ }
148
+
149
+ // 单选逻辑强化:清空所有选中状态
150
+ this.clearAllChecked(this.data);
151
+
152
+ const targetList = this.isSingleSelect
153
+ ? (targets.length > 0 ? [targets[targets.length - 1]] : [])
154
+ : targets;
155
+
156
+ // 转换为有效orgUnitId数组
157
+ const targetIds = targetList.map(item => {
158
+ if (typeof item === 'string') return item.trim();
159
+ return (item.orgUnitId || item.id || '').trim();
160
+ }).filter(id => id); // 过滤空ID
161
+
162
+ // 查找并选中目标节点
163
+ const findAndCheckNode = (treeData, targetId) => {
164
+ for (const node of treeData) {
165
+ const nodeId = (node.orgUnitId || node.id || '').trim();
166
+ if (nodeId === targetId) {
167
+ this.$set(node, 'checked', true);
168
+ this.expandToNode(node);
169
+ return true;
170
+ }
171
+ const childNodes = node.children || node.orgChildrenList || [];
172
+ if (childNodes.length && findAndCheckNode(childNodes, targetId)) {
173
+ return true;
174
+ }
175
+ }
176
+ return false;
177
+ };
178
+
179
+ targetIds.forEach(id => findAndCheckNode(this.data, id));
180
+
181
+ // 传递过滤后的有效节点
182
+ const checkedNodes = this.collectCurrentCheckedNodes(this.data);
183
+ this.$nextTick(() => {
184
+ this.$refs.tree.$forceUpdate();
185
+ this.$emit('handleChange', checkedNodes);
186
+ });
187
+ },
188
+
189
+ expandToNode(node) {
190
+ let current = node;
191
+ while (current && current.parent) {
192
+ if (typeof current.parent === 'object' && !Array.isArray(current.parent)) {
193
+ this.$set(current.parent, 'expand', true);
194
+ }
195
+ current = current.parent.orgUnitId
196
+ ? this.findNodeInTree(this.data, current.parent.orgUnitId)
197
+ : null;
198
+ }
199
+ },
200
+
201
+ collectAllCheckedNodes(nodeList) {
202
+ let checkedNodes = [];
203
+ nodeList.forEach(node => {
204
+ if (node.checked) {
205
+ checkedNodes.push(node);
206
+ }
207
+ const childNodes = node.children || node.orgChildrenList || [];
208
+ if (childNodes.length) {
209
+ checkedNodes = [...checkedNodes, ...this.collectAllCheckedNodes(childNodes)];
210
+ }
211
+ });
212
+ return checkedNodes;
213
+ },
214
+
215
+ clearAllChecked(treeData) {
216
+ if (!Array.isArray(treeData)) return;
217
+ treeData.forEach(node => {
218
+ if (node.checked) {
219
+ this.$set(node, 'checked', false);
220
+ }
221
+ const childNodes = node.children || node.orgChildrenList || [];
222
+ if (childNodes.length) {
223
+ this.clearAllChecked(childNodes);
224
+ }
225
+ });
226
+ },
227
+
228
+ updateTreeData(newTreeData) {
229
+ if (!Array.isArray(newTreeData)) return;
230
+ const tree = deepCopy(newTreeData);
231
+ this.initTree(tree);
232
+ this.data = tree;
233
+ this.$nextTick(() => {
234
+ this.$refs.tree.$forceUpdate();
235
+ });
236
+ },
237
+
238
+ findNodeInTree(treeData, targetOrgUnitId) {
239
+ if (!treeData || !targetOrgUnitId) return null;
240
+
241
+ for (const node of treeData) {
242
+ if (!node.parent && treeData !== this.data) {
243
+ node.parent = treeData;
244
+ }
245
+
246
+ if (node.orgUnitId === targetOrgUnitId) return node;
247
+
248
+ const childNodes = node.children || node.orgChildrenList || [];
249
+ if (childNodes.length) {
250
+ const childNode = this.findNodeInTree(childNodes, targetOrgUnitId);
251
+ if (childNode) return childNode;
252
+ }
253
+ }
254
+ return null;
255
+ },
256
+
257
+ initTree(treeList) {
258
+ const defineTree = function (list) {
259
+ if (!list) return;
66
260
  list.forEach((item) => {
67
- item.title=item.orgNodeName;
261
+ item.title = item.orgNodeName;
68
262
  item.loading = false;
69
263
  item.children = [];
70
264
  item.expand = false;
71
- item.checked = false;
72
- if(item.leafNode){
265
+ item.checked = false; // 确保checked属性初始化
266
+ if (item.leafNode) {
73
267
  delete item.loading;
74
268
  delete item.children;
75
269
  }
76
- })
77
- return list
78
- }
79
- treeList.forEach(item=>{
80
- item.title=item.orgNodeName;
270
+ if (item.orgChildrenList && item.orgChildrenList.length) {
271
+ item.children = defineTree(item.orgChildrenList);
272
+ item.expand = true;
273
+ }
274
+ });
275
+ return list;
276
+ };
277
+ treeList.forEach(item => {
278
+ item.title = item.orgNodeName;
81
279
  item.loading = false;
82
280
  item.children = [];
83
281
  item.expand = false;
84
- item.checked = false;
85
- if(item.orgChildrenList && item.orgChildrenList.length){
282
+ item.checked = false; // 初始化Checkbox绑定的checked属性
283
+ if (item.orgChildrenList && item.orgChildrenList.length) {
86
284
  item.children = defineTree(item.orgChildrenList);
87
285
  item.expand = true;
88
286
  }
89
- if(this.disabled){
287
+ if (this.disabled) {
90
288
  item.disabled = true;
91
289
  }
92
- if(item.leafNode){
290
+ if (item.leafNode) {
93
291
  delete item.loading;
94
292
  delete item.children;
95
293
  }
96
- })
294
+ });
97
295
  },
98
- renderContent(h, { root, node, data }){
296
+
297
+ //renderContent添加Checkbox组件
298
+ renderContent(h, {root, node, data}) {
99
299
  return h('div', {
100
300
  style: {
101
301
  width: '100%',
102
- overflow:'hidden',
103
- display:'flex',
302
+ overflow: 'hidden',
303
+ display: 'flex',
104
304
  alignItems: 'center',
305
+ cursor: 'default' // 文本区域鼠标指针改为默认
306
+ },
307
+ // 阻断整个节点容器的点击事件(仅复选框除外)
308
+ on: {
309
+ click: (e) => {
310
+ // 仅当点击复选框时放行,其他区域阻断
311
+ const isCheckbox = e.target.closest('.ivu-checkbox') || e.target.closest('.ivu-checkbox-icon');
312
+ if (!isCheckbox) {
313
+ e.stopPropagation();
314
+ e.preventDefault();
315
+ }
316
+ }
105
317
  }
106
318
  }, [
107
- h('img', {
108
- attrs: {
109
- src: require('./assets/icon.png'),
110
- },
111
- style: {
112
- marginRight: '8px',
113
- width:'14px',
114
- height:'14px',
115
- }
116
- }),
117
- h('span',{
118
- style: {
119
- overflow: 'hidden',
120
- textOverflow:'ellipsis',
121
- flex:1
122
- },
123
- on:{
124
- click:()=>{
125
- this.checkNode(data)
319
+ h('Checkbox', {
320
+ props: {
321
+ value: data.checked,
322
+ disabled: data.disabled || this.disabled,
323
+ indeterminate: false
324
+ },
325
+ style: {
326
+ marginRight: '8px',
327
+ cursor: 'pointer' // 复选框保留点击指针
328
+ },
329
+ on: {
330
+ 'on-change': (checked) => {
331
+ this.$set(data, 'checked', checked);
332
+
333
+ // 单选逻辑:勾选当前节点时,清空其他所有节点
334
+ if (this.isSingleSelect && checked) {
335
+ this.clearOtherCheckedNodes([data.orgUnitId]);
126
336
  }
337
+
338
+ // 收集选中节点(单选时仅返回当前节点)
339
+ const checkedNodes = this.isSingleSelect
340
+ ? (checked ? [data] : [])
341
+ : this.collectCurrentCheckedNodes(this.data);
342
+
343
+ this.handleChange(checkedNodes);
127
344
  }
128
- }, data.title)
345
+ }
346
+ }),
347
+ h('img', {
348
+ attrs: { src: require('./assets/icon.png') },
349
+ style: {
350
+ marginRight: '8px',
351
+ width: '14px',
352
+ height: '14px',
353
+ pointerEvents: 'none' // 图标禁用点击
354
+ }
355
+ }),
356
+ h('span', {
357
+ style: {
358
+ // overflow: 'hidden',
359
+ // textOverflow: 'ellipsis',
360
+ flex: 1,
361
+ pointerEvents: 'none', // 文本完全禁用点击
362
+ userSelect: 'none' // 禁止文本选中
363
+ }
364
+ }, data.title)
129
365
  ]);
130
366
  },
131
- upDataTree(){
132
- const unCheck = (list)=>{
133
- list.forEach((item)=>{
134
- this.$set(item, 'selected', false);
135
- if(item.children && item.children.length){
136
- unCheck(item.children)
367
+ collectCurrentCheckedNodes(nodeList) {
368
+ let checkedNodes = [];
369
+ nodeList.forEach(node => {
370
+ if (node.checked) {
371
+ // 强制补全orgUnitId,过滤无效节点
372
+ const nodeId = node.orgUnitId || node.id || '';
373
+ if (nodeId && typeof nodeId === 'string' && nodeId.trim()) {
374
+ const validNode = {
375
+ ...node,
376
+ orgUnitId: nodeId.trim(),
377
+ orgNodeName: node.orgNodeName || node.title || node.name || `未命名组织(${nodeId.trim()})`
378
+ };
379
+ checkedNodes.push(validNode);
137
380
  }
138
- })
139
- }
140
- unCheck(this.data)
381
+ }
382
+ // 递归处理子节点
383
+ const childNodes = node.children || node.orgChildrenList || [];
384
+ if (childNodes.length) {
385
+ checkedNodes = [...checkedNodes, ...this.collectCurrentCheckedNodes(childNodes)];
386
+ }
387
+ });
388
+ return checkedNodes;
389
+ },
390
+ upDataTree() {
391
+ const unCheck = (list) => {
392
+ list.forEach((item) => {
393
+ this.$set(item, 'checked', false);
394
+ });
395
+ };
396
+ unCheck(this.data);
141
397
  }
142
398
  },
143
- }
399
+ watch: {
400
+ 'treeList': {
401
+ handler(val) {
402
+ let tree = deepCopy(val);
403
+ this.initTree(tree);
404
+ this.data = tree;
405
+ },
406
+ deep: true
407
+ }
408
+ }
409
+ };
144
410
  </script>
411
+
145
412
  <style lang="less" scoped>
146
- .content{
147
- width: 100%;
148
- height: 100%;
149
- overflow: auto;
413
+ .content {
414
+ width: 100% !important;
415
+ height: 100% !important;
416
+ overflow: visible !important; /* 完全放开溢出限制 */
417
+ box-sizing: border-box;
418
+ }
419
+
420
+ /* 子组件内层容器强制宽度自适应 */
421
+ .tree-inner {
422
+ width: fit-content !important;
423
+ min-width: 100% !important;
424
+ height: 100% !important;
425
+ white-space: nowrap !important;
426
+ }
427
+
428
+ /deep/ .ivu-tree-node {
429
+ white-space: nowrap !important;
430
+ width: auto !important;
431
+ min-width: 100% !important;
432
+ box-sizing: border-box;
433
+ }
434
+
435
+ /deep/ .ivu-tree-node-content {
436
+ cursor: default !important;
437
+ pointer-events: none !important;
438
+ padding-left: 0 !important;
439
+
440
+ .ivu-checkbox, .ivu-tree-switcher {
441
+ pointer-events: auto !important;
442
+ cursor: pointer !important;
443
+ }
444
+ }
445
+
446
+ /deep/ .ivu-tree {
447
+ white-space: nowrap !important;
448
+ display: inline-block !important;
449
+ width: auto !important; /* 宽度自适应内容 */
450
+ min-width: 100% !important; /* 最小宽度占满容器 */
451
+ box-sizing: border-box;
452
+ }
453
+
454
+ /deep/ .ivu-tree-node,
455
+ /deep/ .ivu-tree-node *,
456
+ /deep/ .ivu-tree-node-content,
457
+ /deep/ .ivu-tree-title {
458
+ box-shadow: none !important;
459
+ text-shadow: none !important;
460
+ outline: none !important;
461
+ border: none !important;
462
+ background: transparent !important;
463
+ }
464
+
465
+ /deep/ .ivu-tree-node-selected,
466
+ /deep/ .ivu-tree-node-selected *,
467
+ /deep/ .ivu-tree-node-focus,
468
+ /deep/ .ivu-tree-node-focus *,
469
+ /deep/ .ivu-tree-node-hover,
470
+ /deep/ .ivu-tree-node-hover * {
471
+ box-shadow: none !important;
472
+ text-shadow: none !important;
473
+ background: transparent !important;
474
+ color: inherit !important;
475
+ }
476
+
477
+ /deep/ .ivu-tree-node-content {
478
+ padding-left: 0 !important;
150
479
  }
151
- /deep/.ivu-tree ul li{
152
- overflow: hidden;
480
+
481
+ /deep/ .ivu-tree::-webkit-scrollbar {
482
+ display: block !important;
153
483
  }
154
- /deep/.ivu-tree-title{
155
- width: calc(100% - 20px);
484
+
485
+ /deep/ .ivu-tree-indent {
486
+ margin-right: 4px;
487
+ display: inline-block !important;
156
488
  }
157
489
 
158
- </style>
490
+ /deep/ .ivu-tree-node::before,
491
+ /deep/ .ivu-tree-node::after,
492
+ /deep/ .ivu-tree-title::before,
493
+ /deep/ .ivu-tree-title::after {
494
+ box-shadow: none !important;
495
+ text-shadow: none !important;
496
+ background: none !important;
497
+ }
498
+ </style>