Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align right one mat-header-cell in a angular material table with sort headers

I am trying to align one of the headers in my table right. I tried:

.header-align-right {
    display: flex;
    justify-content: flex-end;
  }

In a class and add it to the mat-header-cell. This aligned the text right but also added weird spacings to the element that made it not aligned with the rest of the headers. Tried it also without the display:flex but that did nothing.

<ng-container matColumnDef="Number">
        <th mat-header-cell *matHeaderCellDef mat-sort-header class="header-align-right">Number</th>
        <td mat-cell *matCellDef="let row" align="right">{{row.Number}}</td>
        <td mat-footer-cell *matFooterCellDef></td>
</ng-conIainer>

As you see i align right the content of the cell and I would like to align the header as well.

like image 766
Sknecht Avatar asked Mar 12 '19 09:03

Sknecht


People also ask

How do you make sort arrows in angular mat-table always visible?

In the case where a column is unsorted, e.g., the arrow is in an undefined state, the arrow should be an up arrow and have the opacity 0.54. If I click on the arrow, it should have the opacity set to 1. If I cancel the sorting, the arrow should have the opacity 0.54 again.

How do I right align text in header?

Align the text left or right Select the text that you want to align. On the Home tab, in the Paragraph group, click Align Left or Align Right .

What is Mat sort header?

The <mat-sort-header> is used to add sorting state and display the data in tabular data.


1 Answers

Cell alignment is done by default via flexbox when using <mat-header-cell> and <mat-cell>, but not when using <th mat-header-cell> and <td mat-cell>. When using the table elements, the alignment is done with text-align. If you force flexbox all of the time, the table cells using th and td have the possibility of losing vertical alignment with the rest of the table.

I found that a combination of setting text-align: right and setting justify-content: flex-end works best. That way, elements with display:table-cell use text-align, and elements with display:flex use justify-content.

For your example:

.header-align-right {
  text-align: right;
  justify-content: flex-end;
}

To get the arrow to show up before the header, you should use the arrowPosition attribute.

Also, I would suggest using the auto-added column class (.mat-column- + case-sensitive value of matColumnDef) instead of adding class="header-align-right". That will align the headers, cells, and footers. So here's what I would suggest:

.mat-column-Number {
  text-align: right;
  justify-content: flex-end;
}
<ng-container matColumnDef="Number">
  <th mat-header-cell *matHeaderCellDef mat-sort-header arrowPosition="before">Number</th>
  <td mat-cell *matCellDef="let row">{{row.Number}}</td>
  <td mat-footer-cell *matFooterCellDef></td>
</ng-conIainer>
like image 184
bts Avatar answered Oct 18 '22 18:10

bts