Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add static content to my horizontal angular mat-table?

I change direction columns in the mat-table and after that I can't add ng-container to the table. I made this like this. How to add predefined row in this case with my content ?

component.ts

export class MyComponent implements OnInit {
    displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'actions'];
    dataSource = ELEMENT_DATA;
    fourthFormGroup: FormGroup;
    displayColumns: string[];
    displayData: any[];

    constructor(
        private _formBuilder: FormBuilder
    ) { }

    ngOnInit() {
        this.displayColumns = ['0'].concat(this.dataSource.map(x => x.position.toString()));
        this.displayData = this.displayedColumns.map(x => this.formatInputRow(x));
        this.fourthFormGroup = this._formBuilder.group({});
    }

    formatInputRow(row) {
        const output = {};

        output[0] = row;
        for (let i = 0; i < this.dataSource.length; ++i) {
            output[this.dataSource[i].position] = this.dataSource[i][row];
        }

        return output;
    }
}

component.html

<table mat-table [dataSource]="displayData" class="mat-elevation-z8">

    <!-- Position Column -->
    <ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
        <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
        <td mat-cell *matCellDef="let element"> {{ element[column] }} </td>
    </ng-container>

    <!-- Below Colunm doesn't show -->
    <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef>Actions </th>
        <td mat-cell *matCellDef="let element">
            <button (click)="accept($event)">Accept</button>
            <button (click)="remove($event)">Remove</button>
        </td>
    </ng-container>

    <tr mat-row *matRowDef="let row; columns: displayColumns"></tr>
</table>
like image 268
andrzej Avatar asked Aug 13 '19 05:08

andrzej


1 Answers

Adding an ng-container would be adding a new matColumnDef. You do not want a column instead you need a new row to display.

Since you are just mapping your displayColumns to become your new "horizontal" table headers. You can just add actions there to see the new row.

displayedColumns: string[] = ['position', 'name', 'weight', 'symbol', 'actions'];

Now to display your static content, we will need to modify your template. We know that the "horizontal" table header exists in element[0], we can use this with *ngIf to correctly display the new row.

<ng-container [matColumnDef]="column" *ngFor="let column of displayColumns">
    <th mat-header-cell *matHeaderCellDef> {{ column }} </th>
    <td mat-cell *matCellDef="let element">
        <span *ngIf="column == 0 || element[0] !== 'actions'; else actions">{{ element[column] }}</span>
        <ng-template #actions>
            <button (click)="accept($event)">Accept</button>
            <button (click)="remove($event)">Remove</button>
        </ng-template>
    </td>
</ng-container>

Here is a working example on StackBlitz.

like image 120
nash11 Avatar answered Oct 02 '22 14:10

nash11