Fixed header in primeng DataTable

走远了吗. 提交于 2019-12-10 17:54:10

问题


I'm using PrimeNG 4.1.0-rc.2.

What I'm trying to create is datatable with fixed header. Header should be always visible even if I scroll my table to the bottom (like fixed menu on the top of stackoverflow).

I tried scrollable and scrollHeight properties of p-dataTable but there is a scroll on the table side. I don't need it because I already have one for entire page. Also I tried to fix it with position: fixed but then table headers and table contents have a different size.

Any help would be appreciated.

Now I have something like this: http://embed.plnkr.co/bnB2ZDvDPZos3JLMmVFe/ There is scrollable option turned on and position: fixed is commented out.


回答1:


I found solution, I should use position: sticky with scrollable. Here is an example: http://embed.plnkr.co/jZJ3it0ARLLYSe0zI6DL/

Maybe this will help anyone.

EDIT: Finally there is another solution. In the component:

private isScrolled: boolean = false;
constructor(private renderer: Renderer) {
    window.onscroll = () => {
        this.zone.run(() => {
             var header = document.getElementsByClassName('ui-datatable-scrollable-header')[0];
             this.isScrolled = window.pageYOffset > 35; // there is another sticky on the top in my app
             this.renderer.setElementClass(header, 'header_scrolled', this.isScrolled);
        });
    }
}

And CSS:

.header_scrolled {
    position: fixed !important;
    top: 60px; 
}



回答2:


You can also put the code bellow into your @Component

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.css'],
  styles: [`
  :host ::ng-deep .ui-table .stk {
      position: -webkit-sticky;
      position: sticky;
      top: 0px;
      z-index: 9999;
  }
  :host ::ng-deep .margTop {
    position: -webkit-sticky;
    position: sticky;
    top: 52px;
    z-index: 9999;
  }

`],

My HTML header is like this:

<tr>
                <th class="stk" rowspan="2" style="width:60px;">Field.</th>
                <th class="stk" rowspan="2" style="width:200px;">Field</th>
                <th class="stk" rowspan="2" style="width:135px;">Field</th>
                <th class="stk" rowspan="2" style="width:105px;">Field</th>
                <th class="stk" rowspan="2">Field</th>
                <th class="stk" rowspan="2" style="width:105px;">Field</th>
                <th class="stk" colspan="2"><p class="text-center">Field</p></th>
                <th class="stk" colspan="2"><p class="text-center">Field</p></th>
                <th class="stk" rowspan="2">Field</th>
                <th class="stk" rowspan="2">Field</th>
                <th class="stk" rowspan="2" style="width:60px;">Field</th>
            </tr>
            <tr>
                <th class="margTop">Field</th>
                <th class="margTop">Field</th>
                <th class="margTop">Field</th>
                <th class="margTop">Field</th>
            </tr>


来源:https://stackoverflow.com/questions/44807850/fixed-header-in-primeng-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!