1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How do I update/add table column headers to Angular Material table?

Discussão em 'Angular' iniciado por bootsy1974, Setembro 15, 2025.

  1. bootsy1974

    bootsy1974 Guest

    I am trying to updated the current table headers and add new column headers as well. The issue is when I do that I lose data off of the main row, not the expanded rows. For example, I want to replace 'Weight', 'Symbol', etc headers to another column header name, the data for each column disappears on the main row.

    I also want to add new column headers to the wrench icon dropdown, which adds and removes headers from the table via checkboxes.

    html

    <ng-container matColumnDef="{{toggleColDef}}">
    <th mat-header-cell *matHeaderCellDef>
    <button
    mat-button
    [matMenuTriggerFor]="menu"
    class="table-config-menu my-wrench"
    >
    <mat-icon aria-hidden="false" aria-label="Example home icon"
    >build</mat-icon
    >
    <mat-menu #menu="matMenu">
    <span class="table-config-menu-label">Edit Columns</span>
    <div
    class="table-config-menu-options"
    style="display: flex; flex-direction: column; padding: 8px"
    >
    <mat-checkbox
    *ngFor="let cd of columnDefinitions; let i = index"
    [checked]="isColumnVisible(cd.def)"
    (change)="toggleColumn(cd.def, $event.checked)"
    >
    {{cd.label}}
    </mat-checkbox>
    </div>
    </mat-menu>
    </button>
    </th>
    <td mat-cell *matCellDef="let element"></td>
    </ng-container>


    This is where I've tried to changed the column headers in multiple places in the typescript.

    ts

    export class TableExpandableRowsExample implements AfterViewInit, OnDestroy {
    columnsToDisplay: string[] = [
    'Name',
    'weight',
    'symbol',
    'position',
    'Delivery Day',
    ];

    columns: any[] = ['Name', 'weight', 'symbol', 'position', 'Delivery Day'];

    columnDefinitions = [
    { def: 'Name', label: 'Name', visible: true },
    {
    def: 'weight',
    label: 'Weight',
    visible: true,
    },
    {
    def: 'symbol',
    label: 'Symbol',
    visible: true,
    },
    {
    def: 'position',
    label: 'Position',
    visible: true,
    },
    ];


    Also if there is a better more efficient way to change and add to the headers than the way I'm doing it that would be very beneficial also.

    Stackblitz

    Continue reading...

Compartilhe esta Página