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();
127 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
128 // this.displayEl = navel.el.select('span',true).first();
134 Roo.each(_this.buttons, function(e){
135 Roo.factory(e).onRender(_this.el, null);
139 Roo.each(_this.toolbarItems, function(e) {
140 _this.navgroup.addItem(e);
144 this.first = this.navgroup.addItem({
145 tooltip: this.firstText,
147 icon : 'fa fa-backward',
149 preventDefault: true,
150 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
153 this.prev = this.navgroup.addItem({
154 tooltip: this.prevText,
156 icon : 'fa fa-step-backward',
158 preventDefault: true,
159 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
161 //this.addSeparator();
164 var field = this.navgroup.addItem( {
166 cls : 'x-paging-position',
168 html : this.beforePageText +
169 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
170 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
173 this.field = field.el.select('input', true).first();
174 this.field.on("keydown", this.onPagingKeydown, this);
175 this.field.on("focus", function(){this.dom.select();});
178 this.afterTextEl = field.el.select('.x-paging-after',true).first();
179 //this.field.setHeight(18);
180 //this.addSeparator();
181 this.next = this.navgroup.addItem({
182 tooltip: this.nextText,
184 html : ' <i class="fa fa-step-forward">',
186 preventDefault: true,
187 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
189 this.last = this.navgroup.addItem({
190 tooltip: this.lastText,
191 icon : 'fa fa-forward',
194 preventDefault: true,
195 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
197 //this.addSeparator();
198 this.loading = this.navgroup.addItem({
199 tooltip: this.refreshText,
200 icon: 'fa fa-refresh',
201 preventDefault: true,
202 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
208 updateInfo : function(){
210 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
211 var msg = count == 0 ?
215 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
217 this.displayEl.update(msg);
222 onLoad : function(ds, r, o){
223 this.cursor = o.params ? o.params.start : 0;
224 var d = this.getPageData(),
228 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
229 this.field.dom.value = ap;
230 this.first.setDisabled(ap == 1);
231 this.prev.setDisabled(ap == 1);
232 this.next.setDisabled(ap == ps);
233 this.last.setDisabled(ap == ps);
234 this.loading.enable();
239 getPageData : function(){
240 var total = this.ds.getTotalCount();
243 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
244 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
249 onLoadError : function(){
250 this.loading.enable();
254 onPagingKeydown : function(e){
256 var d = this.getPageData();
258 var v = this.field.dom.value, pageNum;
259 if(!v || isNaN(pageNum = parseInt(v, 10))){
260 this.field.dom.value = d.activePage;
263 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
264 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
267 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))
269 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
270 this.field.dom.value = pageNum;
271 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
274 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
276 var v = this.field.dom.value, pageNum;
277 var increment = (e.shiftKey) ? 10 : 1;
278 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
280 if(!v || isNaN(pageNum = parseInt(v, 10))) {
281 this.field.dom.value = d.activePage;
284 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
286 this.field.dom.value = parseInt(v, 10) + increment;
287 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
288 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
295 beforeLoad : function(){
297 this.loading.disable();
302 onClick : function(which){
311 ds.load({params:{start: 0, limit: this.pageSize}});
314 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
317 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
320 var total = ds.getTotalCount();
321 var extra = total % this.pageSize;
322 var lastStart = extra ? (total - extra) : total-this.pageSize;
323 ds.load({params:{start: lastStart, limit: this.pageSize}});
326 ds.load({params:{start: this.cursor, limit: this.pageSize}});
332 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
333 * @param {Roo.data.Store} store The data store to unbind
335 unbind : function(ds){
336 ds.un("beforeload", this.beforeLoad, this);
337 ds.un("load", this.onLoad, this);
338 ds.un("loadexception", this.onLoadError, this);
339 ds.un("remove", this.updateInfo, this);
340 ds.un("add", this.updateInfo, this);
345 * Binds the paging toolbar to the specified {@link Roo.data.Store}
346 * @param {Roo.data.Store} store The data store to bind
349 ds.on("beforeload", this.beforeLoad, this);
350 ds.on("load", this.onLoad, this);
351 ds.on("loadexception", this.onLoadError, this);
352 ds.on("remove", this.updateInfo, this);
353 ds.on("add", this.updateInfo, this);