I'm trying to make a table with sub-columns from an array of column object that can itself contain sub-columns.
The header object :
export class ListHeader {
id: string;
label: string;
type: int;
subColumns?: ListHeader[];
}
Table component partial code
columns: ListHeader[];
columnsWithSubColumns: ListHeader[];
constructor(){
this.columns = [{
id: "AC1",
label: "AC1",
type: 1
},
{
id: "AC3",
label: "AC3",
type: 1
},
{
id: "AC2",
label: "AC2",
type: 1,
subColumns: [
{
id: "ColB",
label: "ColB",
type: 1
},
{
id: "ColA",
label: "ColA",
type: 1
},
{
id: "ColC",
label: "ColC",
type: 1
}
]
}
];
this.columnsWithSubColumns = this.columns
.filter(c => typeof c.subColumns !== 'undefined' && c.subColumns !== null && c.subColumns.length > 1);
}
The HTML :
<table>
<thead>
<tr>
<th *ngFor='let key of columns' [id]='key.id'>
{{ key.label }}
</th>
</tr>
<tr>
<th *ngFor='let key of columnsWithSubColumns '>
<--! Can't figure how to do from there -->
</th>
</tr>
</thead>
<--! tbody, etc... -->
</table>
I tied multiple solution, by modifying the HTML and/or the underlying Typescript, but I'm unable to find a way to show correctly the sub-columns. Either they shows under the wrong column, or don't shows at all.
I've found a working static table that does wore or less what I want (http://jsfiddle.net/8qycxkv9/), but I'm looking to make it dynamics.
As such, I'm trying to create a second array of ListHeader, from all subHeaders arrays in the columns Array. I managed to filter out the columns without subcolumns, but can't find a way to go further.
I'm open to any implementation, the only constrain is keeping as it the ListHeader object.