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.grid.Grid
14 * @extends Roo.util.Observable
15 * This class represents the primary interface of a component based grid control.
16 * <br><br>Usage:<pre><code>
17 var grid = new Roo.grid.Grid("my-container-id", {
20 selModel: mySelectionModel,
21 autoSizeColumns: true,
22 monitorWindowResize: false,
28 * <b>Common Problems:</b><br/>
29 * - Grid does not resize properly when going smaller: Setting overflow hidden on the container
30 * element will correct this<br/>
31 * - If you get el.style[camel]= NaNpx or -2px or something related, be certain you have given your container element
32 * dimensions. The grid adapts to your container's size, if your container has no size defined then the results
33 * are unpredictable.<br/>
34 * - Do not render the grid into an element with display:none. Try using visibility:hidden. Otherwise there is no way for the
35 * grid to calculate dimensions/offsets.<br/>
37 * @param {String/HTMLElement/Roo.Element} container The element into which this grid will be rendered -
38 * The container MUST have some type of size defined for the grid to fill. The container will be
39 * automatically set to position relative if it isn't already.
40 * @param {Object} config A config object that sets properties on this grid.
42 Roo.grid.Grid = function(container, config){
43 // initialize the container
44 this.container = Roo.get(container);
45 this.container.update("");
46 this.container.setStyle("overflow", "hidden");
47 this.container.addClass('x-grid-container');
49 this.id = this.container.id;
51 Roo.apply(this, config);
52 // check and correct shorthanded configs
54 this.dataSource = this.ds;
58 this.colModel = this.cm;
62 this.selModel = this.sm;
67 this.selModel = Roo.factory(this.selModel, Roo.grid);
69 if (typeof(this.colModel.config) == 'undefined') {
70 this.colModel = new Roo.grid.ColumnModel(this.colModel);
72 if (this.dataSource) {
73 this.dataSource= Roo.factory(this.dataSource, Roo.data);
79 this.container.setWidth(this.width);
83 this.container.setHeight(this.height);
90 * The raw click event for the entire grid.
91 * @param {Roo.EventObject} e
96 * The raw dblclick event for the entire grid.
97 * @param {Roo.EventObject} e
102 * The raw contextmenu event for the entire grid.
103 * @param {Roo.EventObject} e
105 "contextmenu" : true,
108 * The raw mousedown event for the entire grid.
109 * @param {Roo.EventObject} e
114 * The raw mouseup event for the entire grid.
115 * @param {Roo.EventObject} e
120 * The raw mouseover event for the entire grid.
121 * @param {Roo.EventObject} e
126 * The raw mouseout event for the entire grid.
127 * @param {Roo.EventObject} e
132 * The raw keypress event for the entire grid.
133 * @param {Roo.EventObject} e
138 * The raw keydown event for the entire grid.
139 * @param {Roo.EventObject} e
147 * Fires when a cell is clicked
149 * @param {Number} rowIndex
150 * @param {Number} columnIndex
151 * @param {Roo.EventObject} e
155 * @event celldblclick
156 * Fires when a cell is double clicked
158 * @param {Number} rowIndex
159 * @param {Number} columnIndex
160 * @param {Roo.EventObject} e
162 "celldblclick" : true,
165 * Fires when a row is clicked
167 * @param {Number} rowIndex
168 * @param {Roo.EventObject} e
173 * Fires when a row is double clicked
175 * @param {Number} rowIndex
176 * @param {Roo.EventObject} e
178 "rowdblclick" : true,
181 * Fires when a header is clicked
183 * @param {Number} columnIndex
184 * @param {Roo.EventObject} e
186 "headerclick" : true,
188 * @event headerdblclick
189 * Fires when a header cell is double clicked
191 * @param {Number} columnIndex
192 * @param {Roo.EventObject} e
194 "headerdblclick" : true,
196 * @event rowcontextmenu
197 * Fires when a row is right clicked
199 * @param {Number} rowIndex
200 * @param {Roo.EventObject} e
202 "rowcontextmenu" : true,
204 * @event cellcontextmenu
205 * Fires when a cell is right clicked
207 * @param {Number} rowIndex
208 * @param {Number} cellIndex
209 * @param {Roo.EventObject} e
211 "cellcontextmenu" : true,
213 * @event headercontextmenu
214 * Fires when a header is right clicked
216 * @param {Number} columnIndex
217 * @param {Roo.EventObject} e
219 "headercontextmenu" : true,
222 * Fires when the body element is scrolled
223 * @param {Number} scrollLeft
224 * @param {Number} scrollTop
228 * @event columnresize
229 * Fires when the user resizes a column
230 * @param {Number} columnIndex
231 * @param {Number} newSize
233 "columnresize" : true,
236 * Fires when the user moves a column
237 * @param {Number} oldIndex
238 * @param {Number} newIndex
243 * Fires when row(s) start being dragged
245 * @param {Roo.GridDD} dd The drag drop object
246 * @param {event} e The raw browser event
251 * Fires when a drag operation is complete
253 * @param {Roo.GridDD} dd The drag drop object
254 * @param {event} e The raw browser event
259 * Fires when dragged row(s) are dropped on a valid DD target
261 * @param {Roo.GridDD} dd The drag drop object
262 * @param {String} targetId The target drag drop object
263 * @param {event} e The raw browser event
268 * Fires while row(s) are being dragged. "targetId" is the id of the Yahoo.util.DD object the selected rows are being dragged over.
270 * @param {Roo.GridDD} dd The drag drop object
271 * @param {String} targetId The target drag drop object
272 * @param {event} e The raw browser event
277 * Fires when the dragged row(s) first cross another DD target while being dragged
279 * @param {Roo.GridDD} dd The drag drop object
280 * @param {String} targetId The target drag drop object
281 * @param {event} e The raw browser event
286 * Fires when the dragged row(s) leave another DD target while being dragged
288 * @param {Roo.GridDD} dd The drag drop object
289 * @param {String} targetId The target drag drop object
290 * @param {event} e The raw browser event
295 * Fires when the grid is rendered
301 Roo.grid.Grid.superclass.constructor.call(this);
303 Roo.extend(Roo.grid.Grid, Roo.util.Observable, {
305 * @cfg {Number} minColumnWidth The minimum width a column can be resized to. Default is 25.
310 * @cfg {Boolean} autoSizeColumns True to automatically resize the columns to fit their content
311 * <b>on initial render.</b> It is more efficient to explicitly size the columns
312 * through the ColumnModel's {@link Roo.grid.ColumnModel#width} config option. Default is false.
314 autoSizeColumns : false,
317 * @cfg {Boolean} autoSizeHeaders True to measure headers with column data when auto sizing columns. Default is true.
319 autoSizeHeaders : true,
322 * @cfg {Boolean} monitorWindowResize True to autoSize the grid when the window resizes. Default is true.
324 monitorWindowResize : true,
327 * @cfg {Boolean} maxRowsToMeasure If autoSizeColumns is on, maxRowsToMeasure can be used to limit the number of
328 * rows measured to get a columns size. Default is 0 (all rows).
330 maxRowsToMeasure : 0,
333 * @cfg {Boolean} trackMouseOver True to highlight rows when the mouse is over. Default is true.
335 trackMouseOver : true,
338 * @cfg {Boolean} enableDragDrop True to enable drag and drop of rows. Default is false.
340 enableDragDrop : false,
343 * @cfg {Boolean} enableColumnMove True to enable drag and drop reorder of columns. Default is true.
345 enableColumnMove : true,
348 * @cfg {Boolean} enableColumnHide True to enable hiding of columns with the header context menu. Default is true.
350 enableColumnHide : true,
353 * @cfg {Boolean} enableRowHeightSync True to manually sync row heights across locked and not locked rows. Default is false.
355 enableRowHeightSync : false,
358 * @cfg {Boolean} stripeRows True to stripe the rows. Default is true.
363 * @cfg {Boolean} autoHeight True to fit the height of the grid container to the height of the data. Default is false.
368 * @cfg {String} autoExpandColumn The id (or dataIndex) of a column in this grid that should expand to fill unused space. This id can not be 0. Default is false.
370 autoExpandColumn : false,
373 * @cfg {Number} autoExpandMin The minimum width the autoExpandColumn can have (if enabled).
379 * @cfg {Number} autoExpandMax The maximum width the autoExpandColumn can have (if enabled). Default is 1000.
381 autoExpandMax : 1000,
384 * @cfg {Object} view The {@link Roo.grid.GridView} used by the grid. This can be set before a call to render().
389 * @cfg {Object} loadMask An {@link Roo.LoadMask} config or true to mask the grid while loading. Default is false.
397 * @cfg {Boolean} autoWidth True to set the grid's width to the default total width of the grid's columns instead
398 * of a fixed width. Default is false.
401 * @cfg {Number} maxHeight Sets the maximum height of the grid - ignored if autoHeight is not on.
404 * Called once after all setup has been completed and the grid is ready to be rendered.
405 * @return {Roo.grid.Grid} this
408 var c = this.container;
409 // try to detect autoHeight/width mode
410 if((!c.dom.offsetHeight || c.dom.offsetHeight < 20) || c.getStyle("height") == "auto"){
411 this.autoHeight = true;
413 var view = this.getView();
416 c.on("click", this.onClick, this);
417 c.on("dblclick", this.onDblClick, this);
418 c.on("contextmenu", this.onContextMenu, this);
419 c.on("keydown", this.onKeyDown, this);
421 this.relayEvents(c, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
423 this.getSelectionModel().init(this);
428 this.loadMask = new Roo.LoadMask(this.container,
429 Roo.apply({store:this.dataSource}, this.loadMask));
433 if (this.toolbar && this.toolbar.xtype) {
434 this.toolbar.container = this.getView().getHeaderPanel(true);
435 this.toolbar = new Ext.Toolbar(this.toolbar);
437 if (this.footer && this.footer.xtype) {
438 this.footer.dataSource = this.getDataSource();
439 this.footer.container = this.getView().getFooterPanel(true);
440 this.footer = Roo.factory(this.footer, Roo);
442 this.rendered = true;
443 this.fireEvent('render', this);
448 * Reconfigures the grid to use a different Store and Column Model.
449 * The View will be bound to the new objects and refreshed.
450 * @param {Roo.data.Store} dataSource The new {@link Roo.data.Store} object
451 * @param {Roo.grid.ColumnModel} The new {@link Roo.grid.ColumnModel} object
453 reconfigure : function(dataSource, colModel){
455 this.loadMask.destroy();
456 this.loadMask = new Roo.LoadMask(this.container,
457 Roo.apply({store:dataSource}, this.loadMask));
459 this.view.bind(dataSource, colModel);
460 this.dataSource = dataSource;
461 this.colModel = colModel;
462 this.view.refresh(true);
466 onKeyDown : function(e){
467 this.fireEvent("keydown", e);
472 * @param {Boolean} removeEl True to remove the element
474 destroy : function(removeEl, keepListeners){
476 this.loadMask.destroy();
478 var c = this.container;
479 c.removeAllListeners();
481 this.colModel.purgeListeners();
483 this.purgeListeners();
486 if(removeEl === true){
492 processEvent : function(name, e){
493 this.fireEvent(name, e);
494 var t = e.getTarget();
496 var header = v.findHeaderIndex(t);
497 if(header !== false){
498 this.fireEvent("header" + name, this, header, e);
500 var row = v.findRowIndex(t);
501 var cell = v.findCellIndex(t);
503 this.fireEvent("row" + name, this, row, e);
505 this.fireEvent("cell" + name, this, row, cell, e);
512 onClick : function(e){
513 this.processEvent("click", e);
517 onContextMenu : function(e, t){
518 this.processEvent("contextmenu", e);
522 onDblClick : function(e){
523 this.processEvent("dblclick", e);
527 walkCells : function(row, col, step, fn, scope){
528 var cm = this.colModel, clen = cm.getColumnCount();
529 var ds = this.dataSource, rlen = ds.getCount(), first = true;
541 if(fn.call(scope || this, row, col, cm) === true){
559 if(fn.call(scope || this, row, col, cm) === true){
571 getSelections : function(){
572 return this.selModel.getSelections();
576 * Causes the grid to manually recalculate its dimensions. Generally this is done automatically,
577 * but if manual update is required this method will initiate it.
579 autoSize : function(){
582 if(this.view.adjustForScroll){
583 this.view.adjustForScroll();
589 * Returns the grid's underlying element.
590 * @return {Element} The element
592 getGridEl : function(){
593 return this.container;
596 // private for compatibility, overridden by editor grid
597 stopEditing : function(){},
600 * Returns the grid's SelectionModel.
601 * @return {SelectionModel}
603 getSelectionModel : function(){
605 this.selModel = new Roo.grid.RowSelectionModel();
607 return this.selModel;
611 * Returns the grid's DataSource.
612 * @return {DataSource}
614 getDataSource : function(){
615 return this.dataSource;
619 * Returns the grid's ColumnModel.
620 * @return {ColumnModel}
622 getColumnModel : function(){
623 return this.colModel;
627 * Returns the grid's GridView object.
630 getView : function(){
632 this.view = new Roo.grid.GridView(this.viewConfig);
637 * Called to get grid's drag proxy text, by default returns this.ddText.
640 getDragDropText : function(){
641 var count = this.selModel.getCount();
642 return String.format(this.ddText, count, count == 1 ? '' : 's');
646 * Configures the text is the drag proxy (defaults to "%0 selected row(s)").
647 * %0 is replaced with the number of selected rows.
650 Roo.grid.Grid.prototype.ddText = "{0} selected row{1}";