Hello,
I'm trying to dynamically add a child node to a treeview, but adding a child node to the selected dataitem does not refresh the treeview.
When I inspect the dataitem that I add a child node to, it has that child node.
Interestingly remove works fine.
I created a stackblitz, so that you can see the problem I'm having.
https://stackblitz.com/edit/angular-vqmryk?file=app%2Fapp.component.ts
Click the buttons I highlighted in the screenshot, and this will call onAddGroupClick() or onAddClick() where I'm adding a child node.
How can I make it so that the treeview understands that a node is added?
Below is the code.
import { Component } from "@angular/core";
@Component({
selector: "my-app",
template: `
<
kendo-treeview
[nodes]="instructionJson.items"
textField
=
"text"
kendoTreeViewExpandable
kendoTreeViewHierarchyBinding
childrenField
=
"items"
[isExpanded]="isExpanded"
>
<
ng-template
kendoTreeViewNodeTemplate let-dataItem>
<
ng-container
*
ngIf
=
"dataItem.isHeader"
>
<
kendo-buttongroup
selection
=
"single"
>
<
button
kendoButton
[toggleable]="true"
(selectedChange)="
onOperatorSelectionChange($event, 'And', dataItem)
"
[primary]="dataItem.operator == 'And'"
>
And
</
button
>
<
button
kendoButton
[toggleable]="true"
(selectedChange)="
onOperatorSelectionChange($event, 'Or', dataItem)
"
[primary]="dataItem.operator == 'Or'"
>
Or
</
button
>
</
kendo-buttongroup
>
<
kendo-buttongroup
>
<
button
kendoButton
icon
=
"k-icon k-i-filter-add-expression"
(click)="onAddGroupClick(dataItem)"
></
button
>
<
button
kendoButton
icon
=
"k-icon k-i-filter-add-group"
(click)="onAddClick(dataItem)"
></
button
>
</
kendo-buttongroup
>
<
kendo-buttongroup
>
<
button
kendoButton
icon
=
"k-icon k-i-close"
(click)="onDeleteClick(dataItem)"
></
button
>
</
kendo-buttongroup
>
</
ng-container
>
<
ng-container
*
ngIf
=
"!dataItem.isHeader"
>
<
div
>
<
button
kendoButton
icon
=
"k-icon k-i-close"
(click)="onDeleteClick(dataItem)"
></
button
>
</
div
>
</
ng-container
>
</
ng-template
>
</
kendo-treeview
>
`
})
export class AppComponent {
public instructionJson: any = {
triggerType: null,
url: null,
items: [
{
isHeader: true,
operator: "And",
items: [
{
isHeader: true,
operator: "Or",
items: []
},
{
isHeader: false,
items: []
},
{
isHeader: false,
items: []
}
]
}
]
};
public isExpanded = (dataItem: any, index: string) => {
return true;
};
public onOperatorSelectionChange(event, operator: string, dataItem: any) {
dataItem.operator = operator;
}
public onDeleteClick(dataItem: any) {
let parentDataItem = this.getParentDataItem(this.instructionJson, dataItem);
if (parentDataItem) {
if (dataItem.isHeader == true) parentDataItem.items = [];
else this.remove(parentDataItem, dataItem);
}
}
private getParentDataItem(currentParentDataItem, dataItemToFind) {
if (
currentParentDataItem &&
currentParentDataItem.items &&
currentParentDataItem.items.length > 0
) {
for (let childItem of currentParentDataItem.items) {
if (childItem == dataItemToFind) {
return currentParentDataItem;
} else {
let parentDataItem = this.getParentDataItem(
childItem,
dataItemToFind
);
if (parentDataItem != null) return parentDataItem;
}
}
}
return null;
}
private remove(parentDataItem, dataItemToDelete) {
const index = parentDataItem.items.indexOf(dataItemToDelete);
if (index > -1) {
parentDataItem.items.splice(index, 1);
}
}
public onAddGroupClick(dataItem: any) {
dataItem.items.push({
isHeader: true,
operator: "And",
items: []
});
}
public onAddClick(dataItem: any) {
dataItem.items.push({
isHeader: false,
items: []
});
}
}