【问题标题】:Angular Material Table -Border Inside the Table角材料表 - 表内边框
【发布时间】:2018-06-22 14:18:01
【问题描述】:

我正在使用 Angular 材质表,我想在表内设置边框,使用 CSS 我能够设置边框:正常情况 [
但是当特定单元格的内容增加时,相邻单元格的边框不会增长并且表格看起来很糟糕,带有额外内容的单元格
这是 CSS:

`.example-container {
  display: flex;
  flex-direction: column;
  max-height: 500px;
  min-width: 300px;
}

.mat-table {
  overflow: auto;
  max-height: 500px;
}
.mat-column-name{
  border-left: 1px solid grey;
  min-height: 48px;

}
.mat-column-weight{
  border-left: 1px solid grey;

   min-height: 48px;

.mat-column-symbol{
 border-left: 1px solid grey;
   min-height: 48px;
}`

HTML`

<!--- Note that these columns can be defined in any order.
      The actual rendered columns are set as a property on the row definition" -->

<!-- Position Column -->
<ng-container matColumnDef="position">
  <mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>

<!-- Name Column -->
<ng-container matColumnDef="name">
  <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>

<!-- Weight Column -->
<ng-container matColumnDef="weight">
  <mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
</ng-container>

<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
  <mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
  <mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>

<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>

`

【问题讨论】:

    标签: css angular angular-material


    【解决方案1】:

    方法:1 每当父行高度增加时,您都需要拉伸表格单元格的内容。 为此,您可以将表格单元格设置为弹性框,向 mat-cell &lt;mat-cell class="flex-stretch"&gt; 添加一个额外的类并将这些添加到您的 css 中:

    .mat-cell .flex-stretch {
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
        -webkit-align-self: stretch;
        -ms-flex-item-align: stretch;
        align-self: stretch;
        /* align-items center so that cell content is vertically centered */
        -webkit-align-items: center;
        -ms-flex-align: center;
        align-items: center;
    }
    

    【讨论】:

      【解决方案2】:

      方法 2: 最终,我找到了解决方案,因为材质使用了我们可以使用的 flex-layout CSS align-self: stretch; /* Stretch 'auto'-sized items to fit the container */Result with align-self: stretch

      这是更新后的 CSS

          `.example-container {
            display: flex;
            flex-direction: column;
      
            flex-basis: 300px;
          }
      
          .mat-table {
            overflow: auto;
            max-height: 500px;
          }
      
           .mat-column-name{
          border-right: 1px solid grey;
          align-self: stretch;
          text-align: center
      
      
          }
          .mat-column-position{
          border-right: 1px solid grey;
          align-self: stretch;
          text-align: center;
      
          }
          .mat-column-weight{
          border-right: 1px solid grey;
          align-self: stretch;
          text-align: center;
      
          } 
          .mat-column-symbol{
      
          text-align: center;
          align-self: stretch;
          }
         .mat-column-weight{
             align-self: stretch;
          } `
      

      【讨论】:

      • 很高兴你找到了巧妙的解决方案:)
      • 到了这里,也可以使用通配符来防止css规则重复:[class*="mat-column-"] {border-right: 1px solid gray; ... } link
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2019-04-19
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多