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) {
29 this.store= Roo.factory(this.store, Roo.data);
31 ds.xmodule = this.xmodule || false;
34 this.toolbarItems = [];
36 this.toolbarItems = config.items;
40 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
48 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
52 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
54 * @cfg {Roo.data.Store} dataSource
55 * The underlying data store providing the paged data
58 * @cfg {String/HTMLElement/Element} container
59 * container The id or element that will contain the toolbar
62 * @cfg {Boolean} displayInfo
63 * True to display the displayMsg (defaults to false)
66 * @cfg {Number} pageSize
67 * The number of records to display per page (defaults to 20)
71 * @cfg {String} displayMsg
72 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
74 displayMsg : 'Displaying {0} - {1} of {2}',
76 * @cfg {String} emptyMsg
77 * The message to display when no records are found (defaults to "No data to display")
79 emptyMsg : 'No data to display',
81 * Customizable piece of the default paging text (defaults to "Page")
84 beforePageText : "Page",
86 * Customizable piece of the default paging text (defaults to "of %0")
89 afterPageText : "of {0}",
91 * Customizable piece of the default paging text (defaults to "First Page")
94 firstText : "First Page",
96 * Customizable piece of the default paging text (defaults to "Previous Page")
99 prevText : "Previous Page",
101 * Customizable piece of the default paging text (defaults to "Next Page")
104 nextText : "Next Page",
106 * Customizable piece of the default paging text (defaults to "Last Page")
109 lastText : "Last Page",
111 * Customizable piece of the default paging text (defaults to "Refresh")
114 refreshText : "Refresh",
118 onRender : function(ct, position)
120 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
121 this.navgroup.parentId = this.id;
122 this.navgroup.onRender(this.el, null);
123 // add the buttons to the navgroup
125 if(this.displayInfo){
126 Roo.log(this.el.select('ul.navbar-nav',true).first());
127 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
128 this.displayEl = this.el.select('.x-paging-info', true).first();
129 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
130 // this.displayEl = navel.el.select('span',true).first();
136 Roo.each(_this.buttons, function(e){
137 Roo.factory(e).onRender(_this.el, null);
141 Roo.each(_this.toolbarItems, function(e) {
142 _this.navgroup.addItem(e);
146 this.first = this.navgroup.addItem({
147 tooltip: this.firstText,
149 icon : 'fa fa-backward',
151 preventDefault: true,
152 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
155 this.prev = this.navgroup.addItem({
156 tooltip: this.prevText,
158 icon : 'fa fa-step-backward',
160 preventDefault: true,
161 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
163 //this.addSeparator();
166 var field = this.navgroup.addItem( {
168 cls : 'x-paging-position',
170 html : this.beforePageText +
171 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
172 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
175 this.field = field.el.select('input', true).first();
176 this.field.on("keydown", this.onPagingKeydown, this);
177 this.field.on("focus", function(){this.dom.select();});
180 this.afterTextEl = field.el.select('.x-paging-after',true).first();
181 //this.field.setHeight(18);
182 //this.addSeparator();
183 this.next = this.navgroup.addItem({
184 tooltip: this.nextText,
186 html : ' <i class="fa fa-step-forward">',
188 preventDefault: true,
189 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
191 this.last = this.navgroup.addItem({
192 tooltip: this.lastText,
193 icon : 'fa fa-forward',
196 preventDefault: true,
197 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
199 //this.addSeparator();
200 this.loading = this.navgroup.addItem({
201 tooltip: this.refreshText,
202 icon: 'fa fa-refresh',
203 preventDefault: true,
204 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
210 updateInfo : function(){
212 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
213 var msg = count == 0 ?
217 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
219 this.displayEl.update(msg);
224 onLoad : function(ds, r, o){
225 this.cursor = o.params ? o.params.start : 0;
226 var d = this.getPageData(),
230 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
231 this.field.dom.value = ap;
232 this.first.setDisabled(ap == 1);
233 this.prev.setDisabled(ap == 1);
234 this.next.setDisabled(ap == ps);
235 this.last.setDisabled(ap == ps);
236 this.loading.enable();
241 getPageData : function(){
242 var total = this.ds.getTotalCount();
245 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
246 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
251 onLoadError : function(){
252 this.loading.enable();
256 onPagingKeydown : function(e){
258 var d = this.getPageData();
260 var v = this.field.dom.value, pageNum;
261 if(!v || isNaN(pageNum = parseInt(v, 10))){
262 this.field.dom.value = d.activePage;
265 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
266 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
269 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))
271 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
272 this.field.dom.value = pageNum;
273 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
276 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
278 var v = this.field.dom.value, pageNum;
279 var increment = (e.shiftKey) ? 10 : 1;
280 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
282 if(!v || isNaN(pageNum = parseInt(v, 10))) {
283 this.field.dom.value = d.activePage;
286 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
288 this.field.dom.value = parseInt(v, 10) + increment;
289 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
290 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
297 beforeLoad : function(){
299 this.loading.disable();
304 onClick : function(which){
313 ds.load({params:{start: 0, limit: this.pageSize}});
316 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
319 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
322 var total = ds.getTotalCount();
323 var extra = total % this.pageSize;
324 var lastStart = extra ? (total - extra) : total-this.pageSize;
325 ds.load({params:{start: lastStart, limit: this.pageSize}});
328 ds.load({params:{start: this.cursor, limit: this.pageSize}});
334 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
335 * @param {Roo.data.Store} store The data store to unbind
337 unbind : function(ds){
338 ds.un("beforeload", this.beforeLoad, this);
339 ds.un("load", this.onLoad, this);
340 ds.un("loadexception", this.onLoadError, this);
341 ds.un("remove", this.updateInfo, this);
342 ds.un("add", this.updateInfo, this);
347 * Binds the paging toolbar to the specified {@link Roo.data.Store}
348 * @param {Roo.data.Store} store The data store to bind
351 ds.on("beforeload", this.beforeLoad, this);
352 ds.on("load", this.onLoad, this);
353 ds.on("loadexception", this.onLoadError, this);
354 ds.on("remove", this.updateInfo, this);
355 ds.on("add", this.updateInfo, this);