Okay, I've investigated the option would be best for you, but your code behind logic itself limits it. I am not sure I should fiddle with your data source simulation -- I might diverge from your actual use case.
That said, I will give you directions on how to allow the page to send the actual page the user asked for and how to check back (once the result is received) which page you actually received (to update in the view):
The override will then have to get a new treat for when hitting the "enter" key. This is just the start. After this is working, you'd have to also adjust page up and page down keys:
Ext.define('Ext.toolbar.Paging', {
override: 'Ext.toolbar.Paging',
moveLast: function () {
this.store.lastPageRequested = true;
return this.callParent(arguments);
},
onPagingKeyDown: function (field, e) {
var me = this,
k = e.getKey(),
pageData = me.getPageData(),
pageNum;
if (k == e.RETURN) {
e.stopEvent();
pageNum = me.readPageFromInput(pageData);
if (pageNum !== false) {
this.store.attemptBeyondLastPage = pageNum > pageData.pageCount;
if (me.fireEvent('beforechange', me, pageNum) !== false) {
me.store.loadPage(pageNum);
}
}
} else {
// if not our custom-treated key, then call original method to handle other keys
me.callParent(arguments);
}
}
});
The other keys that switch pages would be treated similarly, just get the original code for this method and then adapt it.
This is the script in the "before request" part.
Now for the "during request" part, I've added an additional row to the result (which is not displayed in the grid at all). Here are the changes I made on your
getObjects2
method:
var pageReturned = parameters.Page;
(...)
if (start + limit > totalRecordCount) { // not changed -- just for reference
(...)
pageReturned = (totalRecordCount - (totalRecordCount % parameters.Limit)) / parameters.Limit;
}
(...)
var lastPageSpecialRow = new Dictionary<string, string>();
lastPageSpecialRow.Add("returnedPage",pageReturned); // get the actual last page, instead of a fixed '2'
objects.Add(lastPageSpecialRow);
return this.Store(new Paging<Dictionary<string, string>>(objects, totalRecordCount)); // not changed -- just for reference
And then, upon store reload (the "after part of the history"), we have to chew this new data. So we change the store handler function like this:
var storeLoadHandler = function (store, records, successful) {
if (!store.orgTotalCount) {
store.orgTotalCount = store.totalCount;
} else {
var skBeyondLast = store.lastPageRequested || store.attemptBeyondLastPage;
if (store.totalCount != store.orgTotalCount && skBeyondLast) {
store.orgTotalCount = store.totalCount;
if (store.lastPageRequested) {
// Get +1 page if the count does not exactly divides to the amount per page
var lastPage = Math.ceil(store.totalCount / store.pageSize);
// Force switching to last page, enforcing the 'last page' command.
if (store.currentPage != lastPage) {
store.loadPage(lastPage);
}
} else { // it can only be attemptBeyondLastPage
var storeItems = store.data.items,
lastItem = storeItems[storeItems.length - 1],
returnedPage = parseInt(lastItem.raw.returnedPage);
// If we really got the returnedPage value from the request
if (!isNaN(returnedPage)) {
// And the page is not really the same page we requested
if (returnedPage != store.currentPage) {
// This is just being pragmatic, far from optimal:
store.loadPage(returnedPage);
// It would rather start from here:
// Update the value of current page in the field
//App.pt1.getInputItem().setValue(returnedPage);
// Then update the value of 'Displaying objects .. - .. of ..
}
}
}
}
}
At this point, the code is working like your option '2'. If anything beyond the currently known last page is asked, it gets the last page. But the limitation is actually on the code behind part. It is now up to you whether you can or cannot handle your option 3.
The problem is that your code behind logic also does not know how many pages it actually has before calling getLastPage.
This part of the code gets triggered and then it always gets the last page if I choose anything beyond the current known last page and before actual (random) last page). For example, if I choose page '4'.
totalRecordCount = (int)TempData["recordCount"];
pagingQuery = new DataModel2(start, limit);
if (start + limit > totalRecordCount)
{
objects = pagingQuery.LastPage();
totalRecordCount = pagingQuery.RecordCount;
pageReturned = (totalRecordCount - (totalRecordCount % parameters.Limit)) / parameters.Limit;
}
I'm not sure if your actual scenario allows you to evaluate the actual
totalRecordCount
before making the test. I may have changed your controller code to this extent but I would just be giving long shots that may not be useful.
So, again, it is up to you whether you can or cannot get the total amount or records before that test and accurately fetch the requested page. If you can stick to option 2 or deepen your work to have it actually jump to an existing page if possible.
One possibility I'd think about was if
DataModel2
had means to get the updated totalRecordCount up front. This might though, trouble that behavior (that I didn't pay too much attention to so far) you have when you move one page by a time (that keeps stacking up total amount of data, increasing last page every 'next page' you click).
Well, I hope the text is not very boring to read :P.. and also hope it helps!