i am trying to build hierarchical grid in angular2 with n-child grids.
My markup should look something like this
<grid [observable]="users$">
<column key="Username" caption="Username"></column>
<column key="Name" caption="Name"></column>
<grid property="UserGroups">
<column key="GroupNo" caption="Group-No"></column>
</grid>
</grid>
Here is my component:
@Component({
selector: 'grid',
directives: [FORM_DIRECTIVES, Column],
template: `
<table class="table table-grid">
<thead>
<tr>
<th *ngIf="childGrid"></th>
<th *ngFor="#col of columns" [attr.key]="col.ColumnKey">
{{ col.Caption }}
</th>
</tr>
</thead>
<tbody>
<template ngFor #item [ngForOf]="data">
<tr>
<td *ngIf="childGrid" (click)="setChildData(item)">
</td>
<td *ngFor="#col of columns" [attr.key]="col.ColumnKey">
{{ item[col.ColumnKey] }}
</td>
</tr>
<tr *ngIf="childGrid" class="child-container">
<td></td>
<td [attr.colspan]="columns.length">
<ng-content select="grid"></ng-content>
</td>
</tr>
</template>
</tbody>
</table>
`
})
export class Grid {
private _initialized: boolean = false;
@Input('observable') obs$: Observable<any[]>;
@Input('property') propertyName: string;
@ContentChildren(Column, false) columns: QueryList<Column>;
@ContentChildren(Grid, true) childs: QueryList<Grid>;
data: any[] = [];
get childGrid(): Grid {
if (this.childs.length == 0) return null;
return this.childs.filter(x => x != this)[0];
}
htmlNode: HTMLElement;
constructor(elementRef: ElementRef) {
this.htmlNode = <HTMLElement>elementRef.nativeElement;
}
ngOnInit() {
if (this.obs$ != null) this.obs$.subscribe(data => this.data = data);
}
setChildData(data: any) {
if (data != null && this.childGrid != null) {
var prop = this.childGrid.propertyName;
this.childGrid.data = data[prop]
}
}
}
My problem now is that the child-grid gets only rendered once. Is there a way to render it for every loop?