diff --git a/src/tree/test/tree.spec.ts b/src/tree/test/tree.spec.ts index ea2e68cba..91ba2a243 100644 --- a/src/tree/test/tree.spec.ts +++ b/src/tree/test/tree.spec.ts @@ -54,6 +54,11 @@ describe('ThyTreeComponent', () => { expect(treeElement.querySelector('.thy-tree-expand-icon').classList).toContain('thy-icon-plus-square'); }); + it('test set selectedKeys correctly', () => { + expect(treeComponent.getSelectedNodes().length).toEqual(1); + expect(treeComponent.getSelectedNode().title).toEqual('未分配部门'); + }); + it(`test public function 'getRootNodes()`, () => { expect(treeComponent.getRootNodes().length).toEqual(2); }); @@ -66,7 +71,6 @@ describe('ThyTreeComponent', () => { }); it(`test public function 'getSelectedNodes()`, () => { - expect(treeComponent.getSelectedNodes().length).toEqual(0); treeComponent.selectTreeNode(treeComponent.getRootNodes()[1]); fixture.detectChanges(); expect(treeComponent.getSelectedNodes().length).toEqual(1); @@ -158,6 +162,7 @@ describe('ThyTreeComponent', () => { [thyDraggable]="options.draggable" [thyCheckable]="options.checkable" [thyMultiple]="options.multiple" + [thySelectedKeys]="['000000000000000000000000']" [thyShowExpand]="true" [thyBeforeDragDrop]="beforeDragDrop" (thyOnDragDrop)="onEvent()" diff --git a/src/tree/tree-node.component.ts b/src/tree/tree-node.component.ts index 6440001ff..9b67044d9 100644 --- a/src/tree/tree-node.component.ts +++ b/src/tree/tree-node.component.ts @@ -99,7 +99,11 @@ export class ThyTreeNodeComponent implements OnDestroy { } public clickNode(event: Event) { - this.root.toggleTreeNode(this.node); + if (!this.root.thyMultiple) { + this.root.selectTreeNode(this.node); + } else { + this.root.toggleTreeNode(this.node); + } this.thyOnClick.emit({ eventName: 'click', event: event, diff --git a/src/tree/tree.component.ts b/src/tree/tree.component.ts index 3377620ce..56c5b1d0b 100644 --- a/src/tree/tree.component.ts +++ b/src/tree/tree.component.ts @@ -102,6 +102,8 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges @Input() thyTitleTruncate = true; + @Input() thySelectedKeys: string[]; + @Input() thyBeforeDragStart: (e: ThyDragStartEvent) => boolean; @Input() thyBeforeDragDrop: (e: ThyDragDropEvent) => boolean; @@ -169,6 +171,7 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges this._setTreeType(); this._setTreeSize(); this._instanceSelectionModel(); + this._setDefaultSelectedKeys(); } private _setTreeType() { @@ -187,6 +190,12 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges this._selectionModel = new SelectionModel<any>(this.thyMultiple); } + private _setDefaultSelectedKeys() { + (this.thySelectedKeys || []).forEach(key => { + this.selectTreeNode(this.thyTreeService.getTreeNode(key)); + }); + } + public isSelected(node: ThyTreeNode) { return this._selectionModel.isSelected(node); }