我正在使用Angular Material v6。每当单击mat-table
中的一行时,我都想添加波纹效果。
HTML:
<table mat-table [dataSource]="this.projectsList" class="mat-elevation-z8">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef>ID</th>
<td mat-cell *matCellDef="let project">{{project.id}}</td>
</ng-container>
<ng-container matColumnDef="title">
<th mat-header-cell *matHeaderCellDef>Title</th>
<td mat-cell *matCellDef="let project">{{project.title}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="this.displayColumns"></tr>
<tr mat-row *matRowDef="let row; columns: this.displayColumns"></tr>
</table>
SCSS:
table {
width: 90vw;
margin-top: 1%;
margin-left: auto;
margin-right: auto;
}
.mat-row:hover {
background: rgba(0, 0, 0, 0.04);
}
模块导入:
AppRoutingModule,
BrowserModule,
BrowserAnimationsModule,
MatToolbarModule,
MatSidenavModule,
MatButtonModule,
MatCheckboxModule
自定义主题:
@import '~@angular/material/theming';
@import './palette.scss';
// Plus imports for other components in your app.
// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// **Be sure that you only ever include this mixin once!**
@include mat-core();
// Define the default theme (same as the example above).
$light-primary-blue: mat-palette($primary-blue);
$light-accent-purple: mat-palette($accent-purple);
$light-theme: mat-light-theme($light-primary-blue, $light-accent-purple);
// Include the default theme styles.
@include angular-material-theme($light-theme);
// Define an alternate dark theme.
$dark-primary: mat-palette($primary-blue);
$dark-accent: mat-palette($accent-purple);
$dark-theme: mat-dark-theme($dark-primary, $dark-accent);
// Include the alternative theme styles inside of a block with a CSS class. You can make this
// CSS class whatever you want. In this example, any component inside of an element with
// `.dark` will be affected by this alternate dark theme instead of the default theme.
.dark {
@include angular-material-theme($dark-theme);
}
我找不到任何有关在表上添加波纹效果的文档,但是我看到了几个Stackblitz实例,它们在行单击时具有波纹效果。我试图复制他们正在做的事情,但没有成功。
答案 0 :(得分:1)
如下修改您的td元素。
<table mat-table [dataSource]="this.projectsList" class="mat-elevation-z8">
<ng-container matColumnDef="title">
<th mat-header-cell *matHeaderCellDef>Title</th>
<td mat-cell *matCellDef="let project">
<div class='mat-ripple-wrapper' matRipple>
{{project.title}}
</div>
</td>
</ng-container>
</table>
样式
.mat-ripple-wrapper{
position: relative;
}
单击触发器元素后,mat-ripple指令将创建一个div,如果将其放置在一行上,它将使其不适合放置。最好的办法是将td数据包装在div中,然后在该div上添加指令以包含动态生成的div。
答案 1 :(得分:0)
您可以尝试使用此代码
/deep/.mat-row:hover {
background: rgba(0, 0, 0, 0.04);
}
对于波纹效果,请尝试
.ripple {
background-position: center;
transition: background 0.8s;
}
.ripple:hover {
background: #47a7f5 radial-gradient(circle, transparent 1%, #47a7f5 1%) center/15000%;
}
.ripple:active {
background-color: #6eb9f7;
background-size: 100%;
transition: background 0s;
}