components icon indicating copy to clipboard operation
components copied to clipboard

[Table] Add input for sticky headers

Open andrewseguin opened this issue 9 years ago • 24 comments

Add an input to <cdk-table> that makes the table's header stick to the top of the scroll container.

Interim solution: Use styles position: sticky; top: 0 on the header element.

andrewseguin avatar Jul 19 '17 21:07 andrewseguin

Using sticky position, interim solution has some bugs when you navigate through pages in a table, if you're not at the top of the table when you change the page, header disappear, any solution ?

ghost avatar Aug 24 '17 06:08 ghost

sticky position is not supported in IE. When will we see a cross-browser solution?

jscharett avatar Sep 06 '17 14:09 jscharett

Hello @andrewseguin, any solution to the problem related to the interim solution?

ghost avatar Oct 02 '17 15:10 ghost

You can use position: sticky; top: 0 on the header row for now. Unfortunately this isn't a valid solution for IE but we have an implementation for this that we'd like to introduce soon.

andrewseguin avatar Oct 02 '17 15:10 andrewseguin

It's not really working on Firefox too, and in Chrome navigating or changing items per page cause bug. I know this feature is not urgent but have you an idea of the delivery date of this?

ghost avatar Oct 02 '17 15:10 ghost

Support for position: sticky isn't great across browsers.

oliverschenk avatar Oct 04 '17 05:10 oliverschenk

For now, here's a fun solution. Separate your table into two - the first won't show any rows, and the second won't display the header. Make the overflow scroll only on the rows.

If you set the same column IDs, you can re-use and existing column-specific class styles you might have applied (e.g. mat-column-name { min-width: 60px; }) - it will apply to both tables.

Proof-of-concept: https://plnkr.co/edit/Gc3zVWr1GXqaJdK2S8mQ?p=preview

Just realized this looks great on a mac, which overlays scrollbars rather than pushes content to show them. With this, scrollbars will push the rows left while the header is static, causing mis-alignment.

andrewseguin avatar Oct 06 '17 19:10 andrewseguin

@andrewseguin any suggestions on how to support horizontal + vertical scroll with sticky headers? So far I either get one or the other working properly. Here's the plunkr https://plnkr.co/edit/CXN6rNz3Kb9gDHKCceJ9?p=preview

rhythmnewt avatar Oct 10 '17 14:10 rhythmnewt

@andrewseguin While this works visually, I believe it has some accessibility issues. A screen reader is likely going to announce 2 tables, of which the second has hidden headers, so its not clear how a screen reader would handle it.

jscharett avatar Oct 11 '17 01:10 jscharett

Hi all and @jscharett , I write a directive to auto set max height for multiple resolutions from ideal of @andrewseguin . Hope it can help everyone. Thanks.

import { Directive, AfterViewInit, OnDestroy } from "@angular/core";
/**
 * floating table header when user scroll table
 */
@Directive({ selector: "[autFloatingTableHeader]" })
export class FloatingTableHeaderDirective implements AfterViewInit, OnDestroy {
  private tableHasContentElement: HTMLElement;
  private targetElement: HTMLElement;

  ngAfterViewInit(): void {
    this.tableHasContentElement= document.getElementById("list-content") as HTMLElement;
    this.targetElement= document.getElementsByTagName("footer").item(0) as HTMLElement;
    this.resetTableHeight();
    window.addEventListener("resize", this.resetTableHeight.bind(this));
  }

  ngOnDestroy(): void {
    window.removeEventListener("resize");
  }

    private resetTableHeight(): void {
      setTimeout(() => {
        if(this.targetElement&& this.tableHasContentElement) {
          let distance = this.targetElement.getBoundingClientRect().top - this.tableHasContentElement.getBoundingClientRect().top;
          this.tableHasContentElement.style.maxHeight = `${Math.round(distance)}px`;
        }
      }, 0);
    };
}

tamvo94 avatar Oct 13 '17 06:10 tamvo94

Support for sticky headers would be a really great feature! Really waiting for it :)

Maximaximum avatar Nov 24 '17 08:11 Maximaximum

I would love this feature as well !!!

FJBDev avatar Dec 14 '17 19:12 FJBDev

I had to do position: sticky; top: -0.5px; background: white; for iOS retina (maybe Android too). It was showing a single device pixel gap above the header which showed the data underneath as you scroll.

simeyla avatar Jan 06 '18 05:01 simeyla

@simeyla can you share code? Where did you add position: sticky; top: -0.5px; background: white;

pantonis avatar Jan 18 '18 07:01 pantonis

@pantonis I did this in my styles.css

You may need to look at the styles on the parent container if this doesn't work (eg. display or flex settings) - but this works quite nicely for me until there's an official update.

/* Table header row */
mat-header-row {
    /*https://github.com/angular/material2/issues/5885*/
    position: sticky;
    top: -.5px;
    background: white;
    z-index: 100;
}

simeyla avatar Jan 18 '18 20:01 simeyla

@simeyla Can you share how you reproduced the issue? I'm seeing this show up on Chrome dev tools when I enter responsive mode, but I can't seem to get a real world example on my Android phone and mac laptop

andrewseguin avatar Jan 29 '18 23:01 andrewseguin

Got it reproduced using this example https://stackblitz.com/edit/angular-s1t3nz?file=app/app.component.ts

Seeing it on mobile chrome with desktop view showed an issue: https://angular-s1t3nz.stackblitz.io/

andrewseguin avatar Jan 30 '18 00:01 andrewseguin

@andrewseguin What I posted was working quite well for me, so I was happy with this and wasn't really reporting an issue. But I'm only testing in latest Chrome at this time. If by issue you mean why did I have to use half a pixel it was on my iPad and showing through underneath the header as I scrolled.

simeyla avatar Jan 30 '18 23:01 simeyla

@simeyla , beautiful. works like a charm . thumbs up

my css now has these two elements

mat-table {
    height: calc(100vh - 30px);
    overflow: auto;
}

mat-header-row {
    position: sticky;
    top: -.5px;
    z-index: 100;
}

erickyi2006 avatar Jan 31 '18 07:01 erickyi2006

@andrewseguin any update on sticky for mat header row.

I tried the stickybits which works across browsers but doesn't seem to work for column headers. Here's the Stackblitz .

kal93 avatar Apr 25 '18 09:04 kal93