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;
28 if (this.store && !ds) {
30 this.store= Roo.factory(this.store, Roo.data);
32 ds.xmodule = this.xmodule || false;
35 this.toolbarItems = [];
37 this.toolbarItems = config.items;
41 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
49 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
53 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
55 * @cfg {Roo.data.Store} dataSource
56 * The underlying data store providing the paged data
59 * @cfg {String/HTMLElement/Element} container
60 * container The id or element that will contain the toolbar
63 * @cfg {Boolean} displayInfo
64 * True to display the displayMsg (defaults to false)
67 * @cfg {Number} pageSize
68 * The number of records to display per page (defaults to 20)
72 * @cfg {String} displayMsg
73 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
75 displayMsg : 'Displaying {0} - {1} of {2}',
77 * @cfg {String} emptyMsg
78 * The message to display when no records are found (defaults to "No data to display")
80 emptyMsg : 'No data to display',
82 * Customizable piece of the default paging text (defaults to "Page")
85 beforePageText : "Page",
87 * Customizable piece of the default paging text (defaults to "of %0")
90 afterPageText : "of {0}",
92 * Customizable piece of the default paging text (defaults to "First Page")
95 firstText : "First Page",
97 * Customizable piece of the default paging text (defaults to "Previous Page")
100 prevText : "Previous Page",
102 * Customizable piece of the default paging text (defaults to "Next Page")
105 nextText : "Next Page",
107 * Customizable piece of the default paging text (defaults to "Last Page")
110 lastText : "Last Page",
112 * Customizable piece of the default paging text (defaults to "Refresh")
115 refreshText : "Refresh",
119 onRender : function(ct, position)
121 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
122 this.navgroup.parentId = this.id;
123 this.navgroup.onRender(this.el, null);
124 // add the buttons to the navgroup
126 if(this.displayInfo){
127 Roo.log(this.el.select('ul.navbar-nav',true).first());
128 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
129 this.displayEl = this.el.select('.x-paging-info', true).first();
130 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
131 // this.displayEl = navel.el.select('span',true).first();
137 Roo.each(_this.buttons, function(e){
138 Roo.factory(e).onRender(_this.el, null);
142 Roo.each(_this.toolbarItems, function(e) {
143 _this.navgroup.addItem(e);
147 this.first = this.navgroup.addItem({
148 tooltip: this.firstText,
150 icon : 'fa fa-backward',
152 preventDefault: true,
153 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
156 this.prev = this.navgroup.addItem({
157 tooltip: this.prevText,
159 icon : 'fa fa-step-backward',
161 preventDefault: true,
162 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
164 //this.addSeparator();
167 var field = this.navgroup.addItem( {
169 cls : 'x-paging-position',
171 html : this.beforePageText +
172 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
173 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
176 this.field = field.el.select('input', true).first();
177 this.field.on("keydown", this.onPagingKeydown, this);
178 this.field.on("focus", function(){this.dom.select();});
181 this.afterTextEl = field.el.select('.x-paging-after',true).first();
182 //this.field.setHeight(18);
183 //this.addSeparator();
184 this.next = this.navgroup.addItem({
185 tooltip: this.nextText,
187 html : ' <i class="fa fa-step-forward">',
189 preventDefault: true,
190 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
192 this.last = this.navgroup.addItem({
193 tooltip: this.lastText,
194 icon : 'fa fa-forward',
197 preventDefault: true,
198 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
200 //this.addSeparator();
201 this.loading = this.navgroup.addItem({
202 tooltip: this.refreshText,
203 icon: 'fa fa-refresh',
204 preventDefault: true,
205 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
211 updateInfo : function(){
213 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
214 var msg = count == 0 ?
218 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
220 this.displayEl.update(msg);
225 onLoad : function(ds, r, o){
226 this.cursor = o.params ? o.params.start : 0;
227 var d = this.getPageData(),
231 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
232 this.field.dom.value = ap;
233 this.first.setDisabled(ap == 1);
234 this.prev.setDisabled(ap == 1);
235 this.next.setDisabled(ap == ps);
236 this.last.setDisabled(ap == ps);
237 this.loading.enable();
242 getPageData : function(){
243 var total = this.ds.getTotalCount();
246 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
247 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
252 onLoadError : function(){
253 this.loading.enable();
257 onPagingKeydown : function(e){
259 var d = this.getPageData();
261 var v = this.field.dom.value, pageNum;
262 if(!v || isNaN(pageNum = parseInt(v, 10))){
263 this.field.dom.value = d.activePage;
266 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
267 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
270 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))
272 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
273 this.field.dom.value = pageNum;
274 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
277 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
279 var v = this.field.dom.value, pageNum;
280 var increment = (e.shiftKey) ? 10 : 1;
281 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
283 if(!v || isNaN(pageNum = parseInt(v, 10))) {
284 this.field.dom.value = d.activePage;
287 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
289 this.field.dom.value = parseInt(v, 10) + increment;
290 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
291 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
298 beforeLoad : function(){
300 this.loading.disable();
305 onClick : function(which){
314 ds.load({params:{start: 0, limit: this.pageSize}});
317 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
320 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
323 var total = ds.getTotalCount();
324 var extra = total % this.pageSize;
325 var lastStart = extra ? (total - extra) : total-this.pageSize;
326 ds.load({params:{start: lastStart, limit: this.pageSize}});
329 ds.load({params:{start: this.cursor, limit: this.pageSize}});
335 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
336 * @param {Roo.data.Store} store The data store to unbind
338 unbind : function(ds){
339 ds.un("beforeload", this.beforeLoad, this);
340 ds.un("load", this.onLoad, this);
341 ds.un("loadexception", this.onLoadError, this);
342 ds.un("remove", this.updateInfo, this);
343 ds.un("add", this.updateInfo, this);
348 * Binds the paging toolbar to the specified {@link Roo.data.Store}
349 * @param {Roo.data.Store} store The data store to bind
352 ds.on("beforeload", this.beforeLoad, this);
353 ds.on("load", this.onLoad, this);
354 ds.on("loadexception", this.onLoadError, this);
355 ds.on("remove", this.updateInfo, this);
356 ds.on("add", this.updateInfo, this);