I have the following column configuration for my Angular Material mat-table
columns:
export interface TableColumn {
name: string;
dataKey: string;
isSortable?: boolean;
width?: string; // column width
}
//...
this.tableColumns = [
{
name: 'Id',
dataKey: 'id',
position: 'left',
width: '50px'
},
{
name: 'Name',
dataKey: 'name',
position: 'left',
width: '100%'
}
];
As far as I see, many people use css in order to set column width as on this page, etc. However, would not it be nice if we set the column widths for each column by pixel and percentage in the this.tableColumns
array as the other properties when defining columns? I think I just need a configuration of mat-table
, but I am not sure if there is such a config for mat-table
. If not, I think I can use [style.width] and set each column by column config value. Any suggestion for that?
In general, you defined the columns based in your array like:
And your displayedColumns as
NOTE: I supose you defined the width as '50%' or '30px' if only use a number you can use, e.g.
Update imagine you want to create a column with actions buttons, but this buttons can be one, two or three. We can asign to this last colum a width of "1%" and 'white-space: nowrap'
NOTE: I'm not prety sure what happens with the width of columns because the % total is bigger that 100%
We can try using the mat-table in flex-mode
See that the column "action" the "head" repeat the buttons -with visibility:hidden-
NOTE: In this case, the "with" -really the flex- is a number, not a %,