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;
26 this.toolbarItems = [];
28 this.toolbarItems = config.items;
32 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
39 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
43 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
45 * @cfg {Roo.data.Store} dataSource
46 * The underlying data store providing the paged data
49 * @cfg {String/HTMLElement/Element} container
50 * container The id or element that will contain the toolbar
53 * @cfg {Boolean} displayInfo
54 * True to display the displayMsg (defaults to false)
57 * @cfg {Number} pageSize
58 * The number of records to display per page (defaults to 20)
62 * @cfg {String} displayMsg
63 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
65 displayMsg : 'Displaying {0} - {1} of {2}',
67 * @cfg {String} emptyMsg
68 * The message to display when no records are found (defaults to "No data to display")
70 emptyMsg : 'No data to display',
72 * Customizable piece of the default paging text (defaults to "Page")
75 beforePageText : "Page",
77 * Customizable piece of the default paging text (defaults to "of %0")
80 afterPageText : "of {0}",
82 * Customizable piece of the default paging text (defaults to "First Page")
85 firstText : "First Page",
87 * Customizable piece of the default paging text (defaults to "Previous Page")
90 prevText : "Previous Page",
92 * Customizable piece of the default paging text (defaults to "Next Page")
95 nextText : "Next Page",
97 * Customizable piece of the default paging text (defaults to "Last Page")
100 lastText : "Last Page",
102 * Customizable piece of the default paging text (defaults to "Refresh")
105 refreshText : "Refresh",
109 onRender : function(ct, position)
111 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
112 this.navgroup.parentId = this.id;
113 this.navgroup.onRender(this.el, null);
114 // add the buttons to the navgroup
116 if(this.displayInfo){
117 Roo.log(this.el.select('ul.navbar-nav',true).first());
118 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
119 this.displayEl = this.el.select('.x-paging-info', true).first();
120 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
121 // this.displayEl = navel.el.select('span',true).first();
127 Roo.each(_this.buttons, function(e){
128 Roo.factory(e).onRender(_this.el, null);
132 Roo.each(_this.toolbarItems, function(e) {
133 _this.navgroup.addItem(e);
137 this.first = this.navgroup.addItem({
138 tooltip: this.firstText,
140 icon : 'fa fa-backward',
142 preventDefault: true,
143 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
146 this.prev = this.navgroup.addItem({
147 tooltip: this.prevText,
149 icon : 'fa fa-step-backward',
151 preventDefault: true,
152 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
154 //this.addSeparator();
157 var field = this.navgroup.addItem( {
159 cls : 'x-paging-position',
161 html : this.beforePageText +
162 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
163 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
166 this.field = field.el.select('input', true).first();
167 this.field.on("keydown", this.onPagingKeydown, this);
168 this.field.on("focus", function(){this.dom.select();});
171 this.afterTextEl = field.el.select('.x-paging-after',true).first();
172 //this.field.setHeight(18);
173 //this.addSeparator();
174 this.next = this.navgroup.addItem({
175 tooltip: this.nextText,
177 html : ' <i class="fa fa-step-forward">',
179 preventDefault: true,
180 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
182 this.last = this.navgroup.addItem({
183 tooltip: this.lastText,
184 icon : 'fa fa-forward',
187 preventDefault: true,
188 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
190 //this.addSeparator();
191 this.loading = this.navgroup.addItem({
192 tooltip: this.refreshText,
193 icon: 'fa fa-refresh',
194 preventDefault: true,
195 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
201 updateInfo : function(){
203 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
204 var msg = count == 0 ?
208 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
210 this.displayEl.update(msg);
215 onLoad : function(ds, r, o){
216 this.cursor = o.params ? o.params.start : 0;
217 var d = this.getPageData(),
221 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
222 this.field.dom.value = ap;
223 this.first.setDisabled(ap == 1);
224 this.prev.setDisabled(ap == 1);
225 this.next.setDisabled(ap == ps);
226 this.last.setDisabled(ap == ps);
227 this.loading.enable();
232 getPageData : function(){
233 var total = this.ds.getTotalCount();
236 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
237 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
242 onLoadError : function(){
243 this.loading.enable();
247 onPagingKeydown : function(e){
249 var d = this.getPageData();
251 var v = this.field.dom.value, pageNum;
252 if(!v || isNaN(pageNum = parseInt(v, 10))){
253 this.field.dom.value = d.activePage;
256 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
257 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
260 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))
262 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
263 this.field.dom.value = pageNum;
264 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
267 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
269 var v = this.field.dom.value, pageNum;
270 var increment = (e.shiftKey) ? 10 : 1;
271 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
273 if(!v || isNaN(pageNum = parseInt(v, 10))) {
274 this.field.dom.value = d.activePage;
277 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
279 this.field.dom.value = parseInt(v, 10) + increment;
280 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
281 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
288 beforeLoad : function(){
290 this.loading.disable();
295 onClick : function(which){
304 ds.load({params:{start: 0, limit: this.pageSize}});
307 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
310 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
313 var total = ds.getTotalCount();
314 var extra = total % this.pageSize;
315 var lastStart = extra ? (total - extra) : total-this.pageSize;
316 ds.load({params:{start: lastStart, limit: this.pageSize}});
319 ds.load({params:{start: this.cursor, limit: this.pageSize}});
325 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
326 * @param {Roo.data.Store} store The data store to unbind
328 unbind : function(ds){
329 ds.un("beforeload", this.beforeLoad, this);
330 ds.un("load", this.onLoad, this);
331 ds.un("loadexception", this.onLoadError, this);
332 ds.un("remove", this.updateInfo, this);
333 ds.un("add", this.updateInfo, this);
338 * Binds the paging toolbar to the specified {@link Roo.data.Store}
339 * @param {Roo.data.Store} store The data store to bind
342 ds.on("beforeload", this.beforeLoad, this);
343 ds.on("load", this.onLoad, this);
344 ds.on("loadexception", this.onLoadError, this);
345 ds.on("remove", this.updateInfo, this);
346 ds.on("add", this.updateInfo, this);