简体   繁体   English

Angular - DataSource 的 Observable 或 Subject

[英]Angular - Observable or Subject for DataSource

I have an Angular 7 application with Mat Tables retrieving data from API.我有一个 Angular 7 应用程序,其 Mat Tables 从 API 检索数据。 I have assigned dynamic pagination values, that is pageSizeOptions value will be getting changed whenever I load grid based on some dropdown value and by default all the records will be getting displayed.我已经分配了动态分页值,也就是说,每当我根据某个下拉值加载网格时,pageSizeOptions 值都会发生变化,默认情况下所有记录都会显示。

If API returns 23 records, then pageSizeOptions will have 10,20,23,30,40,50 and by default it will display all 23 records.如果 API 返回 23 条记录,则 pageSizeOptions 将有 10,20,23,30,40,50 并且默认显示所有 23 条记录。 When next time I change dropdown value, API returns 45 records but this time still my grid displays only 23 records eventhough pageSizeOptions has 10,20,30,40,45,50 and selection shows 45 as selected.下次我更改下拉值时,API 返回 45 条记录,但这次我的网格仍然只显示 23 条记录,尽管 pageSizeOptions 有 10、20、30、40、45、50 并且选择显示 45 为选中状态。

I assume this is due to View/Grid loaded before pageSizeOptions value is assigned since API takes some time to return data.我认为这是由于在分配 pageSizeOptions 值之前加载了视图/网格,因为 API 需要一些时间来返回数据。 I thought of resolving this issue by implementing Observable/subscribe.我想通过实现 Observable/subscribe 来解决这个问题。

I am not sure, how to implement these for pageSizeOptions.我不确定如何为 pageSizeOptions 实现这些。 Can someone please help me to resolve this.有人可以帮我解决这个问题。

let gridData = responseStudents.Students.map(item => new ResponseStudents());
this.myDataSource = new MatTableDataSource(gridData);
this.Count = gridData.length;
this.PageSizeOptions = [this.Count, 5, 10, 25, 100, 500];
//To remove duplicate
this.PageSizeOptions = Array.from(this.PageSizeOptions.reduce((m, t) => m.set(t, t), new Map()).values());

I was able to get a StackBlitz that may accomplish the foundation of what you are looking for.我能够得到一个StackBlitz可以完成你正在寻找的基础。 There ended up being some issues with actually refreshing the table when it came to pagination updates done via the component file, so I opted to change the dataSource.在通过组件文件完成分页更新时,实际刷新表格最终出现了一些问题,因此我选择更改数据源。

You should essentially be able to take the code shown in the changePageArray() method and implemented for your use case.您基本上应该能够采用changePageArray()方法中显示的代码并为您的用例实现。 Add logic where needed, and maybe refresh changeDetection if it isn't propagating to the ui.在需要的地方添加逻辑,如果没有传播到 ui,可能会刷新 changeDetection。

Hope this is what you were looking for, happy coding!希望这就是您正在寻找的,快乐的编码!

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

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