In Angular i have a table and it should be scrollable. But header should be fixed. but while scrolling header is not fixed. please help
4 Answers
Well, the requirement seems to be you want a scrollable table body. Not sure if angular could makes things different anyhow in this case. Just to be sure, please see the fiddle at https://jsfiddle.net/hh6etg6s/ if this serves the purpose.
.tablediv {
padding:20px;
}
table ,tr td{
border:1px solid red
}
tbody {
display:block;
height:200px;
overflow:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;/* even columns width , fix width of table too*/
}
thead {
width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
width:400px;
}
4 Comments
Check out this post from angularjswiki: https://www.angularjswiki.com/material/mat-table-fixed-sticky-header/
Taken from there:
In Angular, To create a
mat-tablewith fixed header and scrollable body, we have to addstickyinput to thematHeaderRowDef.
Comments
Have two instances of your table in your HTML in separate divs. In the top container, hide your body. In your bottom container hide your headers. Do this with visibility: collapse;
If you need to hide the scrollbars:
Comments
In addition to charu solution, I wanted the size to be dynamic, and not a fixed height size...
So I added this:
table {
border-collapse: separate;
border-spacing: 0;
display: grid;
grid-template-rows: auto 1fr auto;
height: 100%;
overflow-y: auto;
}
tbody {
display: block;
overflow-y: auto;
height: 100%;
}
thead, tbody tr {
display: table;
width: 100%;
table-layout: fixed;
}
fixed-headeris it any directive?