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.bootstrap.PagingToolbar
15 * A specialized toolbar that is bound to a {@link Roo.data.Store} and provides automatic paging controls.
17 * Create a new PagingToolbar
18 * @param {Object} config The config object
20 Roo.bootstrap.PagingToolbar = function(config)
22 // old args format still supported... - xtype is prefered..
23 // created from xtype...
25 var ds = config.dataSource;
27 if (this.store && !ds) {
28 this.store= Roo.factory(this.store, Roo.data);
30 ds.xmodule = this.xmodule || false;
33 this.toolbarItems = [];
35 this.toolbarItems = config.items;
39 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
46 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
50 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
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",
116 onRender : function(ct, position)
118 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
119 this.navgroup.parentId = this.id;
120 this.navgroup.onRender(this.el, null);
121 // add the buttons to the navgroup
123 if(this.displayInfo){
124 Roo.log(this.el.select('ul.navbar-nav',true).first());
125 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
126 this.displayEl = this.el.select('.x-paging-info', true).first();
128 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
129 // this.displayEl = navel.el.select('span',true).first();
135 Roo.each(_this.buttons, function(e){
136 Roo.factory(e).onRender(_this.el, null);
140 Roo.each(_this.toolbarItems, function(e) {
141 _this.navgroup.addItem(e);
145 this.first = this.navgroup.addItem({
146 tooltip: this.firstText,
148 icon : 'fa fa-backward',
150 preventDefault: true,
151 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
154 this.prev = this.navgroup.addItem({
155 tooltip: this.prevText,
157 icon : 'fa fa-step-backward',
159 preventDefault: true,
160 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
162 //this.addSeparator();
165 var field = this.navgroup.addItem( {
167 cls : 'x-paging-position',
169 html : this.beforePageText +
170 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
171 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
174 this.field = field.el.select('input', true).first();
175 this.field.on("keydown", this.onPagingKeydown, this);
176 this.field.on("focus", function(){this.dom.select();});
179 this.afterTextEl = field.el.select('.x-paging-after',true).first();
180 //this.field.setHeight(18);
181 //this.addSeparator();
182 this.next = this.navgroup.addItem({
183 tooltip: this.nextText,
185 html : ' <i class="fa fa-step-forward">',
187 preventDefault: true,
188 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
190 this.last = this.navgroup.addItem({
191 tooltip: this.lastText,
192 icon : 'fa fa-forward',
195 preventDefault: true,
196 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
198 //this.addSeparator();
199 this.loading = this.navgroup.addItem({
200 tooltip: this.refreshText,
201 icon: 'fa fa-refresh',
202 preventDefault: true,
203 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
209 updateInfo : function(){
211 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
212 var msg = count == 0 ?
216 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
218 this.displayEl.update(msg);
223 onLoad : function(ds, r, o){
224 this.cursor = o.params ? o.params.start : 0;
225 var d = this.getPageData(),
229 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
230 this.field.dom.value = ap;
231 this.first.setDisabled(ap == 1);
232 this.prev.setDisabled(ap == 1);
233 this.next.setDisabled(ap == ps);
234 this.last.setDisabled(ap == ps);
235 this.loading.enable();
240 getPageData : function(){
241 var total = this.ds.getTotalCount();
244 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
245 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
250 onLoadError : function(){
251 this.loading.enable();
255 onPagingKeydown : function(e){
257 var d = this.getPageData();
259 var v = this.field.dom.value, pageNum;
260 if(!v || isNaN(pageNum = parseInt(v, 10))){
261 this.field.dom.value = d.activePage;
264 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
265 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
268 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))
270 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
271 this.field.dom.value = pageNum;
272 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
275 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
277 var v = this.field.dom.value, pageNum;
278 var increment = (e.shiftKey) ? 10 : 1;
279 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
281 if(!v || isNaN(pageNum = parseInt(v, 10))) {
282 this.field.dom.value = d.activePage;
285 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
287 this.field.dom.value = parseInt(v, 10) + increment;
288 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
289 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
296 beforeLoad : function(){
298 this.loading.disable();
303 onClick : function(which){
312 ds.load({params:{start: 0, limit: this.pageSize}});
315 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
318 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
321 var total = ds.getTotalCount();
322 var extra = total % this.pageSize;
323 var lastStart = extra ? (total - extra) : total-this.pageSize;
324 ds.load({params:{start: lastStart, limit: this.pageSize}});
327 ds.load({params:{start: this.cursor, limit: this.pageSize}});
333 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
334 * @param {Roo.data.Store} store The data store to unbind
336 unbind : function(ds){
337 ds.un("beforeload", this.beforeLoad, this);
338 ds.un("load", this.onLoad, this);
339 ds.un("loadexception", this.onLoadError, this);
340 ds.un("remove", this.updateInfo, this);
341 ds.un("add", this.updateInfo, this);
346 * Binds the paging toolbar to the specified {@link Roo.data.Store}
347 * @param {Roo.data.Store} store The data store to bind
350 ds.on("beforeload", this.beforeLoad, this);
351 ds.on("load", this.onLoad, this);
352 ds.on("loadexception", this.onLoadError, this);
353 ds.on("remove", this.updateInfo, this);
354 ds.on("add", this.updateInfo, this);