The PrimeNG table component in Angular is having trouble maintaining a fixed width

I am facing an issue with fixing the width of the first column in a primeNG table. Despite inspecting the element and confirming that it is displaying correctly, my CSS adjustments are being overridden.

Essentially, I am seeking a CSS solution where, regardless of the other column sizes, the first column will maintain a fixed width when switching tabs.

I attempted to set a fixed width for the first child, but it seems to be defaulting to 100% width instead.

https://i.sstatic.net/M1HGZ.png The image above shows that the size appears as 368 despite defining it as 100px.

Similarly, upon changing tabs, the width ends up being 920 even with the 100px fixed width setting.

https://i.sstatic.net/c9tnN.png

Is there a method to establish a fixed width of 100px while keeping the table responsive with a width of 100%?

For a reproducible example, you can visit the following link:

https://stackblitz.com/edit/primeng-tablescroll-demo-wtpbny

Answer №1

If you're using primeng 12, be aware that the colgroup template has been removed. Custom column width may not work as expected if you're still using colgroup. Check out the migration guide for more information.

link:https://github.com/primefaces/primeng/wiki/Migration-Guide

solution: Ensure that you set the same width for both <th [style]="'width':'100px'"> and <td [style]="'width':'100px'"> If you are working with dynamic columns, add a 'width' property as shown below:

[{ header: "Name", field: "name", width: '100px' }] // array of column objects

<p-table [value]="data" [columns]="ColumnObjectArray">   
<ng-template pTemplate="header" let-columns>
                <tr>
                    <th *ngFor="let col of columns; let idx=index;" class="" 
                           [style]="'width':col.width">{{col.header}}</th>
               </tr>
</ng-template>
<ng-template pTemplate="body" let-data let-columns="columns">
                <tr>
                    <td *ngFor="let col of columns" [style]="'width':col.width">
                       {{data[col.field]}}
                    </td>
                </tr>
</ng-template>
</p-table>

Give this method a try to achieve responsive table width along with custom column and header widths.

Answer №2

I have successfully implemented the necessary solution in a previous project.

To achieve the desired outcome, follow these steps:

  1. Specify the width for each of your columns
  2. Implement horizontal scrolling according to the PrimeNG documentation, specifically in the Horizontal Scroll section:

Horizontal Scroll: When using horizontal scrolling, it is crucial to assign fixed widths to columns. When customizing column widths for scrollable tables, utilize colgroup as shown below to prevent alignment issues across different sections such as headers, bodies, and footers.

Take a look at this live example which demonstrates a table with a fixed size for the first column and responsive behavior.

This solution (setting fixed column sizes) is essential because setting the width to 100% on mobile devices may result in some columns not being displayed or overlapping, thus compromising responsiveness.

Answer №3

It seems like the issue lies in your selector targeting the th element. The current selector is:

