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' });
50 this.navgroup = new Roo.bootstrap.NavGroup({ cls: 'pagination' });
55 Roo.extend(Roo.bootstrap.PagingToolbar, Roo.bootstrap.NavSimplebar, {
57 * @cfg {Roo.data.Store} dataSource
58 * The underlying data store providing the paged data
61 * @cfg {String/HTMLElement/Element} container
62 * container The id or element that will contain the toolbar
65 * @cfg {Boolean} displayInfo
66 * True to display the displayMsg (defaults to false)
69 * @cfg {Number} pageSize
70 * The number of records to display per page (defaults to 20)
74 * @cfg {String} displayMsg
75 * The paging status message to display (defaults to "Displaying {start} - {end} of {total}")
77 displayMsg : 'Displaying {0} - {1} of {2}',
79 * @cfg {String} emptyMsg
80 * The message to display when no records are found (defaults to "No data to display")
82 emptyMsg : 'No data to display',
84 * Customizable piece of the default paging text (defaults to "Page")
87 beforePageText : "Page",
89 * Customizable piece of the default paging text (defaults to "of %0")
92 afterPageText : "of {0}",
94 * Customizable piece of the default paging text (defaults to "First Page")
97 firstText : "First Page",
99 * Customizable piece of the default paging text (defaults to "Previous Page")
102 prevText : "Previous Page",
104 * Customizable piece of the default paging text (defaults to "Next Page")
107 nextText : "Next Page",
109 * Customizable piece of the default paging text (defaults to "Last Page")
112 lastText : "Last Page",
114 * Customizable piece of the default paging text (defaults to "Refresh")
117 refreshText : "Refresh",
121 onRender : function(ct, position)
123 Roo.bootstrap.PagingToolbar.superclass.onRender.call(this, ct, position);
124 this.navgroup.parentId = this.id;
125 this.navgroup.onRender(this.el, null);
126 // add the buttons to the navgroup
128 if(this.displayInfo){
129 this.el.select('ul.navbar-nav',true).first().createChild({cls:'x-paging-info'});
130 this.displayEl = this.el.select('.x-paging-info', true).first();
131 // var navel = this.navgroup.addItem( { tagtype : 'span', html : '', cls : 'x-paging-info', preventDefault : true } );
132 // this.displayEl = navel.el.select('span',true).first();
138 Roo.each(_this.buttons, function(e){ // this might need to use render????
139 Roo.factory(e).render(_this.el);
143 Roo.each(_this.toolbarItems, function(e) {
144 _this.navgroup.addItem(e);
148 this.first = this.navgroup.addItem({
149 tooltip: this.firstText,
150 cls: "prev btn-outline-secondary",
151 html : ' <i class="fa fa-step-backward"></i>',
153 preventDefault: true,
154 listeners : { click : this.onClick.createDelegate(this, ["first"]) }
157 this.prev = this.navgroup.addItem({
158 tooltip: this.prevText,
159 cls: "prev btn-outline-secondary",
160 html : ' <i class="fa fa-backward"></i>',
162 preventDefault: true,
163 listeners : { click : this.onClick.createDelegate(this, ["prev"]) }
165 //this.addSeparator();
168 var field = this.navgroup.addItem( {
170 cls : 'x-paging-position btn-outline-secondary',
172 html : this.beforePageText +
173 '<input type="text" size="3" value="1" class="x-grid-page-number">' +
174 '<span class="x-paging-after">' + String.format(this.afterPageText, 1) + '</span>'
177 this.field = field.el.select('input', true).first();
178 this.field.on("keydown", this.onPagingKeydown, this);
179 this.field.on("focus", function(){this.dom.select();});
182 this.afterTextEl = field.el.select('.x-paging-after',true).first();
183 //this.field.setHeight(18);
184 //this.addSeparator();
185 this.next = this.navgroup.addItem({
186 tooltip: this.nextText,
187 cls: "next btn-outline-secondary",
188 html : ' <i class="fa fa-forward"></i>',
190 preventDefault: true,
191 listeners : { click : this.onClick.createDelegate(this, ["next"]) }
193 this.last = this.navgroup.addItem({
194 tooltip: this.lastText,
195 html : ' <i class="fa fa-step-forward"></i>',
196 cls: "next btn-outline-secondary",
198 preventDefault: true,
199 listeners : { click : this.onClick.createDelegate(this, ["last"]) }
201 //this.addSeparator();
202 this.loading = this.navgroup.addItem({
203 tooltip: this.refreshText,
204 cls: "btn-outline-secondary",
205 html : ' <i class="fa fa-refresh"></i>',
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);