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...
24 var ds = config.dataSource;
25 this.toolbarItems = [];
27 this.toolbarItems = config.items;
31 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
38 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
42 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
44 * @cfg {Roo.data.Store} dataSource
45 * The underlying data store providing the paged data
48 * @cfg {String/HTMLElement/Element} container
49 * container The id or element that will contain the toolbar
52 * @cfg {Boolean} displayInfo
53 * True to display the displayMsg (defaults to false)
56 * @cfg {Number} pageSize
57 * The number of records to display per page (defaults to 20)
61 * @cfg {String} displayMsg
62 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
64 displayMsg : 'Displaying {0} - {1} of {2}',
66 * @cfg {String} emptyMsg
67 * The message to display when no records are found (defaults to "No data to display")
69 emptyMsg : 'No data to display',
71 * Customizable piece of the default paging text (defaults to "Page")
74 beforePageText : "Page",
76 * Customizable piece of the default paging text (defaults to "of %0")
79 afterPageText : "of {0}",
81 * Customizable piece of the default paging text (defaults to "First Page")
84 firstText : "First Page",
86 * Customizable piece of the default paging text (defaults to "Previous Page")
89 prevText : "Previous Page",
91 * Customizable piece of the default paging text (defaults to "Next Page")
94 nextText : "Next Page",
96 * Customizable piece of the default paging text (defaults to "Last Page")
99 lastText : "Last Page",
101 * Customizable piece of the default paging text (defaults to "Refresh")
104 refreshText : "Refresh",
108 onRender : function(ct, position)
110 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
111 this.navgroup.parentId = this.id;
112 this.navgroup.onRender(this.el, null);
113 // add the buttons to the navgroup
115 if(this.displayInfo){
116 Roo.log(this.el.select('ul.navbar-nav',true).first());
117 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
118 this.displayEl = this.el.select('.x-paging-info', true).first();
119 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
120 // this.displayEl = navel.el.select('span',true).first();
126 Roo.each(_this.buttons, function(e){
127 Roo.factory(e).onRender(_this.el, null);
131 Roo.each(_this.toolbarItems, function(e) {
132 _this.navgroup.addItem(e);
135 this.first = this.navgroup.addItem({
136 tooltip: this.firstText,
138 icon : 'fa fa-backward',
140 listeners : { click : this.onClick.createDelegate(this, ["first"], true) }
143 this.prev = this.navgroup.addItem({
144 tooltip: this.prevText,
146 icon : 'fa fa-step-backward',
148 listeners : { click : this.onClick.createDelegate(this, ["prev"], true) }
150 //this.addSeparator();
153 var field = this.navgroup.addItem( {
155 cls : 'x-paging-position',
157 html : this.beforePageText +
158 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
159 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
162 this.field = field.el.select('input', true).first();
163 this.field.on("keydown", this.onPagingKeydown, this);
164 this.field.on("focus", function(){this.dom.select();});
167 this.afterTextEl = field.el.select('.x-paging-after',true).first();
168 //this.field.setHeight(18);
169 //this.addSeparator();
170 this.next = this.navgroup.addItem({
171 tooltip: this.nextText,
173 html : ' <i class="fa fa-step-forward">',
175 listeners : { click : this.onClick.createDelegate(this, ["next"], true) }
177 this.last = this.navgroup.addItem({
178 tooltip: this.lastText,
179 icon : 'fa fa-forward',
182 listeners : { click : this.onClick.createDelegate(this, ["last"], true) }
184 //this.addSeparator();
185 this.loading = this.navgroup.addItem({
186 tooltip: this.refreshText,
187 icon: 'fa fa-refresh',
189 listeners : { click : this.onClick.createDelegate(this, ["refresh"], true) }
195 updateInfo : function(){
197 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
198 var msg = count == 0 ?
202 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
204 this.displayEl.update(msg);
209 onLoad : function(ds, r, o){
210 this.cursor = o.params ? o.params.start : 0;
211 var d = this.getPageData(),
215 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
216 this.field.dom.value = ap;
217 this.first.setDisabled(ap == 1);
218 this.prev.setDisabled(ap == 1);
219 this.next.setDisabled(ap == ps);
220 this.last.setDisabled(ap == ps);
221 this.loading.enable();
226 getPageData : function(){
227 var total = this.ds.getTotalCount();
230 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
231 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
236 onLoadError : function(){
237 this.loading.enable();
241 onPagingKeydown : function(e){
243 var d = this.getPageData();
245 var v = this.field.dom.value, pageNum;
246 if(!v || isNaN(pageNum = parseInt(v, 10))){
247 this.field.dom.value = d.activePage;
250 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
251 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
254 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))
256 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
257 this.field.dom.value = pageNum;
258 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
261 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
263 var v = this.field.dom.value, pageNum;
264 var increment = (e.shiftKey) ? 10 : 1;
265 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
267 if(!v || isNaN(pageNum = parseInt(v, 10))) {
268 this.field.dom.value = d.activePage;
271 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
273 this.field.dom.value = parseInt(v, 10) + increment;
274 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
275 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
282 beforeLoad : function(){
284 this.loading.disable();
289 onClick : function(o, e, which){
291 Roo.log('onclick!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
293 if(typeof(e) == 'undefined'){
307 ds.load({params:{start: 0, limit: this.pageSize}});
310 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
313 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
316 var total = ds.getTotalCount();
317 var extra = total % this.pageSize;
318 var lastStart = extra ? (total - extra) : total-this.pageSize;
319 ds.load({params:{start: lastStart, limit: this.pageSize}});
322 ds.load({params:{start: this.cursor, limit: this.pageSize}});
328 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
329 * @param {Roo.data.Store} store The data store to unbind
331 unbind : function(ds){
332 ds.un("beforeload", this.beforeLoad, this);
333 ds.un("load", this.onLoad, this);
334 ds.un("loadexception", this.onLoadError, this);
335 ds.un("remove", this.updateInfo, this);
336 ds.un("add", this.updateInfo, this);
341 * Binds the paging toolbar to the specified {@link Roo.data.Store}
342 * @param {Roo.data.Store} store The data store to bind
345 ds.on("beforeload", this.beforeLoad, this);
346 ds.on("load", this.onLoad, this);
347 ds.on("loadexception", this.onLoadError, this);
348 ds.on("remove", this.updateInfo, this);
349 ds.on("add", this.updateInfo, this);