warstar
8 Aug 2007, 11:43 PM
Hi everyone,
You all know the toolbar pager for the grid i asume. Well it's good but on great for what i needed so with a bit of copy pasting and (a very small part of) brains i created the following numeric pager. I'm sure it can be made better but i don't have the time or know how about ExtJs. Hope some one finds this usefull.
Good luck to everyone
/*
* NumberPagingToolbar (Extends Toolbar)
*
* Created on: 28 jun 2007
* Created by: Warnar Boekkooi
*/
Ext.NumberPagingToolbar=function(el,ds,config){
Ext.NumberPagingToolbar.superclass.constructor.call(this, el, null, config);
this.ds = ds;
this.pageIndex = 0;
this.renderNumbers(this.el);
this.bind(ds);
}
Ext.extend(Ext.NumberPagingToolbar, Ext.Toolbar, {
/**
* @cfg {Number} pageSize
* The number of records to display per page (defaults to 20)
*/
pageSize: 20,
/**
* @cfg {Number} maxLinks
* max. number of links to show on one page (default: 5)
*/
maxLinks: 5,
/**
* @cfg {Boolean} showFirst
* Show go to first page link (default: false)
*/
showFirst: false,
/**
* @cfg {String} firstText
* Text for go to first page (defaults to "First Page")
*/
firstText: "<<<",
/**
* @cfg {Boolean} showLast
* Show go to last page link (default: false)
*/
showLast: false,
/**
* @cfg {String} lastText
* Text for go to last page (defaults to "First Page")
*/
lastText : ">>>",
renderNumbers: function(el) {
Ext.NumberPagingToolbar.superclass.render.call(this, el);
this.field = Ext.get(this.addDom({
tag: "div",
cls: "x-grid-page-numbers"
}).el);
},
setMaxLinks: function(amount) {
amount = Number(amount);
if(!isNaN(amount)) {
this.maxLinks = amount;
}
},
//build the pager
onLoad : function(ds, r, o){
var dh = Ext.DomHelper;
//clear number items
this.field.dom.innerHTML = "";
// get the currentPage and the total numberOfPages
var d = this.getPageData();
var currentPage= d.activePage;
var numberOfPages=d.pages;
// hide numbers if there is only 1 page
if(numberOfPages == 1) {
return;
}
// how mutch links maybe on the left and how mutch on the right
var amount_l = 0;
var amount_r = 0
if (this.maxLinks % 2 == 0) {
amount_l = (this.maxLinks / 2 ) - 1;
amount_r = this.maxLinks / 2;
} else {
amount_l = amount_r = (this.maxLinks - 1) / 2;
}
//how validate the left and right amounts
if(numberOfPages <= this.maxLinks) { // do we need to slide?
amount_l = 0;
amount_r = numberOfPages-1;
} else if(currentPage == 0) { //is this the first page?
amount_l = 0;
amount_r = this.maxLinks-1;
} else {
var dif = 0;
if(0 > (currentPage - amount_l)) { //to far left?
amount_l = currentPage+(amount_l-currentPage);
amount_r = this.maxLinks-amount_l;
}
if((numberOfPages-1) < (currentPage + amount_r)) { //to far right?
amount_r = (numberOfPages-1) - currentPage;
amount_l = this.maxLinks-amount_r-1;
}
}
// now create the links
if(this.showFirst) {
if(currentPage != 0) {
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-first",
children:[
{tag:'a' ,href:'javascript:void(0);',html:this.firstText, cls:'x-grid-page-first-link'}
]
},
true);
el.on("click",this.onClick.createDelegate(this, ["first"]));
} else {
var el = dh.append(this.field,
{
tag: 'div',
html: this.firstText,
cls: "x-grid-page-first"
},
true);
}
}
for (i = (currentPage - amount_l); i <= (currentPage + amount_r); i++) {
//number page
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-number",
children:[
{tag:'a' ,href:'javascript:void(0);',html:i+1, cls:'x-grid-page-link'}
]
},
true);
if(i == currentPage){
el.addClass("x-grid-page-number-current");
}
el.on("click",this.onClick.createDelegate(this, [i]));
}
if(this.showLast) {
if(currentPage != (numberOfPages-1)) {
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-last",
children:[
{tag:'a' ,href:'javascript:void(0);',html:this.lastText, cls:'x-grid-page-last-link'}
]
},
true);
el.on("click",this.onClick.createDelegate(this, ["last"]));
} else {
var el = dh.append(this.field,
{
tag: 'div',
html: this.lastText,
cls: "x-grid-page-last"
},
true);
}
}
},
//called on the page number click
onClick:function(which){
pageNum = Number(which);
if(!isNaN(pageNum)) {
var ds = this.ds;
this.pageIndex = pageNum;
ds.load({params:{start: (this.pageIndex)*this.pageSize, limit: this.pageSize}});
return;
}
switch(which){
case "first":
this.pageIndex = 0;
this.ds.load({params:{start: 0, limit: this.pageSize}});
break;
case "last":
this.pageIndex = this.getPageData().pages-1;
this.ds.load({params:{start: (this.pageIndex)*this.pageSize, limit: this.pageSize}});
break;
}
},
// private
getPageData : function(){
var total = this.ds.getTotalCount();
return {
total : total,
activePage : this.pageIndex,
pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
};
},
/**
* Unbinds the paging toolbar from the specified {@link Ext.data.Store}
* @param {Ext.data.Store} store The data store to unbind
*/
unbind : function(ds){
ds.un("load", this.onLoad, this);
this.ds = undefined;
},
/**
* Binds the paging toolbar to the specified {@link Ext.data.Store}
* @param {Ext.data.Store} store The data store to bind
*/
bind : function(ds){
ds.on("load", this.onLoad, this);
this.ds = ds;
}
});
You all know the toolbar pager for the grid i asume. Well it's good but on great for what i needed so with a bit of copy pasting and (a very small part of) brains i created the following numeric pager. I'm sure it can be made better but i don't have the time or know how about ExtJs. Hope some one finds this usefull.
Good luck to everyone
/*
* NumberPagingToolbar (Extends Toolbar)
*
* Created on: 28 jun 2007
* Created by: Warnar Boekkooi
*/
Ext.NumberPagingToolbar=function(el,ds,config){
Ext.NumberPagingToolbar.superclass.constructor.call(this, el, null, config);
this.ds = ds;
this.pageIndex = 0;
this.renderNumbers(this.el);
this.bind(ds);
}
Ext.extend(Ext.NumberPagingToolbar, Ext.Toolbar, {
/**
* @cfg {Number} pageSize
* The number of records to display per page (defaults to 20)
*/
pageSize: 20,
/**
* @cfg {Number} maxLinks
* max. number of links to show on one page (default: 5)
*/
maxLinks: 5,
/**
* @cfg {Boolean} showFirst
* Show go to first page link (default: false)
*/
showFirst: false,
/**
* @cfg {String} firstText
* Text for go to first page (defaults to "First Page")
*/
firstText: "<<<",
/**
* @cfg {Boolean} showLast
* Show go to last page link (default: false)
*/
showLast: false,
/**
* @cfg {String} lastText
* Text for go to last page (defaults to "First Page")
*/
lastText : ">>>",
renderNumbers: function(el) {
Ext.NumberPagingToolbar.superclass.render.call(this, el);
this.field = Ext.get(this.addDom({
tag: "div",
cls: "x-grid-page-numbers"
}).el);
},
setMaxLinks: function(amount) {
amount = Number(amount);
if(!isNaN(amount)) {
this.maxLinks = amount;
}
},
//build the pager
onLoad : function(ds, r, o){
var dh = Ext.DomHelper;
//clear number items
this.field.dom.innerHTML = "";
// get the currentPage and the total numberOfPages
var d = this.getPageData();
var currentPage= d.activePage;
var numberOfPages=d.pages;
// hide numbers if there is only 1 page
if(numberOfPages == 1) {
return;
}
// how mutch links maybe on the left and how mutch on the right
var amount_l = 0;
var amount_r = 0
if (this.maxLinks % 2 == 0) {
amount_l = (this.maxLinks / 2 ) - 1;
amount_r = this.maxLinks / 2;
} else {
amount_l = amount_r = (this.maxLinks - 1) / 2;
}
//how validate the left and right amounts
if(numberOfPages <= this.maxLinks) { // do we need to slide?
amount_l = 0;
amount_r = numberOfPages-1;
} else if(currentPage == 0) { //is this the first page?
amount_l = 0;
amount_r = this.maxLinks-1;
} else {
var dif = 0;
if(0 > (currentPage - amount_l)) { //to far left?
amount_l = currentPage+(amount_l-currentPage);
amount_r = this.maxLinks-amount_l;
}
if((numberOfPages-1) < (currentPage + amount_r)) { //to far right?
amount_r = (numberOfPages-1) - currentPage;
amount_l = this.maxLinks-amount_r-1;
}
}
// now create the links
if(this.showFirst) {
if(currentPage != 0) {
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-first",
children:[
{tag:'a' ,href:'javascript:void(0);',html:this.firstText, cls:'x-grid-page-first-link'}
]
},
true);
el.on("click",this.onClick.createDelegate(this, ["first"]));
} else {
var el = dh.append(this.field,
{
tag: 'div',
html: this.firstText,
cls: "x-grid-page-first"
},
true);
}
}
for (i = (currentPage - amount_l); i <= (currentPage + amount_r); i++) {
//number page
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-number",
children:[
{tag:'a' ,href:'javascript:void(0);',html:i+1, cls:'x-grid-page-link'}
]
},
true);
if(i == currentPage){
el.addClass("x-grid-page-number-current");
}
el.on("click",this.onClick.createDelegate(this, [i]));
}
if(this.showLast) {
if(currentPage != (numberOfPages-1)) {
var el = dh.append(this.field,
{
tag:'div',
cls: "x-grid-page-last",
children:[
{tag:'a' ,href:'javascript:void(0);',html:this.lastText, cls:'x-grid-page-last-link'}
]
},
true);
el.on("click",this.onClick.createDelegate(this, ["last"]));
} else {
var el = dh.append(this.field,
{
tag: 'div',
html: this.lastText,
cls: "x-grid-page-last"
},
true);
}
}
},
//called on the page number click
onClick:function(which){
pageNum = Number(which);
if(!isNaN(pageNum)) {
var ds = this.ds;
this.pageIndex = pageNum;
ds.load({params:{start: (this.pageIndex)*this.pageSize, limit: this.pageSize}});
return;
}
switch(which){
case "first":
this.pageIndex = 0;
this.ds.load({params:{start: 0, limit: this.pageSize}});
break;
case "last":
this.pageIndex = this.getPageData().pages-1;
this.ds.load({params:{start: (this.pageIndex)*this.pageSize, limit: this.pageSize}});
break;
}
},
// private
getPageData : function(){
var total = this.ds.getTotalCount();
return {
total : total,
activePage : this.pageIndex,
pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
};
},
/**
* Unbinds the paging toolbar from the specified {@link Ext.data.Store}
* @param {Ext.data.Store} store The data store to unbind
*/
unbind : function(ds){
ds.un("load", this.onLoad, this);
this.ds = undefined;
},
/**
* Binds the paging toolbar to the specified {@link Ext.data.Store}
* @param {Ext.data.Store} store The data store to bind
*/
bind : function(ds){
ds.on("load", this.onLoad, this);
this.ds = ds;
}
});