将扩展面板添加到角材料表内的行
Posted
技术标签:
【中文标题】将扩展面板添加到角材料表内的行【英文标题】:Adding expansion panel to the row inside angular material table 【发布时间】:2018-06-26 21:15:54 【问题描述】:我正在尝试添加一个类似于 mat-accordion 的扩展面板(可能是一个新组件),以便在单击时显示 mat-table 行内的详细视图。我附上了一个 plunker 来证明这个要求。 http://plnkr.co/edit/mBGT3D1JV5rXXRiEwCAc?p=info
这是我尝试添加扩展列的演示角度材料表,我在 html 数据表上添加了 plunker
<mat-table #table [dataSource]="dataSource">
<!-- 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>
</mat-table>
【问题讨论】:
您可以使用这种方法 - ***.com/a/47867948/3055401 更新:stackblitz.com/angular/… 旧方法:找到了我正在寻找的答案,即使该方法不是最好的方法,但它仍然有效。任何其他方法都值得赞赏。 stackblitz.com/edit/… 您提供的代码无法正常工作,会出现一些区域问题 已修复,您的代码可以正常运行,但无法按预期运行!扩展面板应在另一个打开后关闭 @Asif Karim Bherani,我的回答能回答你的问题吗? 【参考方案1】:在 angular2 材质文档中,现在有一个带有可扩展行的表格的示例: https://material.angular.io/components/table/examples
这是Stackblitz example 但我还包含了下面 Angular Material 文档中的 sn-ps
import Component from '@angular/core';
import animate, state, style, transition, trigger from '@angular/animations';
/**
* @title Table with expandable rows
*/
@Component(
selector: 'table-expandable-rows-example',
styleUrls: ['table-expandable-rows-example.css'],
templateUrl: 'table-expandable-rows-example.html',
animations: [
trigger('detailExpand', [
state('collapsed', style(height: '0px', minHeight: '0')),
state('expanded', style(height: '*')),
transition('expanded <=> collapsed', animate('225ms cubic-bezier(0.4, 0.0, 0.2, 1)')),
]),
],
)
export class TableExpandableRowsExample
dataSource = ELEMENT_DATA;
columnsToDisplay = ['name', 'weight', 'symbol', 'position'];
expandedElement: PeriodicElement | null;
export interface PeriodicElement
name: string;
position: number;
weight: number;
symbol: string;
description: string;
const ELEMENT_DATA: PeriodicElement[] = [
position: 1,
name: 'Hydrogen',
weight: 1.0079,
symbol: 'H',
description: `Hydrogen is a chemical element with symbol H and atomic number 1. With a standard
atomic weight of 1.008, hydrogen is the lightest element on the periodic table.`
,
position: 2,
name: 'Helium',
weight: 4.0026,
symbol: 'He',
description: `Helium is a chemical element with symbol He and atomic number 2. It is a
colorless, odorless, tasteless, non-toxic, inert, monatomic gas, the first in the noble gas
group in the periodic table. Its boiling point is the lowest among all the elements.`
,
position: 3,
name: 'Lithium',
weight: 6.941,
symbol: 'Li',
description: `Lithium is a chemical element with symbol Li and atomic number 3. It is a soft,
silvery-white alkali metal. Under standard conditions, it is the lightest metal and the
lightest solid element.`
,
position: 4,
name: 'Beryllium',
weight: 9.0122,
symbol: 'Be',
description: `Beryllium is a chemical element with symbol Be and atomic number 4. It is a
relatively rare element in the universe, usually occurring as a product of the spallation of
larger atomic nuclei that have collided with cosmic rays.`
,
position: 5,
name: 'Boron',
weight: 10.811,
symbol: 'B',
description: `Boron is a chemical element with symbol B and atomic number 5. Produced entirely
by cosmic ray spallation and supernovae and not by stellar nucleosynthesis, it is a
low-abundance element in the Solar system and in the Earth's crust.`
,
position: 6,
name: 'Carbon',
weight: 12.0107,
symbol: 'C',
description: `Carbon is a chemical element with symbol C and atomic number 6. It is nonmetallic
and tetravalent—making four electrons available to form covalent chemical bonds. It belongs
to group 14 of the periodic table.`
,
position: 7,
name: 'Nitrogen',
weight: 14.0067,
symbol: 'N',
description: `Nitrogen is a chemical element with symbol N and atomic number 7. It was first
discovered and isolated by Scottish physician Daniel Rutherford in 1772.`
,
position: 8,
name: 'Oxygen',
weight: 15.9994,
symbol: 'O',
description: `Oxygen is a chemical element with symbol O and atomic number 8. It is a member of
the chalcogen group on the periodic table, a highly reactive nonmetal, and an oxidizing
agent that readily forms oxides with most elements as well as with other compounds.`
,
position: 9,
name: 'Fluorine',
weight: 18.9984,
symbol: 'F',
description: `Fluorine is a chemical element with symbol F and atomic number 9. It is the
lightest halogen and exists as a highly toxic pale yellow diatomic gas at standard
conditions.`
,
position: 10,
name: 'Neon',
weight: 20.1797,
symbol: 'Ne',
description: `Neon is a chemical element with symbol Ne and atomic number 10. It is a noble gas.
Neon is a colorless, odorless, inert monatomic gas under standard conditions, with about
two-thirds the density of air.`
,
];
table
width: 100%;
tr.example-detail-row
height: 0;
tr.example-element-row:not(.example-expanded-row):hover
background: whitesmoke;
tr.example-element-row:not(.example-expanded-row):active
background: #efefef;
.example-element-row td
border-bottom-width: 0;
.example-element-detail
overflow: hidden;
display: flex;
.example-element-diagram
min-width: 80px;
border: 2px solid black;
padding: 8px;
font-weight: lighter;
margin: 8px 0;
height: 104px;
.example-element-symbol
font-weight: bold;
font-size: 40px;
line-height: normal;
.example-element-description
padding: 16px;
.example-element-description-attribution
opacity: 0.5;
<table mat-table
[dataSource]="dataSource" multiTemplateDataRows
class="mat-elevation-z8">
<ng-container matColumnDef="column" *ngFor="let column of columnsToDisplay">
<th mat-header-cell *matHeaderCellDef> column </th>
<td mat-cell *matCellDef="let element"> element[column] </td>
</ng-container>
<!-- Expanded Content Column - The detail row is made up of this one column that spans across all columns -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
<div class="example-element-detail"
[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
<div class="example-element-diagram">
<div class="example-element-position"> element.position </div>
<div class="example-element-symbol"> element.symbol </div>
<div class="example-element-name"> element.name </div>
<div class="example-element-weight"> element.weight </div>
</div>
<div class="example-element-description">
element.description
<span class="example-element-description-attribution"> -- Wikipedia </span>
</div>
</div>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>
您需要的关键位是:
component.html / 标记multiTemplateDataRows
上的 mat-table
指令
扩展行的专用列(在提供的示例中为matColumnDef="expandedDetail"
)。请注意 colspan
属性,它使这个跨越整个表。
扩展行的专用行。这将包含扩展列 ^
div 上的detailExpand
指令包含展开行[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'"
中的详细信息
component.ts
@Component
装饰器中的动画。这些在 UI 中切换展开/折叠状态
一个属性(在本例中为expandedElement
),用于跟踪展开的行
component.scss
隐藏展开行溢出的样式
.example-element-detail overflow: hidden;
【讨论】:
这是 OP 预期方法的替代方案。 @stefan 可以回答这个问题:***.com/questions/65497251/…以上是关于将扩展面板添加到角材料表内的行的主要内容,如果未能解决你的问题,请参考以下文章