2

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
  • 1
    fixed-header is it any directive? Commented Jun 1, 2018 at 9:55
  • i have seen in some answer so added that but no change Commented Jun 1, 2018 at 10:00
  • i think issue is because i have added scroll for the entire div<>; but tried adding scroll for body also it didnt work Commented Jun 1, 2018 at 10:02
  • If your table body has a fixed height it's easy: thead, tbody { display: block;}tbody { overflow-y: scroll; height: 100px;} If not, the only thing I know can work is duplicate the table, one in a fixed div with a height,e.g. 1.5rem getbootstrap.com/docs/4.0/utilities/position/#fixed-top, and in a div that will see under the "fixed" Commented Jun 1, 2018 at 11:20

4 Answers 4

9

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Would appreciate if you would accept the answer too.
i have given fixed height for the table but when the result is only one then it seems like some blank space i mean the height of the table is not adjusting automatically. How to achieve that.
I have updated the fiddle to suit as per your requirement jsfiddle.net/hh6etg6s/2. And I again request you to please acknowledge the answer if you are satisfied.
Could you please help this post also : stackoverflow.com/questions/50903324/…
3

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-table with fixed header and scrollable body, we have to add sticky input to the matHeaderRowDef.

Comments

1

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:

Hide Scroll Bars

Comments

0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.