在Angular 7中,可以使用Mat Table来实现可展开行的效果。以下是一个包含代码示例的解决方法:
npm install --save @angular/material @angular/cdk @angular/animations
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatTableModule } from '@angular/material/table';
import { MatExpansionModule } from '@angular/material/expansion';
@NgModule({
imports: [
BrowserAnimationsModule,
MatTableModule,
MatExpansionModule
],
// ...
})
export class AppModule { }
Name
{{row.name}}
{{element.description}}
.example-element-row {
cursor: pointer;
}
.example-expanded-row {
background: rgba(0, 0, 0, 0.1);
}
.example-detail-row {
background: rgba(0, 0, 0, 0.1);
}
.example-element-detail {
overflow: hidden;
display: flex;
}
.example-element-diagram {
width: 100px;
height: 100px;
background: #555;
}
.example-element-description {
padding: 16px;
}
import { Component } from '@angular/core';
export interface Element {
name: string;
description: string;
}
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.css']
})
export class TableComponent {
displayedColumns: string[] = ['name'];
dataSource: Element[] = [
{ name: 'Element 1', description: 'Description 1' },
{ name: 'Element 2', description: 'Description 2' },
{ name: 'Element 3', description: 'Description 3' }
];
expandedElement: Element | null;
constructor() {
this.expandedElement = this.dataSource[0];
}
}
以上是使用Angular 7中的Mat Table实现可展开行的解决方法。你可以根据自己的需求进行修改和调整。