Making Mat-Tab Table Headers Sticky in Angular

I'm facing an issue in my application where I have a screen with 3 tabs. One of these tabs contains a table with a large number of rows, and I want to make the headers of this table sticky so that they remain visible when the user scrolls down. Despite trying the method below, I haven't been successful in making the headers sticky. Can someone guide me on how to achieve this?

Main HTML:

    <mat-tab-group class="w-100-p" fxFill style="height:100vh;">
        <mat-tab>
            <ng-template mat-tab-label>
                <mat-icon class="mr-8">shopping_basket</mat-icon>
                Plan Çalışma Parametreleri
            </ng-template>
            <tab-material-plan-items [MaterialPlan]="materialPlan"
                [(MaterialPlanParameters)]="materialPlan.MaterialPlanParameters"
                [MaterialPlanId]="materialPlan.MaterialPlanId"></tab-material-plan-items>
        </mat-tab>
        <mat-tab>
            <ng-template mat-tab-label>
                <mat-icon class="mr-8">shopping_basket</mat-icon>
                Ürün/Reçete Bazlı Sonuçlar
            </ng-template>
            <tab-material-plan-receipt-result [MaterialPlan]="materialPlan"
                [(MaterialPlanReceiptResult)]="materialPlan.MaterialPlanReceiptResults"
                [MaterialPlanReceiptResultId]="materialPlan.MaterialPlanId"></tab-material-plan-receipt-result>
        </mat-tab>
    </mat-tab-group>

Tab HTML:

<form class="w-100-p" #workItemForm>
        <table mat-table matSort #table [dataSource]="dataSource" class="w-100-p" style="overflow: auto;">
            <ng-container matColumnDef="TotalOrderedQuantity">
                <th mat-header-cell *matHeaderCellDef mat-sort-header [ngStyle]="{'color': 'black'}" class="sticky" sticky>
                    <b>
                        Sipariş Edilen Miktar
                    </b>
                </th>
                <td mat-cell *matCellDef="let row; let i = index"
                    [ngStyle]="{'color':  row.TotalRequiredQuantity == 0 ? '#046307' : ' #E41B17'}">
                    <div>
                        {{row.TotalOrderedQuantity | number}}
                    </div>
                </td>
            </ng-container>
            <ng-container matColumnDef="TotalRequiredQuantity">
                <th mat-header-cell *matHeaderCellDef mat-sort-header [ngStyle]="{'color': 'black'}" class="sticky" sticky>
                    <b>
                        Planlanan İhtiyaç Miktarı
                    </b>
                </th>
                <td mat-cell *matCellDef="let row; let i = index"
                    [ngStyle]="{'color':  row.TotalRequiredQuantity == 0 ? '#046307' : ' #E41B17'}">
                    <div>
                        <b>
                            {{row.TotalRequiredQuantity | number}}
                        </b>
                    </div>
                </td>
            </ng-container>
            <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true" class="sticky"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns; let i = index"></tr>
        </table>
    </form>
    <mat-paginator [pageSizeOptions]="[10, 20, 50, 100]" class="sticky"></mat-paginator>

CSS:

.sticky {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
  }

Answer №1

Consider removing any sticky directives and styles, and only keep the row

<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
with the sticky as you have done.

Check out this StackBlitz sample that I found helpful for working with stickiness. It might serve as a useful reference for you -

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the [src] attribute of an image dynamically

