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
14 * @extends Roo.bootstrap.NavSimplebar
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
19 * @param {Roo.data.Store} store
21 Roo.bootstrap.PagingToolbar = function(config)
23 // old args format still supported... - xtype is prefered..
24 // created from xtype...
26 this.ds = config.dataSource;
28 if (config.store && !this.ds) {
29 this.store= Roo.factory(config.store, Roo.data);
31 this.ds.xmodule = this.xmodule || false;
34 this.toolbarItems = [];
36 this.toolbarItems = config.items;
39 Roo.bootstrap.PagingToolbar.superclass.constructor.call(this, config);
47 if (Roo.bootstrap.version == 4) {
48 this.navgroup = new Roo.bootstrap.ButtonGroup({ cls: 'pagination' });
51 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
56 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
58 * @cfg {Roo.data.Store} dataSource
59 * The underlying data store providing the paged data
62 * @cfg {String/HTMLElement/Element} container
63 * container The id or element that will contain the toolbar
66 * @cfg {Boolean} displayInfo
67 * True to display the displayMsg (defaults to false)
70 * @cfg {Number} pageSize
71 * The number of records to display per page (defaults to 20)
75 * @cfg {String} displayMsg
76 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
78 displayMsg : 'Displaying {0} - {1} of {2}',
80 * @cfg {String} emptyMsg
81 * The message to display when no records are found (defaults to "No data to display")
83 emptyMsg : 'No data to display',
85 * Customizable piece of the default paging text (defaults to "Page")
88 beforePageText : "Page",
90 * Customizable piece of the default paging text (defaults to "of %0")
93 afterPageText : "of {0}",
95 * Customizable piece of the default paging text (defaults to "First Page")
98 firstText : "First Page",
100 * Customizable piece of the default paging text (defaults to "Previous Page")
103 prevText : "Previous Page",
105 * Customizable piece of the default paging text (defaults to "Next Page")
108 nextText : "Next Page",
110 * Customizable piece of the default paging text (defaults to "Last Page")
113 lastText : "Last Page",
115 * Customizable piece of the default paging text (defaults to "Refresh")
118 refreshText : "Refresh",
122 onRender : function(ct, position)
124 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
125 this.navgroup.parentId = this.id;
126 this.navgroup.onRender(this.el, null);
127 // add the buttons to the navgroup
129 if(this.displayInfo){
130 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
131 this.displayEl = this.el.select('.x-paging-info', true).first();
132 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
133 // this.displayEl = navel.el.select('span',true).first();
139 Roo.each(_this.buttons, function(e){ // this might need to use render????
140 Roo.factory(e).render(_this.el);
144 Roo.each(_this.toolbarItems, function(e) {
145 _this.navgroup.addItem(e);
149 this.first = this.navgroup.addItem({
150 tooltip: this.firstText,
152 icon : 'fa fa-step-backward',
154 preventDefault: true,
155 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
158 this.prev = this.navgroup.addItem({
159 tooltip: this.prevText,
161 icon : 'fa fa-backward',
163 preventDefault: true,
164 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
166 //this.addSeparator();
169 var field = this.navgroup.addItem( {
171 cls : 'x-paging-position',
173 html : this.beforePageText +
174 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
175 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
178 this.field = field.el.select('input', true).first();
179 this.field.on("keydown", this.onPagingKeydown, this);
180 this.field.on("focus", function(){this.dom.select();});
183 this.afterTextEl = field.el.select('.x-paging-after',true).first();
184 //this.field.setHeight(18);
185 //this.addSeparator();
186 this.next = this.navgroup.addItem({
187 tooltip: this.nextText,
189 html : ' <i class="fa fa-forward">',
191 preventDefault: true,
192 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
194 this.last = this.navgroup.addItem({
195 tooltip: this.lastText,
196 icon : 'fa fa-step-forward',
199 preventDefault: true,
200 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
202 //this.addSeparator();
203 this.loading = this.navgroup.addItem({
204 tooltip: this.refreshText,
205 icon: 'fa fa-refresh',
206 preventDefault: true,
207 listeners : { click : this.onClick.createDelegate(this, ["refresh"]) }
213 updateInfo : function(){
215 var count = (typeof(this.getCount) == 'undefined') ? this.ds.getCount() : this.getCount();
216 var msg = count == 0 ?
220 this.cursor+1, this.cursor+count, this.ds.getTotalCount()
222 this.displayEl.update(msg);
227 onLoad : function(ds, r, o)
229 this.cursor = o.params.start ? o.params.start : 0;
231 var d = this.getPageData(),
236 this.afterTextEl.dom.innerHTML = String.format(this.afterPageText, d.pages);
237 this.field.dom.value = ap;
238 this.first.setDisabled(ap == 1);
239 this.prev.setDisabled(ap == 1);
240 this.next.setDisabled(ap == ps);
241 this.last.setDisabled(ap == ps);
242 this.loading.enable();
247 getPageData : function(){
248 var total = this.ds.getTotalCount();
251 activePage : Math.ceil((this.cursor+this.pageSize)/this.pageSize),
252 pages : total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
257 onLoadError : function(){
258 this.loading.enable();
262 onPagingKeydown : function(e){
264 var d = this.getPageData();
266 var v = this.field.dom.value, pageNum;
267 if(!v || isNaN(pageNum = parseInt(v, 10))){
268 this.field.dom.value = d.activePage;
271 pageNum = Math.min(Math.max(1, pageNum), d.pages) - 1;
272 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
275 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))
277 var pageNum = (k == e.HOME || (k == e.DOWN && e.ctrlKey) || (k == e.LEFT && e.ctrlKey) || (k == e.PAGEDOWN && e.ctrlKey)) ? 1 : d.pages;
278 this.field.dom.value = pageNum;
279 this.ds.load({params:{start: (pageNum - 1) * this.pageSize, limit: this.pageSize}});
282 else if(k == e.UP || k == e.RIGHT || k == e.PAGEUP || k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN)
284 var v = this.field.dom.value, pageNum;
285 var increment = (e.shiftKey) ? 10 : 1;
286 if(k == e.DOWN || k == e.LEFT || k == e.PAGEDOWN) {
289 if(!v || isNaN(pageNum = parseInt(v, 10))) {
290 this.field.dom.value = d.activePage;
293 else if(parseInt(v, 10) + increment >= 1 & parseInt(v, 10) + increment <= d.pages)
295 this.field.dom.value = parseInt(v, 10) + increment;
296 pageNum = Math.min(Math.max(1, pageNum + increment), d.pages) - 1;
297 this.ds.load({params:{start: pageNum * this.pageSize, limit: this.pageSize}});
304 beforeLoad : function(){
306 this.loading.disable();
311 onClick : function(which){
320 ds.load({params:{start: 0, limit: this.pageSize}});
323 ds.load({params:{start: Math.max(0, this.cursor-this.pageSize), limit: this.pageSize}});
326 ds.load({params:{start: this.cursor+this.pageSize, limit: this.pageSize}});
329 var total = ds.getTotalCount();
330 var extra = total % this.pageSize;
331 var lastStart = extra ? (total - extra) : total-this.pageSize;
332 ds.load({params:{start: lastStart, limit: this.pageSize}});
335 ds.load({params:{start: this.cursor, limit: this.pageSize}});
341 * Unbinds the paging toolbar from the specified {@link Roo.data.Store}
342 * @param {Roo.data.Store} store The data store to unbind
344 unbind : function(ds){
345 ds.un("beforeload", this.beforeLoad, this);
346 ds.un("load", this.onLoad, this);
347 ds.un("loadexception", this.onLoadError, this);
348 ds.un("remove", this.updateInfo, this);
349 ds.un("add", this.updateInfo, this);
354 * Binds the paging toolbar to the specified {@link Roo.data.Store}
355 * @param {Roo.data.Store} store The data store to bind
358 ds.on("beforeload", this.beforeLoad, this);
359 ds.on("load", this.onLoad, this);
360 ds.on("loadexception", this.onLoadError, this);
361 ds.on("remove", this.updateInfo, this);
362 ds.on("add", this.updateInfo, this);