In knockout Change Paging when Data is changed

眉间皱痕 提交于 2019-12-11 13:27:09

问题


In knockout I wish to change/ refresh Paging when data is changed. For example: I have a data set of 100[5 Records per Page] and I am on Page 10. Now with some more search Data is changed from 100 to 5 and I am on Page 10, but when data is changed I want paging to be on First page. i.e Paging to be refreshed.

Here is my [Fiddle] (https://jsfiddle.net/975ncawv/281/)

MyVM.prototype.loadData = function(rawData) {
  this.items(rawData.map(RowModel.fromRawDataPoint));
};
ko.applyBindings(new MyVM(myData));

回答1:


Two things need to be changed inside the loadData function.

  1. The all observableArray needs to be updated with the new values of items, since it gets the values in a similar fashion in line 65.
  2. pageNumber needs to be reset to 0, since paginated and all are listening to it through the computed function. (And the table is displaying the values of paginated).

It would look like this updated fiddle.




回答2:


but when data is changed

this.items.subscribe( /* ... */ )

I want paging to be on First page

this.pageNumber(0)

Put together:

this.items.subscribe(function() {
  this.pageNumber(0);
}, this);

I don't see any logic concerning the search data, but I'd expect something along the lines of:

this.filteredItems = ko.pureComputed(function() {
  return this.items().filter(/* ... */);
}, this);

Once implemented, you can change the subscribe to items to be on filteredItems to make sure you reset both when the data source changes as well as when the search query changes.



来源:https://stackoverflow.com/questions/51780997/in-knockout-change-paging-when-data-is-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!