::ng-deep .p-datatable .p-datatable-tbody > tr > th:nth-child(1) {

However, it should be adjusted to use .p-datatable-thead instead of .p-datatable-tbody:

::ng-deep .p-datatable .p-datatable-thead > tr > th:nth-child(1) {

Answer №4

After some experimentation, I discovered that by applying the CSS property table-layout: auto; to the .p-datatable table class, the issue was resolved:

.p-datatable table {
  table-layout: auto;
}

With this adjustment, the table will now properly adhere to the specified width of each td

Answer №5

Fixing the issue by removing fixed widths from all table header cells.

.table-header th {
    width: auto !important;
}

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

Positioning a .png file in the center of a div while giving it a high z-index

Is it possible to ensure that a .png image is always centered in a div, regardless of the device size, and appears above another .png image within the same div? Below is the HTML code snippet: <div class="col-sm-6"> <div id="spi ...

Issues with Angular Structural Directives arising from NPM installation concerns are causing problems

I have developed an npm package called sezam-shareds To integrate the package into a new project, you need to follow these steps: Add the following component from the package: <sezam-overflow [show]="true"></sezam-overflow> to a compo ...

Add a new to-do list item to the current list using jQuery

Currently, I am in the process of developing a todo list application using jQuery. However, I have encountered some issues and bugs that are causing challenges. Initially, my objective was to set the first element through HTML code, which is functioning as ...

What is the process for generating a flexible multi-column unordered list in a single column using Bootstrap 4?

I have been attempting to design a multi-column unordered list that can be centered in relation to its heading. Despite trying various methods found online to create two columns, I have struggled to properly center it under a heading or adjust the padding ...

Assign a value to the input field based on changes made in another input field

I am brand new to the world of JavaScript and currently grappling with setting a value in an input field based on the onchange event of another input field. Here is my code sample for input field 1: <input type='text' onchange='methodTh ...

It appears that when using Python's Selenium to open Chrome, the HTML page is dynamically inserting an iframe element

Recently, I started delving into the world of selenium and web automation, attempting to develop a program to streamline paper searches on PubMed using chromedriver. My primary goal is to automate the process of clicking the "Sign in" button located in th ...

Implementing ngClass on a paragraph with Radio Buttons for dynamic styling

In my home.css file, I created two classes named 'male' and 'female'. I also added two radio buttons with values 'male' and 'female'. My goal is to change the background color of a paragraph when I select one of thes ...

When selecting an input within a div, the Angular onblur function is behaving erratically

How can I prevent the div from closing when I click on an input inside it after setting a tabindex to the div and closing it on blur? Solution for app.component.html: <button (click)="openToggle('toggle1')">Toggle 1</button> ...

Using PHP script, extract information from a JSON file and populate a dropdown menu in HTML

I am attempting to extract data from a JSON file using PHP and then display this data in an HTML select tag on the front end. Below is my PHP file: <?php ini_set('display-errors', 'on'); error_reporting(E_ALL); $executionStartTim ...

Utilizing local storage to retain column resize settings in PrimeNG

I am currently working on implementing the order, toggle, and resize (Fit Mode) features on a primeng table. So far, I have managed to update the selectedColumns array for order and toggle, allowing me to save the current user settings. My issue lies with ...

Arrangement of HTML file script and CSS styling

After watching Doug Crockford's Theory of DOM video where he discusses the positioning of <script> tags and CSS <link> at 16:50, I began to ponder a few things. He recommends placing <script src> closer to the bottom of the body and ...

I'm having issues with the functionality of my code inside the ng-template and ngIf. What could be

I am facing an issue with my method that is supposed to display the specified HTML content. However, it seems like the ngIf condition within the code block is not functioning correctly. Can someone help me understand why the ngIf statement is being ignored ...

The banner appears unusually compact when viewed on mobile devices

It's about time for me to delve into the intricacies of responsive and adaptive design, but I'm hoping there's a straightforward solution to tackle this issue. Here is my desktop web page as displayed in my browser: https://i.stack.imgur.c ...

Encountering an AWS Cognito issue while using Angular 4: Error message indicates configuration is missing a

I'm currently in the process of developing a web application that heavily relies on AWS services for its backend infrastructure. As of now, I am utilizing AWS Cognito to manage user sessions within the application. In my development work, I am using A ...

Utilizing Angular to Handle Multiple Sockets on a Single Client

Is it feasible to achieve this task? If not, kindly guide me in an alternative direction. I am managing a server that has the capability for multiple socket-io connections. The server produces a random number and transmits it to registered sockets. In th ...

The <th> and <td> elements retrieved from the database do not align properly in their respective index positions

Hey fellow Developers, I'm currently working on a School Management System and I have two tables in the database - one for Subjects and another for storing scores obtained in those subjects, called 'scores'. My goal is to display subject nam ...

Issue with retrieving properties in Angular template due to undefined values in HTML

As a newbie to Angular, I am dedicated to improving my skills in the framework. In my project, I am utilizing three essential files: a service (Studentservice.ts) that emits an observable through the ShowBeerDetails method, and then I subscribe to this ob ...

How can you determine the specific type of "display" utilized by CSS Bootstrap for a particular element?

When manipulating the "display" property of a bootstrap element like "row" or "col-md-3" using JavaScript, how can I determine the default value set by Bootstrap CSS? For instance, the Bootstrap source code likely sets the "display" value for the "row" cl ...

Linking Angular 2 Production Mode to the vendor directory

I am currently working on an Angular2 project using the angular-cli npm package. To upload the app to the server, I used the following commands: ng build --prod and ng serve --prod. However, after uploading the dist folder to the server and trying to acc ...

Accessing information from an Angular Elements Web Component using vanilla JavaScript

I am in the process of creating several WebComponents that I plan to utilize across various libraries and frameworks, with a primary focus on plain vanilla JavaScript. My current consideration is to implement Angular Elements for this purpose, and I have a ...