ngx treeview选择一位家长下的所有孩子

时间:2019-02-28 08:04:05

标签: angular ngx-treeview

在Angular 5应用程序中,我在下拉列表中使用ngx treeview。我想添加一个图标来选择一个特定父母下的孩子。

<ngx-treeview [config]="config" (selectedChange)="selectItem()" [items]="Items"></ngx-treeview>

预先感谢

1 个答案:

答案 0 :(得分:1)

在模板中,您可以使用' itemTemplate '

<ngx-treeview [config]="config" (selectedChange)="selectItem()" [items]="Items" [itemTemplate]="itemTemplate">

在模板下方的下方添加 ng-template

<ng-template #itemTemplate let-item="item" let-onCollapseExpand="onCollapseExpand" let-onCheckedChange="onCheckedChange">

<div class="form-inline row-item">

    <i *ngIf="item.children" (click)="onCollapseExpand()" aria-hidden="true" class="fa" [class.fa-caret-right]="item.collapsed" class.fa-caret-down]="!item.collapsed"></i>

    <div class="form-check">

        <input type="checkbox" class="form-check-input" [(ngModel)]="item.checked" (ngModelChange)="onCheckedChange()" [disabled]="item.disabled" [indeterminate]="item.indeterminate" />

        <label class="form-check-label" (click)="item.checked = !item.checked; onCheckedChange()"> {{item.title}} </label>

        <label class="form-check-label">

            <section *ngIf="item.children">
                <img class="icon icon-w tree-view-all" src="assets/img/checked.svg" *ngIf="item.checked && item.internalChecked" (click)="selectChildren(item)" />
                <img class="icon icon-w tree-view-all" src="assets/img/unchecked.svg" *ngIf="!item.checked || !item.internalChecked" (click)="selectChildren(item)" />
            </section>

        </label>

    </div>

</div>

在组件中使用递归函数

import { TreeviewComponent } from 'ngx-treeview';

...

@ViewChild(TreeviewComponent) treeviewComponent: TreeviewComponent;

...

selectChildren(i: TreeviewItem) {

    i.collapsed = false;

    if (i.children) {

        this.selectInsideChildren(i);

        i.checked = !i.checked;
    }

    this.treeviewComponent.raiseSelectedChange();

}



selectInsideChildren(item) {

    item.children.forEach(i => {

        i.checked = !i.checked;

        if (i.children) {

            this.selectInsideChildren(i);

        }

    });
}