I have a component that contains a list of records. export class HomeComponent implements OnInit { public wonders: WonderModel[] = []; constructor(private ms: ModelService){ ms.wonderService.getWonders(); this.wonders = ms.wonder ...

What is the best way to include a padding at the top of the second row?

I have two rows with three columns each. I am trying to create some spacing between the two rows. I attempted to achieve this by wrapping the rows and adding padding to the last child (which should be the second row). However, this approach did not work a ...

The prop type `cellHeight` provided to `GridList` in (Material-ui / React) is invalid

warning.js:33 Warning: The prop type for cellHeight in the GridList component is invalid. I encountered this error message, despite the property functioning correctly. Is there a way to resolve this issue? If you're interested, check out the documen ...

Developing bespoke styles in Angular Material 2

I am in the process of developing a unique theme for my Angular 2 application, incorporating various components from angular material 2. Despite searching extensively online, I haven't been able to find much relevant information. The only documentati ...

Enhancing Your Website: Introducing a Second Language with CSS and HTML

Is it possible to incorporate a second language onto a website using only HTML and CSS? I currently have the English version coded in HTML/CSS and now also require the German translation. ...

Tips on customizing text input in jQuery editable

I have been tasked with customizing the Text Input within my Table using jQuery editable. You can access the Table here. When you navigate to the page and click on Wordpress Design, you are able to edit the text within the Text Input. However, the issue a ...

a Bootstrap 4 element lacking any line-styling

Is it possible to create a link that appears as plain text with no underline, using minimal css? The issue at hand is that the <a> tag defaults to a blue color and adds an underline on hover, making it look like a typical link. I would like it to re ...

A collection of items displayed in an unordered format across two columns

I need help creating a list that displays in two columns. One column for odd numbers, the other for even numbers. Here's an example of what I'm trying to achieve: +----------------------------------------------------+ | col- ...

Guide to creating an endless loop animation within a frame

<div className="sound-wave-container py-8"> <img src={!recording ? Wave : DottedLine} className={!recording ? "sound-wave" : ""} alt="Sound Wave" /> </div> ...

Encountering difficulties accessing functions from apollo-server-express

I have been following a tutorial and attempting to launch the node server, but I am unable to import these functions from the Apollo package const {graphqlExpress, graphiqlExpress} = require('apollo-server-express'); // importing functions here ...

How to return the same value as the input value in Vue before the action is completed

When creating a component and module for functionality X while using Vuex for state management, the code initially works fine. However, after the first insertion, the Getter function consistently returns the previous input value before the action is commit ...

Transmitting JSON information using post method through .ajax(), as well as receiving a JSON reply

Seeking assistance with debugging a registration page I am currently coding. I have hit a roadblock for the past 48 hours and would greatly appreciate any help in resolving the issues. CHALLENGE I am utilizing JavaScript to validate form inputs for error ...

Create a video file using binary data obtained from a socket connection

I have successfully created a socket connection and am sending binary stream data to the server. On the server side, I am receiving the binary data and now I want to use this data to create a video file and save it on the server. However, I am struggling t ...

Using PHP and an HTML form to insert data into a MySQL database

I'm having an issue with inserting data into a MySQL database using PHP and an HTML form. After hitting submit, I receive a message stating that the item was inserted successfully. However, when I check phpMyAdmin, the table appears to be empty. The ...

Ways to tally elements that have been dynamically loaded onto the webpage through AJAX requests

My page has a dynamic region that is continuously updated using jQuery's .ajax and .load methods. In order to submit the form, there need to be at least 3 of these 'regions', so I have to keep track of the number of DIVs with a specific cla ...

Is there a way to modify the default lite-server port in the Angular quickstart setup?

I recently obtained the Angular 4 quickstart app and decided to modify the default lite-server port by including "port": 8000 in bs-config.json like this: { "server": { "port": 8000, "baseDir": "src", "routes": { "/node_modules": "node ...

What is the best way to permit anonymous post requests while restricting unauthorized get requests in Django?

While working in C#, it's easy to add [AllowAnonymous] to a request for a specific method. However, when it comes to Django, I find myself a bit perplexed with Views, Serializers, and so on. Is my understanding correct that a ViewSet allows you to ac ...

What is the best method for sending a DbGeography parameter to MVC?

I am working on developing an API that allows users to save polygons on the server using ASP.NET MVC 5. Can anyone guide me on how to properly format the AJAX parameters for posting requests with DbGeography? This is what I have tried so far: $.ajax({ ...

Can anyone recommend any JavaScript or jQuery thumbnail scripts that are similar to TimThimb (PHP)?

TimThumb is a versatile tool that can quickly generate thumbnails from images of any size or dimensions to fit any desired size. It has the ability to resize, crop, or zoom crop images without any hassle. I've been on the lookout for JavaScript alter ...

What is the proper way to specify the background image path in CSS?

My CSS file is located at: Project/Web/Support/Styles/file.css The image I want to use is found here: Project/Web/images/image.png I am attempting to include this image in my CSS file. I have tried the following methods: 1) background-image: url(/images ...