Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

md-table - How to update the column width

Tags:

angular

I have started using the md-table for my project, and I want fixed column width. Currently all columns width are divided into equal size.

Where can I get the documentation of data table dimensions?

https://material.angular.io/components/table/overview

like image 388
Vinutha Kumar Avatar asked Jul 18 '17 06:07

Vinutha Kumar


People also ask

How do you change the column width in a table?

Change column width To change the width to a specific measurement, click a cell in the column that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Column Width box, and then specify the options you want.

How do you increase the width of a table?

To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.

What are the two ways to resize a column width?

Adjusting column width two waysPlace your mouse pointer to the right side of the column header. The mouse pointer changes to the adjustment tool (double-headed arrow). Drag the edge of the column header to the desired width and release the button.


2 Answers

When Material creates the table it automagically applies two class-names for you which you can use to style each column. In the the example below the styles is named mat-column-userId and cdk-column-userId.

<ng-container cdkColumnDef="userId">   <md-header-cell *cdkHeaderCellDef> ID </md-header-cell>   <md-cell *cdkCellDef="let row"> {{row.id}} </md-cell> </ng-container> 

Now you can use those names in css:

.mat-column-userId {   flex: 0 0 100px; } 

Similar to Rahul Patil's answer, but you don't need to add another class to your column definitions.

like image 174
jymdman Avatar answered Oct 04 '22 08:10

jymdman


Right now, it has not been exposed at API level yet. However you can achieve it using something similar to this

<ng-container cdkColumnDef="userId" >   <md-header-cell *cdkHeaderCellDef [ngClass]="'customWidthClass'"> ID </md-header-cell>   <md-cell *cdkCellDef="let row" [ngClass]="'customWidthClass'"> {{row.id}} </md-cell> </ng-container> 

In css, you need to add this custom class -

.customWidthClass{    flex: 0 0 75px; } 

Feel free to enter the logic to append class or custom width in here. It will apply custom width for the column.

Since md-table uses flex, we need to give fixed width in flex manner. This simply explains -

0 = don't grow (shorthand for flex-grow)

0 = don't shrink (shorthand for flex-shrink)

75px = start at 75px (shorthand for flex-basis)

Plunkr here - https://plnkr.co/edit/v7ww6DhJ6zCaPyQhPRE8?p=preview

like image 20
Rahul Patil Avatar answered Oct 04 '22 07:10

Rahul Patil