简体   繁体   中英

In Angular9's mat-table, how do I enforce that DIVs within a mat-cell start new lines?

I'm using Angular 9. I have defined a very simple mat-table element...

<mat-table #table [dataSource]="dataSource">
  <ng-container matColumnDef="article">
    <mat-cell *matCellDef="let article_stat">
      <div>Hello</div>
      <div>Good-bye</div>
    </mat-cell>
    </ng-container>
</mat-table>

These are the only styles I have in the component...

.mat-row {
  border-bottom: 1px solid;
}

.mat-cell {
  padding-top: .5em;
  padding-bottom: .5em;
}

However, when my table renders, the two different DIVs are aligning on the same horizontal plane.

在此处输入图像描述

I thought DIVs were supposed to wrap to different lines, especially since I haven't applied any additional styles, but I'm not sure what else I need to do to restore the original DIV behavior.

Per the answer, I tried this根据答案,我试过这个

<mat-cell *matCellDef="let article_stat">
<div class="smallHeading">
{{ article_stat.article.path }}
</div>
<div class='mainRow'>
...
</div>
</mat-cell>

with css

.smallHeading, .mainRow {
  display: flex;
  flex-direction: column;
}

but still the DIVs appear on the same horizontal line.

You are using the mat-table and mat-cell directives incorrectly.

You need to place the directives as attributes on vanilla table and td elements.

<table mat-table #table [dataSource]="dataSource">
  <ng-container matColumnDef="article">
    <td mat-cell *matCellDef="let article_stat">
      <div>Hello</div>
      <div>Good-bye</div>
    </td>
    </ng-container>
</table>

Now, you cell content will be placed on separate lines:)

See for more info: https://material.angular.io/components/table/examples

Did u try it as below?

HTML

<mat-table #table [dataSource]="dataSource">
  <ng-container matColumnDef="article">
    <mat-cell *matCellDef="let article_stat">
      <div class="multi-line">
        <div>Hello</div>
        <div>Good-bye</div>
      </div>
    </mat-cell>
    </ng-container>
</mat-table>

CSS

.multi-line {
   display: block; width: 100%;
}

Hope it will help. If it is not your requirement then clarify a bit more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM