简体   繁体   English

用颜色填充垫桌的单元格

[英]Fill a mat-table's cell with a color

I have a fairly basic column in a mat-table as follows: 我在mat-table有一个相当基本的列,如下所示:

<mat-table #table [dataSource]="dataSource" matSort>

    <ng-container matColumnDef="zone">
        <mat-header-cell *matHeaderCellDef mat-sort-header></mat-header-cell>
        <mat-cell *matCellDef="let item" [style.background-color]="'#' + item.zone.color"></mat-cell>
    </ng-container>

    ...

</mat-table>

I'd like to fill zone column cells with some colors varying from row to row (not the whole row, only cells of this specific column). 我想填充zone列单元格,其中一些颜色在行之间变化(不是整行,只有该特定列的单元格)。

All I could find so far is coloring the whole row. 到目前为止,我所能找到的只是为整行着色。

The cells of the zone column are rendered to the following HTML: zone列的单元格呈现为以下HTML:

<mat-cell class="mat-cell cdk-column-zone mat-column-zone" role="gridcell" style="background-color: rgb(255, 182, 193);" _ngcontent-c8=""></mat-cell>

We can see that background-color style property has been correctly applied, but the problem is that cell's height is 0. because the cell doesn't have any content. 我们可以看到background-color样式属性已被正确应用,但问题是单元格的高度为0.因为单元格没有任何内容。 I tried to put an &nbsp; 我试着把一个&nbsp; inside the cell, but it gets displayed as a thin bar with top and bottom margins: 在单元格内部,但它显示为带有顶部和底部边距的细条:

在此输入图像描述

And what I want is to color the whole cell including its margins. 我想要的是为整个细胞着色,包括其边缘。

How can we color individual cells in mat-table ? 我们如何在mat-table单个细胞着色?

If you look at the default CSS provided by Angular Material , you will notice that the mat-row element has a minimum height of 48px . 如果查看Angular Material提供的默认CSS,您会注意到mat-row元素的最小高度为48px

垫排高度

But if you look at the mat-cell you will notice that it don't have a minimum neither a default height set, it relies on the parent element (the row) display / alignment properties to stay centered. 但是如果你看一下mat-cell你会注意到它没有最小的默认高度设置,它依赖于父元素(行)显示/对齐属性来保持居中。

垫子高度

If you look at the previous image it shows exactly the area that is being colored by the background color property in your code. 如果查看上一个图像,它会准确显示代码中背景颜色属性所着色的区域。

So for changing the background color for the entire cell, one of your options would be to set the same minimum height used by the rows to the cells, which could be done using the following CSS declaration: 因此,要更改整个单元格的背景颜色,您可以选择将行使用的最小高度设置为单元格,这可以使用以下CSS声明来完成:

.mat-cell, .mat-header-cell {
    flex: 1;
    min-height: 48px;
    display: flex;
    align-items: center;
}

And then simple use the background color property for changing the color. 然后简单地使用背景颜色属性来更改颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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