components icon indicating copy to clipboard operation
components copied to clipboard

data-table align header cell to right

Open MaickelHubner opened this issue 9 years ago • 24 comments

Bug, feature request, or proposal:

For columns as currency, it is interesting to align the column header to the right

What is the expected behavior?

A parameter to set header alignment

What is the current behavior?

All header cells are aligned to the left

header-cell-right

MaickelHubner avatar Jul 14 '17 14:07 MaickelHubner

Why do you want this to be a parameter?

donroyco avatar Jul 14 '17 15:07 donroyco

@donroyco How else could I do that? I do not have an ID class like with the columns to use in CSS ...

MaickelHubner avatar Jul 14 '17 16:07 MaickelHubner

Each column is given unique class that identifies it by name. For example, if your column name is user_id, then the column's class will be cdk-column-userName (and mat-column-user_id if using the md-table).

andrewseguin avatar Jul 15 '17 02:07 andrewseguin

@andrewseguin The column's class will indeed be mat-column-columnName, and I was only able to align the contents to the right, but not the headers. How do I select the column header? I tried mat-header-cell-columnName, to no avail.

gfigueroa avatar Jul 20 '17 20:07 gfigueroa

@gfigueroa data cells have the class .mat-cell and header cells have the class .mat-header-cell. You can combine those with .mat-column-columnName to select just the column data or just the column headers. Otherwise .mat-column-columnName should select both the data cells and the header cells

https://plnkr.co/edit/f6WuuRofSePei0CzLy7r?p=preview

willshowell avatar Jul 20 '17 21:07 willshowell

@willshowell Thanks for the reply. It aligns the headers to the right on a basic table. However, when using a table with sorting (and md-sort-header) it doesn't align them.

gfigueroa avatar Jul 21 '17 14:07 gfigueroa

@gfigueroa I used this CSS code and it worked:

.mat-column- { display: flex; justify-content: flex-end; }

MaickelHubner avatar Jul 21 '17 15:07 MaickelHubner

@MaickelHubner Are you using a table with sorting? It doesn't seem to work on mine when I use sorting.

gfigueroa avatar Jul 21 '17 16:07 gfigueroa

@gfigueroa https://plnkr.co/edit/Est96yNWhy9tFG5fE96r?p=preview

willshowell avatar Jul 21 '17 17:07 willshowell

@willshowell Ah it indeed works, though only for short header names. When you have a long header name (that takes up 2 lines) it centers it. https://plnkr.co/edit/A87H0D3UWWc94PcZ4LO9?p=preview

gfigueroa avatar Jul 21 '17 19:07 gfigueroa

@gfigueroa That's because with sort headers, they are actually buttons which have text-align: center applied to them. You can override that just fine but watch out for view encapsulation.

See updated styles in index.html and table-sorting-example.css https://plnkr.co/edit/Aqk5wS6mFh7JsT9IXRib?p=preview

For the sort of troubleshooting you're doing, it's best to open up the inspector and observe which classes each element has and how you can target them individually.

willshowell avatar Jul 21 '17 19:07 willshowell

@willshowell Thanks, that seems to do the trick! It was quite a workaround though.

gfigueroa avatar Jul 21 '17 19:07 gfigueroa

This "right" class works in 6 and corrects margin/padding of the header

.right.mat-cell {
    display: flex;
    justify-content: flex-end;
}
.right.mat-header-cell {
    display: flex;
    justify-content: flex-end;
    padding-right: 0;
    padding-left: 24px;
}
.right.mat-header-cell:last-child {
    margin-right: 0;
}

gazpan avatar May 15 '18 09:05 gazpan

using all of the above suggestions, I still get the right column of my table slightly askew, none of the divider lines line up, header is squished vertically, the first line of data is super big, and other lines are bigger than their respective rows.

johren02 avatar May 27 '18 15:05 johren02

I had a similar problem to @johren02 not including style differences in the first line of the data.

chrispruitt avatar Aug 06 '18 16:08 chrispruitt

Using what @MaickelHubner suggested.

.mat-column- { display: flex; justify-content: flex-end; }

I am getting what @johren02 is describing. How do we right align a column using mat-table?

prolink007 avatar Aug 08 '18 15:08 prolink007

I just want to align the HEADER of the columns to the rigth if the content is numeric or money ... and it do not work in my column header.

.mat-column-CODIGO { color: #333; text-align: right; padding-right: 10px; font-weight: bold;

flex: 0 0 75px; }

capturar

duard avatar Aug 09 '18 20:08 duard

In @angular/material 6.4.7 I was able to get sorted header cells to right align using following: In your component, bring in ViewEncapsulation, set it to none: @Component({ selector: 'app-whatever', templateUrl: './whatever.component.html', styleUrls: ['./whatever.component.css'], encapsulation: ViewEncapsulation.None }) In your template, in the sorted header cell that you want right aligned set a class: <th class="cell-right" mat-header-cell *matHeaderCellDef mat-sort-header> In your css file define the class like so: .cell-right .mat-sort-header-container { justify-content: flex-end; }

Setting ViewEncapsulation in your component will probably break your other formatting, so YMMV.

dcanfield avatar Dec 07 '18 14:12 dcanfield

@dcanfield your solution is the only one that worked for me using angular material 6.3.3, thanks a million!

chrispruitt avatar Dec 07 '18 15:12 chrispruitt

Fixed mine using the following code. <th mat-header-cell *matHeaderCellDef mat-sort-header style="direction: rtl"> Amount</th>

Change direction from 'Left to Right' to 'Right to Left' with direction: rtl for the header cell so the sorting arrow will be on the left. Then add justify-content: flex-end; to the column

thisarumahadurage avatar Jan 13 '19 08:01 thisarumahadurage