PDA

View Full Version : Paging Toolbar with variable cursor base



extuzer
19 Feb 2008, 8:55 PM
I had a server where results were on a 1-based index hence the paging toolbar which assumes a 0-based index didn't display the page numbers right. I posted it here. http://extjs.com/forum/showthread.php?t=23651

Today I got some time to modify my code & I'm posting a user extension that takes in cursorBase as a configurable parameter. Its default value is 0 so it behaves like the original paging toolbar. However if your server starts results from 1 (or any number n), specify that as the cursor base in configuration. Internally the cursor is still 0-based but at the interface where it receives or sends parameters to store, cursorBase is adjusted.

Please let me know if you see any problems.



Ext.namespace('Ext.ux');

/**
* Extend the paging toolbar with a variable cursor base
*/
Ext.ux.PagingToolbar = Ext.extend(Ext.PagingToolbar, {

/*
* 0 or 1 based page cursor (Actually, any +ve integer is ok).
* If index of first result (i.e. 'start' parameter) is 1, set cursorBase to 1 in config.
*/
cursorBase : 0,

initComponent : function(){
Ext.ux.PagingToolbar.superclass.initComponent.call(this);
},

// private
onLoad : function(store, r, o){
if(!this.rendered){
this.dsLoaded = [store, r, o];
return;
}
this.cursor = o.params ? o.params[this.paramNames.start] - this.cursorBase : 0;
var d = this.getPageData(), ap = d.activePage, ps = d.pages;

this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
this.field.dom.value = ap;
this.first.setDisabled(ap == 1);
this.prev.setDisabled(ap == 1);
this.next.setDisabled(ap == ps);
this.last.setDisabled(ap == ps);
this.loading.enable();
this.updateInfo();
},

doLoad : function(start){
var o = {}, pn = this.paramNames;
o[pn.start] = start + this.cursorBase;
o[pn.limit] = this.pageSize;
this.store.load({params:o});
}
});

Ext.reg('ux-paging', Ext.ux.PagingToolbar);