[英]Angular Material mat table merged rows
我正在努力解決以下問題。 我想將我的基本 html 表更改為 mat-table。 但是我找不到合並前兩列上的兩行的方法。
我在頂部有 mat-table,底部有基本的 html 表:
[![在此處輸入圖像描述][1]][1]
我想要在墊子表中的內容如下:對於每個參考 + 發票,我想要兩行,一列帶有發票,另一列帶有預期,就像在我的底部表格中一樣。
我的基本 html 表如下:
<table>
<thead>
<th *ngFor= "let column of headersContainers">
{{column}}
</th>
</thead>
<tbody *ngFor = "let container of completeContainers">
<th rowspan = "2" >{{container.containerReference}}</th>
<th rowspan = "2" >{{container.invoiceReference}}</th>
<td>Invoiced</td>
<td>{{container.InvoicedCosts.OFC | currency :'€ '}}</td>
<td>{{container.InvoicedCosts.THC | currency :'€ '}}</td>
<td>{{container.InvoicedCosts.BAF | currency :'€ '}}</td>
<td>{{container.InvoicedCosts.Total | currency :'€ '}}</td>
<tr>
<td>Expected</td>
<td>{{container.ExpectedCosts.OFC | currency :'€ '}}</td>
<td>{{container.ExpectedCosts.THC | currency :'€ '}}</td>
<td>{{container.ExpectedCosts.BAF | currency :'€ '}}</td>
<td>{{container.ExpectedCosts.Total | currency :'€ '}}</td>
</tr>
</tbody>
</table>
雖然我的 mat-table html 如下:
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="containerReference">
<th mat-header-cell *matHeaderCellDef [rowSpan]="2"> Reference </th>
<td mat-cell *matCellDef="let container"> {{container.containerReference}} </td>
</ng-container>
<ng-container matColumnDef="InvoiceReference">
<th mat-header-cell *matHeaderCellDef [rowSpan]="2"> Invoice </th>
<td mat-cell *matCellDef="let container"> {{container.invoiceReference}} </td>
</ng-container>
<ng-container matColumnDef="InvoicedOrExpected">
<th mat-header-cell *matHeaderCellDef> Invoiced or expected </th>
<td mat-cell *matCellDef="let container"> Invoiced </td>
</ng-container>
<ng-container matColumnDef="OHC">
<th mat-header-cell *matHeaderCellDef> Ocean handling costs </th>
<td mat-cell *matCellDef="let container"> {{container.OHC}} </td>
</ng-container>
<ng-container matColumnDef="THC">
<th mat-header-cell *matHeaderCellDef> Terminal handling costs </th>
<td mat-cell *matCellDef="let container"> {{container.THC}} </td>
</ng-container>
<ng-container matColumnDef="BAF">
<th mat-header-cell *matHeaderCellDef> Bunker adjustment factor </th>
<td mat-cell *matCellDef="let container"> {{container.BAF}} </td>
</ng-container>
<ng-container matColumnDef="Total">
<th mat-header-cell *matHeaderCellDef> Total </th>
<td mat-cell *matCellDef="let container"> {{container.Total}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="headersContainers"></tr>
<tr mat-row *matRowDef="let row; columns: headersContainers;"> </tr>
</table>
我的數據源如下所示:
[
{
"containerReference": "20DV HLXU1234567",
"invoiceReference": "109554 (20000678)",
"InvoicedCosts": {
"OFC": 1023.0,
"THC": 100.0,
"BAF": 67.0,
"Total": 1190.0
},
"ExpectedCosts": {
"OFC": 465.0,
"THC": 205.0,
"BAF": 285.0,
"Total": 955.0
}
},
{
"containerReference": "20DV HLXU2234567",
"invoiceReference": "109554 (20000678)",
"InvoicedCosts": {
"OFC": 3445.0,
"THC": 65.0,
"BAF": 77.0,
"Total": 3587.0
},
"ExpectedCosts": {
"OFC": 465.0,
"THC": 205.0,
"BAF": 285.0,
"Total": 955.0
}
}
]
請注意,我可以更改數據源設置,因為我在 java 中使用了 ArrayNode。
編輯:
我已將 HTML 文件更改為:
<h1>Costs per container</h1>
<mat-checkbox (change)="switchEvent($event)" labelPosition ="before">Show correctly invoiced containers</mat-checkbox>
<table mat-table [dataSource]="dataSource">
<!--Container reference-->
<ng-container matColumnDef="containerReference">
<th mat-header-cell *matHeaderCellDef> Reference </th>
<td mat-cell *matCellDef="let container"> {{container.containerReference}} </td>
</ng-container>
<!--Invoice reference-->
<ng-container matColumnDef="invoiceReference">
<th mat-header-cell *matHeaderCellDef> Invoice </th>
<td mat-cell *matCellDef="let container"> {{container.invoiceReference}} </td>
</ng-container>
<!--Invoiced columns-->
<ng-container matColumnDef="invoicedOHC">
<th mat-header-cell *matHeaderCellDef> Ocean handling costs </th>
<td mat-cell *matCellDef="let container"> {{container.InvoicedCosts.OFC}} </td>
</ng-container>
<ng-container matColumnDef="invoicedTHC">
<th mat-header-cell *matHeaderCellDef> Terminal handling costs </th>
<td mat-cell *matCellDef="let container"> {{container.InvoicedCosts.THC}} </td>
</ng-container>
<ng-container matColumnDef="invoicedBAF">
<th mat-header-cell *matHeaderCellDef> Bunker adjustment factor </th>
<td mat-cell *matCellDef="let container"> {{container.InvoicedCosts.BAF}} </td>
</ng-container>
<ng-container matColumnDef="invoicedTotal">
<th mat-header-cell *matHeaderCellDef> Total </th>
<td mat-cell *matCellDef="let container"> {{container.InvoicedCosts.Total}} </td>
</ng-container>
<!--expected columns-->
<ng-container matColumnDef="expectedOHC">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let container"> {{container.ExpectedCosts.OFC}} </td>
</ng-container>
<ng-container matColumnDef="expectedTHC">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let container"> {{container.ExpectedCosts.THC}} </td>
</ng-container>
<ng-container matColumnDef="expectedBAF">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let container"> {{container.ExpectedCosts.BAF}} </td>
</ng-container>
<ng-container matColumnDef="expectedTotal">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let container"> {{container.ExpectedCosts.Total}} </td>
</ng-container>
<!--Invoiced or expected columns-->
<ng-container matColumnDef="invoicedLabel">
<th mat-header-cell *matHeaderCellDef> Invoiced or expected </th>
<td mat-cell *matCellDef="let container"> Invoiced </td>
</ng-container>
<ng-container matColumnDef="expectedLabel">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let container"> Expected </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="headersContainers"></tr>
<tr mat-row *matRowDef="let row; columns: headersContainers;"> </tr>
</table>
SCSS 如下:
table {
width: 100%;
}
.mat-column-containerReference {grid-area: containerReference;}
.mat-column-invoiceReference {grid-area: invoiceReference;}
.mat-column-invoicedOHC {grid-area: invoicedOHC;}
.mat-column-invoicedTHC {grid-area: invoicedTHC;}
.mat-column-invoicedBAF {grid-area: invoicedBAF;}
.mat-column-invoicedTotal {grid-area: invoicedTotal;}
.mat-column-expectedOHC {grid-area: expectedOHC;}
.mat-column-expectedTHC {grid-area: expectedTHC;}
.mat-column-expectedBAF {grid-area: expectedBAF;}
.mat-column-expectedTotal {grid-area: expectedTotal;}
.mat-column-expected {grid-area: expected;}
.mat-column-invoiced {grid-area: invoiced;}
.mat-column-invoicedLabel {grid-area: invoicedLabel;}
.mat-column-expectedLabel {grid-area: expectedLabel;}
tr {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-areas:
"containerReference invoiceReference invoicedLabel invoicedOHC invoicedTHC invoicedBAF invoicedTotal"
"containerReference invoiceReference expectedLabel expectedOHC expectedTHC expectedBAF expectedTotal";
}
tr.mat-header-row {
grid-template-areas:
"containerReference invoiceReference invoicedLabel invoicedOHC invoicedTHC invoicedBAF invoicedTotal";
.mat-column-expected,
.mat-column-expectedLabel {
display: none;
}
}
td, th {
display: flex;
align-items: center;
padding-left: 5px !important;
border-right: 1px solid rgba(128, 128, 128, .4);
}
這導致我想要的合並行,但列不與行對齊:
[![在此處輸入圖像描述][2]][2]
編輯2:
在 RobbieAreBest 的評論之后,我改變了 scss 部分grid-template-columns: repeat(4, 1fr);
到grid-template-columns: repeat(7, 1fr);
這給了我以下結果:[![在此處輸入圖像描述][3]][3]
要么,它看起來仍然不完美。 如果有人知道如何使單元格對齊,那就太好了! [1]: https://i.stack.imgur.com/JzP51.png [2]: https://i.stack.imgur.com/fnut4.png [3]: HTTPS://i.stack。 imgur.com/Vx7cK.png
我認為這對於 CSS 網格來說是一個很好的例子。 在您的模板中,我根本不擔心 rowSpans,只需將所有數據放入其自己的列中(對於已開票和預期值有單獨的列)。
為你的tr
標簽添加一個規則display:grid
並根據需要定義你的grid-template-columns
。 然后,您可以將grid-area
應用於每一列,並使用grid-template-areas
規則定義您的“rowSpan”。
如果您希望標題以不同方式顯示,您tr.mat-header-row
定義一組不同的grid-template-areas
規則。
我已經制定了一個小例子,應該有一個類似於你正在尋找的解決方案: https : //stackblitz.com/edit/angular-u2b8qa?file=src/app/table-basic-example.scss
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.