问题
I have a store that is binded to grid. When it is loaded it has n pages.
Now I want to show the page that contains certain value. In order to do that I use store.load({params: {'file': 'file33939'}});
(the 'file' is used because grid shows list of files, but it's irrelevant to question). At this moment I don't know what the page it should be. The response looks like:
{
"files":[{"id":"33939", "name": "file33939"}, /* ... */],
"total": 1000,
"page": 13
}
Grid displays correct data (the page that really contains 'file33939'). However pagingtoolbar, grid's rownumberer and store.indexOfTotal() behave as if the first page was loaded (instead of 13).
How can I "tell" store that the page that store just had loaded is 13?
回答1:
To change the loaded page you should not use store.load()
. You should find a way to get the page number for the given data (e.g. asking the server via an ajax request) and then update the page via the pagingtoolbar.move(pageNumber)
. This will update your pagingtoolbar along with the grid and the store and everything remains in sync.
回答2:
I've found suitable solution. It's not the most elegant solution. However it works.
After digging in sencha code I've found that pagingtoolbar and store.indexOfTotal() were using record.index
(which in my case was set as if the first page was loaded). record.index
is set by store in function store.loadRecords
:
loadRecords: function(records, options) {
// ...
//FIXME: this is not a good solution. Ed Spencer is totally responsible for this and should be forced to fix it immediately.
for (; i < length; i++) {
if (options.start !== undefined) {
records[i].index = options.start + i;
}
// ...
},
The FIXME comment is not mine. It was in the code.
The ideal solution would be to find an event which is being fired after the response came and before the loadRecords was called and override options
in the handler. However I haven't found such event.
But you always can override loadRecords:
Ext.define('MyApp.store.MyStore', {
extend : 'Ext.data.Store',
// ...
loadRecords : function(records, options) {
if (this.proxy.reader.rawData.page) {
var page = this.proxy.reader.rawData.page;
// This is for pagingtoolbar
this.currentPage = page;
// The following is for rownumberer and correct index behaviour
options.page = page;
options.start = (page-1)*options.limit;
}
this.callParent(arguments);
},
// ...
});
来源:https://stackoverflow.com/questions/7498987/how-to-tell-store-what-page-was-loaded