1

How do we add a horizontal scroll bar to a mat table with angular table sticky header ?

enter image description here

#css code

#table {
  width: 100%;
}
#table tr.mat-row{
  height: 40px;
}

#html code

Diya Agastya
  • 59
  • 1
  • 8
  • Does this answer your question? [Floating horizontal scrollbar with mat-table in Angular](https://stackoverflow.com/questions/55334935/floating-horizontal-scrollbar-with-mat-table-in-angular) – rofrol Apr 25 '23 at 10:05

1 Answers1

1

Please take a look at this post : Mat Table with sticky header and Horizontal ,vertical scroll bar

You can set the width and height to the div container wrap around the mat-table. On stylesheet, you can apply something like this:

.example-container {
  width: 100%;
  height: calc(100vh - 32px);
  overflow: auto;
}

Example with horizontal and vertical scroll: https://stackblitz.com/edit/angular-hdg9xh-uxkaeh

To remove vertical scroll adjust the .example-container css class to be

.example-container {
  width: 100%;
  height: calc(100vh - 32px);
  overflow-x: auto;
  overflow-y: hidden !important;
}

Example : https://stackblitz.com/edit/angular-hdg9xh-3xyt8z?file=src%2Fapp%2Ftable-sticky-columns-example.css

If you want to scroll through data just horizontally you would want the first column to act as the table header.

Example enter image description here

<table>
  <tr>
    <th>Date</th>
    <td>12 February</td>
    <td>24 March</td>
    <td>14 April</td>
  </tr>
  <tr>
    <th>Event</th>
    <td>Waltz with Strauss</td>
    <td>The Obelisks</td>
    <td>The What</td>
  </tr>
  <tr>
    <th>Venue</th>
    <td>Main Hall</td>
    <td>West Wing</td>
    <td>Main Hall</td>
  </tr>
</table>
Steve020
  • 125
  • 1
  • 10
  • How do we remove the vertical scroll ? I only need the horizontal scroll. – Diya Agastya Jun 15 '22 at 01:30
  • I added to my answer on how you can just do the horizontal scroll. Your x axis is your horizontal and your y axis is vertical. – Steve020 Jun 15 '22 at 13:49
  • my goal Sir is to add a horizontal scroll if there are already multiple columns without the vertical inner scroll and the table header columns should be sticky without the vertical inner scroll and just by using the window scroll. – Diya Agastya Jun 15 '22 at 14:04
  • the problem with your ssecond example Sir @Steve is that the sticky is not working without the inner vertical scroll , it should work using the window scroll – Diya Agastya Jun 15 '22 at 14:06
  • how does the sticky work using the window Scroll ? ..that is my goal Sir – Diya Agastya Jun 15 '22 at 14:09
  • Is there a reason you just want to be able to scroll horizontally. Most apps do not like to use horizontal scrolling moving a mouse left to right or your finger left to right on a touch screen isn't done nearly as much as an up and down motion. And could effect your overall user experience. – Steve020 Jun 15 '22 at 14:32
  • yes Sir , I need the horizontal scroll since when the screen is small and there are like 15 table headers then it will just be scrollable , my issue is , horizontal scroll with sticky does not work. – Diya Agastya Jun 15 '22 at 14:39
  • If I scroll down or scroll up using the window scroll the table headers should be sticky. – Diya Agastya Jun 15 '22 at 14:40
  • https://imgur.com/a/ZpAHFm4 , these should be sticky when I scroll vertically up or down using the window scroll. – Diya Agastya Jun 15 '22 at 14:41
  • what I want to be sticky header Sir is the table headers at the top and not on the left side. – Diya Agastya Jun 15 '22 at 14:44
  • are you looking for something like this ? https://stackblitz.com/edit/angular-horizontal-alignment?file=src%2Fapp%2Fapp.component.scss if not what you are trying to do is not possible – Steve020 Jun 15 '22 at 14:49
  • you could also look into data tables .net it is a very very popular jquery plugin. i have used it in multiple angular projects it is alot easier and quicker and especially because you don't have to do all your filtering logic from scratch and it is also free https://datatables.net/ – Steve020 Jun 15 '22 at 14:51
  • Something like this Sir https://stackblitz.com/edit/angular-horizontal-alignment-more-data-content?file=src%2Fapp%2Fapp.component.scss . But instead of inner vertical scroll , it should be window vertical scroll .4 – Diya Agastya Jun 15 '22 at 15:03
  • the table column headers should be sticky but using window scroll – Diya Agastya Jun 15 '22 at 15:05
  • why would it wont match Sir? – Diya Agastya Jun 15 '22 at 15:14
  • Ok the problem is you said you wanted the vertical scroll removed. You have to have the vertical scroll to the table to get what you want done you won't be able to use the window vertical scroll. If you go to the end of this video this is what you are looking for https://youtu.be/mD1Uipb5cc4 and i just seen your example on stackblitz – Steve020 Jun 15 '22 at 15:17
  • here is the datables net plugin example if you want to try that https://datatables.net/examples/basic_init/scroll_xy.html – Steve020 Jun 15 '22 at 15:19
  • Just make a mock up from the tutorial video ... i'm not going to do all the work for you lol – Steve020 Jun 15 '22 at 15:23
  • are both scroll in the video inner scroll ? or are those window scroll ? – Diya Agastya Jun 15 '22 at 15:28
  • Scroll is inner "to the element itself" Chrome , FireFox and safari do not have a default horizontal scroll bar. Chrome may have the feature on windows to be enabled not 100% certain but it would not be the norm to use it. So you will have to use the element scrolling for the table if you want to use it the way you explained. Other wise if you just want to have a sticky header and use the browser window vertical scroll without the horizontal ability that can be done easily. – Steve020 Jun 15 '22 at 15:50
  • I get you are trying to use the browsers built in scroll bars to act like what the inner scrolls would do. It might be possible with chrome but how would you enforce all your users to enable the horizontal scroll bar for chrome it self if that's even possible ? i would stay away from doing that. Also if you have alot of records like at least more than 20 you could look into adding pagination to your table. – Steve020 Jun 15 '22 at 15:54