简体   繁体   English

<mat-table>删除行时分页与粘性发生冲突

[英]<mat-table> pagination runs into conflict with sticky when deleting row

I have a problem, can not find anything online that was helpful to me and therefore hope that someone can help me here.我有一个问题,在网上找不到任何对我有帮助的东西,因此希望有人可以在这里帮助我。

I am building a table which is dynamically filled.我正在构建一个动态填充的表格。 The user has the possibility to delete single rows of the table after the dynamic filling.用户可以在动态填充后删除表的单行。 The has a sticky column (the very first one) and pagination.有一个粘性列(第一个)和分页。 These two features have caused me a number of problems and one of them I cannot solve.这两个功能给我带来了许多问题,其中一个我无法解决。

The following scenario (see in gif): I have 10 rows in the table and because of the paginator, I only display for example 5. If I now delete one of the 5 displayed rows, the sixth row "slips in" and fills the space of the deleted row.以下场景(参见 gif):表格中有 10 行,由于分页器,我只显示例如 5。如果我现在删除显示的 5 行之一,第六行“滑入”并填充被删除行的空间。 Exactly here is the problem.问题正是在这里。 With this "new" line the sticky element simply does not work.使用这条“新”行,粘性元素根本不起作用。 But only for this line, the other 4 are working perfectly fine.但仅对于这条线,其他 4 条运行良好。 The strange thing is that as soon as I click on the edit button or just hover over the paginator, the row is somehow "updated" and the sticky column works again.奇怪的是,只要我单击编辑按钮或将鼠标悬停在分页器上,该行就会以某种方式“更新”并且粘性列再次起作用。

If there are any questions please feel free to respond - I'm happy about any help 🙂如果有任何问题,请随时回复 - 我很高兴有任何帮助🙂

Stackblitz: https://stackblitz.com/edit/angular-editable-table-part-1-sticky-column-atdm3y?file=src/app/app.component.ts (You have to click a delete button once, in order to "start the real bug" which concerns me.. something with the initialization does not work in this stackblitz example, but that's not important in this situation) Stackblitz: https ://stackblitz.com/edit/angular-editable-table-part-1-sticky-column-atdm3y?file=src/app/app.component.ts (您必须单击一次删除按钮,在为了“启动真正的错误”,这与我有关..在这个 stackblitz 示例中初始化的东西不起作用,但这在这种情况下并不重要)

在此处输入图像描述

You need to use trackBy, lucky MatTable provides that as Input https://material.angular.io/components/table/api#MatTable您需要使用 trackBy,幸运MatTable将其作为输入https://material.angular.io/components/table/api#MatTable

NgForOf needs to uniquely identify items in the iterable to correctly perform DOM updates when items in the iterable are reordered, new items are added, or existing items are removed. NgForOf需要唯一标识可迭代项中的项,以便在可迭代项中的项被重新排序、添加新项或删除现有项时正确执行 DOM 更新。

Ref: https://angular.io/api/core/TrackByFunction#description参考: https ://angular.io/api/core/TrackByFunction#description

So basically simple fix:所以基本上简单的修复:

HTML template: HTML 模板:

<table mat-table [dataSource]="dataSource" [trackBy]="trackByIdentity">

Where trackByIdentity can be as simple as:其中 trackByIdentity 可以很简单:

trackByIdentity(index, item) {
   return item.id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM