使用MAT-Table在动态表上的特定列上应用Colspan

发布于 2025-02-13 20:45:12 字数 709 浏览 1 评论 0 原文

我有 Mat-table 在其中动态创建列 我想使用 [attr.colspan] 添加扩展列功能 如果在列标题上检测到单击事件,则将atter应用于该特定列上,

将其插入HTML模板将更改应用于第一列,

 <ng-container
matColumnDef="{{ column }}"
*ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">{{ column }}</th>
<td mat-cell *matCellDef="let element"
[attr.colspan]="1"
>{{ element[column] }}</td>
</ng-container>

如何动态应用 [attr.colspan]

I have mat-table in which columns get created dynamically
I want to add a expand column functionality using [attr.colspan]
if a click event is detected on a column header the attr gets applied on that specific column

inserting it in the HTML template applies the change to the first column

 <ng-container
matColumnDef="{{ column }}"
*ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">{{ column }}</th>
<td mat-cell *matCellDef="let element"
[attr.colspan]="1"
>{{ element[column] }}</td>
</ng-container>

How can I apply the [attr.colspan] dynamically ?

stack blitz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-02-20 20:45:12

是的,您可以使用任何表达式,例如

<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">
  {{ column }}
</th>
<ng-container mat-cell *matCellDef="let element">
  <td
    *ngIf="!element.colspan || column != 'name'"
    [attr.colspan]="column == 'position'?element.colspan:null"
  >
    {{ element[column] }}
  </td>
</ng-container>

,您有一些数据喜欢

 [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He',colspan:2}
 ]

查看您的分叉stackblitz

请参阅制作colspan时,您会“删除”下一个TD

Yes, you can use any expresion, e.g.

<th mat-header-cell *matHeaderCellDef (click)="headerEvent(column)">
  {{ column }}
</th>
<ng-container mat-cell *matCellDef="let element">
  <td
    *ngIf="!element.colspan || column != 'name'"
    [attr.colspan]="column == 'position'?element.colspan:null"
  >
    {{ element[column] }}
  </td>
</ng-container>

And you have data some like

 [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He',colspan:2}
 ]

See your forked stackblitz

See that you "remove" the next td when you make a colspan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文