我有选项的树选择。当我单击Name时,它会显示名称,但我希望它是Item > Name,因为这是子节点。我该怎么办?我找到了nzDisplayWith,但没什么用。
const options = [{
{
title: 'Item',
key: 'Item',
children: [
{
title: 'Name',
key: 'Item-Name',
isLeaf: true,
}
]
},
}]
<nz-tree-select
style="width: 100%"
[nzNodes]="options"
[nzDefaultExpandAll]="true"
aria-autocomplete="none"
nzVirtualHeight="240px"
formControlName="field"
></nz-tree-select>
发布于 2022-09-05 10:48:39
我使用一个recursive
函数来生成基于父级的标题,我使用了nzTreeTemplate
来实现这一点。
html
<nz-tree-select
#tree
class="tree-select"
nzNoAnimation
nzShowExpand
[nzMaxTagCount]="2"
[nzNodes]="nodes"
[nzHideUnMatched]="true"
nzVirtualHeight="498px"
[nzShowSearch]="true"
nzCheckable
nzPlaceHolder="Please select"
>
<ng-template #nzTreeTemplate let-node let-origin="origin">
{{ recursive(node) }}
</ng-template>
</nz-tree-select>
ts
import {
AfterContentInit,
AfterViewInit,
Component,
ViewChild,
} from '@angular/core';
import { NzTreeSelectComponent } from 'ng-zorro-antd/tree-select';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent implements AfterViewInit, AfterContentInit {
@ViewChild(NzTreeSelectComponent, { static: true })
tree: NzTreeSelectComponent;
nodes = [
{
title: 'Item',
key: 'Item',
children: [
{
title: 'Name',
key: 'Item-Name',
isLeaf: true,
},
],
},
];
dropDownVisibleChange(open: boolean): void {
if (open) {
setTimeout(() => this.tree.openDropdown());
} else {
this.tree.closeDropDown();
}
}
ngAfterViewInit(): void {
// console.log(this.tree);
}
test(node) {
console.log(node);
}
ngAfterContentInit(): void {
// console.log(this.tree);
}
recursive(node, output = '') {
if (node.isLeaf && node.parentNode) {
return this.recursive(node.parentNode, output) + ` > ${node.title}`;
} else {
return node.title;
}
}
}
https://stackoverflow.com/questions/73607965
复制相似问题