question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Table with dynamic columns - footer / header does not update after context change

See original GitHub issue

Bug, feature request, or proposal:

Bug

What is the expected behavior?

I define table using dynamic columns like below. In footer cell definition I am using some context object (from current component) and its values.

After context object is updated, table footer displays current data.

        <ng-container
            *ngFor="let month of months;"
            matColumnDef="p{{month}}"
        >
            <th mat-header-cell *matHeaderCellDef>
                {{month}}
            </th>
            <td
                mat-cell
                *matCellDef="let element"
            >
                        {{element.someValue}}
            </td>
            <td mat-footer-cell *matFooterCellDef>
                   {{someContextObject?.someValue}}
            </td>
        </ng-container>

What is the current behavior?

After updating context object, table footer does not show changes. It does not work even, if context variable is a simple string.

This works correctly, if table cell is not defined using *ngFor.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular Material 6.4.7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
danielsharveycommented, Apr 19, 2019

Further to the issues noted by @literalpie above, I have more workarounds for those interested.

Having noted the ‘changed’ flags as part of removeHeaderRowDef() and removeFooterRowDef() https://github.com/angular/material2/blob/21e646a3e7d99323e3f6d1e2961f416c8695bf48/src/cdk/table/table.ts#L555 https://github.com/angular/material2/blob/21e646a3e7d99323e3f6d1e2961f416c8695bf48/src/cdk/table/table.ts#L567

here are some change detection workarounds for header/footer rows:

  • header rows cannot be ngIfd --> use table.removeHeaderRowDef(null) to trigger an update to the header (or table.removeFooterRowDef(null))

See updated stackblitz - note that toggling booly refreshes table header and body rows correctly.

4reactions
TiScommented, Sep 7, 2018

Update - I have been able to make a work around for this using separate ng-template (outside of ng-container), like below. But this is absurd, especially for footer cells, which often show aggregate values (totals) from outside of table data source.

<ng-container
            *ngFor="let month of months;"
            matColumnDef="p{{month}}"
        >
            <th mat-header-cell *matHeaderCellDef>
                {{month}}
            </th>
            <td
                mat-cell
                *matCellDef="let element"
            >
                        {{element.someValue}}
            </td>
            <td mat-footer-cell *matFooterCellDef>
                   <ng-container *ngTemplateOutlet="monthFooter;context: {month: month}"></ng-container>
            </td>
</ng-container>

 <ng-template #monthFooter let-month="month">
                {{someContextObject?.someValue}}
 </ng-template>
Read more comments on GitHub >

github_iconTop Results From Across the Web

<table> with fixed <thead> and scrollable <tbody> [duplicate]
have scrollable <tbody> (scrollbar may be always visible); header and body would handle resizing properly and not mess alignment of the <thead> columns...
Read more >
Excel header and footer: how to add, change and remove
Change header or footer in Page Layout view​​ To switch to Page Layout view, go to the View tab > Workbook Views group,...
Read more >
Tables Tutorial | Web Accessibility Initiative (WAI) - W3C
Tables with irregular headers have header cells that span multiple columns and/or rows: For these tables, define column and row groups and set...
Read more >
Insert a table of contents - Microsoft Support
Add an easy to maintain Table of Contents using heading styles that automatically updates when you make changes to your headings.
Read more >
Table | Angular Material
Each column definition can contain a header-cell template ... you want rendered (e.g. if you do not need a footer row, simply do...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found