4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.PagingToolbar
14 * @extends Roo.Toolbar
15 * @children Roo.Toolbar.Item Roo.form.Field
16 * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
18 * Create a new PagingToolbar
19 * @param {Object} config The config object
21 Roo.PagingToolbar = function(el, ds, config)
23 // old args format still supported... - xtype is prefered..
24 if (typeof(el) == 'object' && el.xtype) {
25 // created from xtype...
28 el = config.container;
36 Roo.PagingToolbar.superclass.constructor.call(this, el, null, config);
39 this.renderButtons(this.el);
42 // supprot items array.
44 Roo.each(items, function(e) {
45 this.add(Roo.factory(e));
50 Roo.extend(Roo.PagingToolbar, Roo.Toolbar, {
52 * @cfg {Roo.data.Store} dataSource
53 * The underlying data store providing the paged data
56 * @cfg {String/HTMLElement/Element} container
57 * container The id or element that will contain the toolbar
60 * @cfg {Boolean} displayInfo
61 * True to display the displayMsg (defaults to false)
64 * @cfg {Number} pageSize
65 * The number of records to display per page (defaults to 20)
69 * @cfg {String} displayMsg
70 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
72 displayMsg : 'Displaying {0} - {1} of {2}',
74 * @cfg {String} emptyMsg
75 * The message to display when no records are found (defaults to "No data to display")
77 emptyMsg : 'No data to display',
79 * Customizable piece of the default paging text (defaults to "Page")
82 beforePageText : "Page",
84 * Customizable piece of the default paging text (defaults to "of %0")
87 afterPageText : "of {0}",
89 * Customizable piece of the default paging text (defaults to "First Page")
92 firstText : "First Page",
94 * Customizable piece of the default paging text (defaults to "Previous Page")
97 prevText : "Previous Page",
99 * Customizable piece of the default paging text (defaults to "Next Page")
102 nextText : "Next Page",
104 * Customizable piece of the default paging text (defaults to "Last Page")
107 lastText : "Last Page",
109 * Customizable piece of the default paging text (defaults to "Refresh")
112 refreshText : "Refresh",
115 renderButtons : function(el){
116 Roo.PagingToolbar.superclass.render.call(this, el);
117 this.first = this.addButton({
118 tooltip: this.firstText,
119 cls: "x-btn-icon x-grid-page-first",
121 handler: this.onClick.createDelegate(this, ["first"])
123 this.prev = this.addButton({
124 tooltip: this.prevText,
125 cls: "x-btn-icon x-grid-page-prev",
127 handler: this.onClick.createDelegate(this, ["prev"])
129 //this.addSeparator();
130 this.add(this.beforePageText);
131 this.field = Roo.get(this.addDom({
136 cls: "x-grid-page-number"
138 this.field.on("keydown", this.onPagingKeydown, this);
139 this.field.on("focus", function(){this.dom.select();});
140 this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
141 this.field.setHeight(18);
142 //this.addSeparator();
143 this.next = this.addButton({
144 tooltip: this.nextText,
145 cls: "x-btn-icon x-grid-page-next",
147 handler: this.onClick.createDelegate(this, ["next"])
149 this.last = this.addButton({
150 tooltip: this.lastText,
151 cls: "x-btn-icon x-grid-page-last",
153 handler: this.onClick.createDelegate(this, ["last"])
155 //this.addSeparator();
156 this.loading = this.addButton({
157 tooltip: this.refreshText,
158 cls: "x-btn-icon x-grid-loading",
159 handler: this.onClick.createDelegate(this, ["refresh"])
162 if(this.displayInfo){
163 this.displayEl = Roo.fly(this.el.dom.firstChild).createChild({cls:'x-paging-info'});
168 updateInfo : function(){
170 var count = this.ds.getCount();
171 var msg = count == 0 ?
175 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
177 this.displayEl.update(msg);
182 onLoad : function(ds, r, o){
183 this.cursor = o.params ? o.params.start : 0;
184 var d = this.getPageData(), ap = d.activePage, ps = d.pages;
186 this.afterTextEl.el.innerHTML = String.format(this.afterPageText, d.pages);
187 this.field.dom.value = ap;
188 this.first.setDisabled(ap == 1);
189 this.prev.setDisabled(ap == 1);
190 this.next.setDisabled(ap == ps);
191 this.last.setDisabled(ap == ps);
192 this.loading.enable();
197 getPageData : function(){
198 var total = this.ds.getTotalCount();
201 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
202 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
207 onLoadError : function(){
208 this.loading.enable();
212 onPagingKeydown : function(e){
214 var d = this.getPageData();
216 var v = this.field.dom.value, pageNum;
217 if(!v || isNaN(pageNum = parseInt(v, 10))){
218 this.field.dom.value = d.activePage;
221 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
222 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
225 else if(k == e.HOME || (k == e.UP && e.ctrlKey) || (k == e.PAGEUP && e.ctrlKey) || (k == e.RIGHT && e.ctrlKey) || k == e.END || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey))
227 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
228 this.field.dom.value = pageNum;
229 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
232 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
234 var v = this.field.dom.value, pageNum;
235 var increment = (e.shiftKey) ? 10 : 1;
236 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN) {
239 if(!v || isNaN(pageNum = parseInt(v, 10))) {
240 this.field.dom.value = d.activePage;
243 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
245 this.field.dom.value = parseInt(v, 10) + increment;
246 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
247 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
254 beforeLoad : function(){
256 this.loading.disable();
261 onClick : function(which){
265 ds.load({params:{start: 0, limit: this.pageSize}});
268 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
271 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
274 var total = ds.getTotalCount();
275 var extra = total % this.pageSize;
276 var lastStart = extra ? (total - extra) : total-this.pageSize;
277 ds.load({params:{start: lastStart, limit: this.pageSize}});
280 ds.load({params:{start: this.cursor, limit: this.pageSize}});
286 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
287 * @param {Roo.data.Store} store The data store to unbind
289 unbind : function(ds){
290 ds.un("beforeload", this.beforeLoad, this);
291 ds.un("load", this.onLoad, this);
292 ds.un("loadexception", this.onLoadError, this);
293 ds.un("remove", this.updateInfo, this);
294 ds.un("add", this.updateInfo, this);
299 * Binds the paging toolbar to the specified {@link Roo.data.Store}
300 * @param {Roo.data.Store} store The data store to bind
303 ds.on("beforeload", this.beforeLoad, this);
304 ds.on("load", this.onLoad, this);
305 ds.on("loadexception", this.onLoadError, this);
306 ds.on("remove", this.updateInfo, this);
307 ds.on("add", this.updateInfo, this);