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;
61 this.colModel = this.cm;
65 this.selModel = this.sm;
70 this.selModel = Roo.factory(this.selModel, Roo.grid);
71 this.sm = this.selModel;
72 this.sm.xmodule = this.xmodule || false;
74 if (typeof(this.colModel.config) == 'undefined') {
75 this.colModel = new Roo.grid.ColumnModel(this.colModel);
76 this.cm = this.colModel;
77 this.cm.xmodule = this.xmodule || false;
79 if (this.dataSource) {
80 this.dataSource= Roo.factory(this.dataSource, Roo.data);
81 this.ds = this.dataSource;
82 this.ds.xmodule = this.xmodule || false;
89 this.container.setWidth(this.width);
93 this.container.setHeight(this.height);
100 * The raw click event for the entire grid.
101 * @param {Roo.EventObject} e
106 * The raw dblclick event for the entire grid.
107 * @param {Roo.EventObject} e
112 * The raw contextmenu event for the entire grid.
113 * @param {Roo.EventObject} e
115 "contextmenu" : true,
118 * The raw mousedown event for the entire grid.
119 * @param {Roo.EventObject} e
124 * The raw mouseup event for the entire grid.
125 * @param {Roo.EventObject} e
130 * The raw mouseover event for the entire grid.
131 * @param {Roo.EventObject} e
136 * The raw mouseout event for the entire grid.
137 * @param {Roo.EventObject} e
142 * The raw keypress event for the entire grid.
143 * @param {Roo.EventObject} e
148 * The raw keydown event for the entire grid.
149 * @param {Roo.EventObject} e
157 * Fires when a cell is clicked
159 * @param {Number} rowIndex
160 * @param {Number} columnIndex
161 * @param {Roo.EventObject} e
165 * @event celldblclick
166 * Fires when a cell is double clicked
168 * @param {Number} rowIndex
169 * @param {Number} columnIndex
170 * @param {Roo.EventObject} e
172 "celldblclick" : true,
175 * Fires when a row is clicked
177 * @param {Number} rowIndex
178 * @param {Roo.EventObject} e
183 * Fires when a row is double clicked
185 * @param {Number} rowIndex
186 * @param {Roo.EventObject} e
188 "rowdblclick" : true,
191 * Fires when a header is clicked
193 * @param {Number} columnIndex
194 * @param {Roo.EventObject} e
196 "headerclick" : true,
198 * @event headerdblclick
199 * Fires when a header cell is double clicked
201 * @param {Number} columnIndex
202 * @param {Roo.EventObject} e
204 "headerdblclick" : true,
206 * @event rowcontextmenu
207 * Fires when a row is right clicked
209 * @param {Number} rowIndex
210 * @param {Roo.EventObject} e
212 "rowcontextmenu" : true,
214 * @event cellcontextmenu
215 * Fires when a cell is right clicked
217 * @param {Number} rowIndex
218 * @param {Number} cellIndex
219 * @param {Roo.EventObject} e
221 "cellcontextmenu" : true,
223 * @event headercontextmenu
224 * Fires when a header is right clicked
226 * @param {Number} columnIndex
227 * @param {Roo.EventObject} e
229 "headercontextmenu" : true,
232 * Fires when the body element is scrolled
233 * @param {Number} scrollLeft
234 * @param {Number} scrollTop
238 * @event columnresize
239 * Fires when the user resizes a column
240 * @param {Number} columnIndex
241 * @param {Number} newSize
243 "columnresize" : true,
246 * Fires when the user moves a column
247 * @param {Number} oldIndex
248 * @param {Number} newIndex
253 * Fires when row(s) start being dragged
255 * @param {Roo.GridDD} dd The drag drop object
256 * @param {event} e The raw browser event
261 * Fires when a drag operation is complete
263 * @param {Roo.GridDD} dd The drag drop object
264 * @param {event} e The raw browser event
269 * Fires when dragged row(s) are dropped on a valid DD target
271 * @param {Roo.GridDD} dd The drag drop object
272 * @param {String} targetId The target drag drop object
273 * @param {event} e The raw browser event
278 * Fires while row(s) are being dragged. "targetId" is the id of the Yahoo.util.DD object the selected rows are being dragged over.
280 * @param {Roo.GridDD} dd The drag drop object
281 * @param {String} targetId The target drag drop object
282 * @param {event} e The raw browser event
287 * Fires when the dragged row(s) first cross another DD target while being dragged
289 * @param {Roo.GridDD} dd The drag drop object
290 * @param {String} targetId The target drag drop object
291 * @param {event} e The raw browser event
296 * Fires when the dragged row(s) leave another DD target while being dragged
298 * @param {Roo.GridDD} dd The drag drop object
299 * @param {String} targetId The target drag drop object
300 * @param {event} e The raw browser event
305 * Fires when a row is rendered, so you can change add a style to it.
306 * @param {GridView} gridview The grid view
307 * @param {Object} rowcfg contains record rowIndex and rowClass - set rowClass to add a style.
313 * Fires when the grid is rendered
319 Roo.grid.Grid.superclass.constructor.call(this);
321 Roo.extend(Roo.grid.Grid, Roo.util.Observable, {
324 * @cfg {String} ddGroup - drag drop group.
328 * @cfg {Number} minColumnWidth The minimum width a column can be resized to. Default is 25.
333 * @cfg {Boolean} autoSizeColumns True to automatically resize the columns to fit their content
334 * <b>on initial render.</b> It is more efficient to explicitly size the columns
335 * through the ColumnModel's {@link Roo.grid.ColumnModel#width} config option. Default is false.
337 autoSizeColumns : false,
340 * @cfg {Boolean} autoSizeHeaders True to measure headers with column data when auto sizing columns. Default is true.
342 autoSizeHeaders : true,
345 * @cfg {Boolean} monitorWindowResize True to autoSize the grid when the window resizes. Default is true.
347 monitorWindowResize : true,
350 * @cfg {Boolean} maxRowsToMeasure If autoSizeColumns is on, maxRowsToMeasure can be used to limit the number of
351 * rows measured to get a columns size. Default is 0 (all rows).
353 maxRowsToMeasure : 0,
356 * @cfg {Boolean} trackMouseOver True to highlight rows when the mouse is over. Default is true.
358 trackMouseOver : true,
361 * @cfg {Boolean} enableDrag True to enable drag of rows. Default is false. (double check if this is needed?)
365 * @cfg {Boolean} enableDragDrop True to enable drag and drop of rows. Default is false.
367 enableDragDrop : false,
370 * @cfg {Boolean} enableColumnMove True to enable drag and drop reorder of columns. Default is true.
372 enableColumnMove : true,
375 * @cfg {Boolean} enableColumnHide True to enable hiding of columns with the header context menu. Default is true.
377 enableColumnHide : true,
380 * @cfg {Boolean} enableRowHeightSync True to manually sync row heights across locked and not locked rows. Default is false.
382 enableRowHeightSync : false,
385 * @cfg {Boolean} stripeRows True to stripe the rows. Default is true.
390 * @cfg {Boolean} autoHeight True to fit the height of the grid container to the height of the data. Default is false.
395 * @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.
397 autoExpandColumn : false,
400 * @cfg {Number} autoExpandMin The minimum width the autoExpandColumn can have (if enabled).
406 * @cfg {Number} autoExpandMax The maximum width the autoExpandColumn can have (if enabled). Default is 1000.
408 autoExpandMax : 1000,
411 * @cfg {Object} view The {@link Roo.grid.GridView} used by the grid. This can be set before a call to render().
416 * @cfg {Object} loadMask An {@link Roo.LoadMask} config or true to mask the grid while loading. Default is false.
420 * @cfg {Roo.dd.DropTarget} dropTarget An {@link Roo.dd.DropTarget} config
430 * @cfg {Boolean} autoWidth True to set the grid's width to the default total width of the grid's columns instead
431 * of a fixed width. Default is false.
434 * @cfg {Number} maxHeight Sets the maximum height of the grid - ignored if autoHeight is not on.
437 * Called once after all setup has been completed and the grid is ready to be rendered.
438 * @return {Roo.grid.Grid} this
442 var c = this.container;
443 // try to detect autoHeight/width mode
444 if((!c.dom.offsetHeight || c.dom.offsetHeight < 20) || c.getStyle("height") == "auto"){
445 this.autoHeight = true;
447 var view = this.getView();
450 c.on("click", this.onClick, this);
451 c.on("dblclick", this.onDblClick, this);
452 c.on("contextmenu", this.onContextMenu, this);
453 c.on("keydown", this.onKeyDown, this);
455 c.on("touchstart", this.onTouchStart, this);
458 this.relayEvents(c, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
460 this.getSelectionModel().init(this);
465 this.loadMask = new Roo.LoadMask(this.container,
466 Roo.apply({store:this.dataSource}, this.loadMask));
470 if (this.toolbar && this.toolbar.xtype) {
471 this.toolbar.container = this.getView().getHeaderPanel(true);
472 this.toolbar = new Roo.Toolbar(this.toolbar);
474 if (this.footer && this.footer.xtype) {
475 this.footer.dataSource = this.getDataSource();
476 this.footer.container = this.getView().getFooterPanel(true);
477 this.footer = Roo.factory(this.footer, Roo);
479 if (this.dropTarget && this.dropTarget.xtype) {
480 delete this.dropTarget.xtype;
481 this.dropTarget = new Roo.dd.DropTarget(this.getView().mainBody, this.dropTarget);
485 this.rendered = true;
486 this.fireEvent('render', this);
491 * Reconfigures the grid to use a different Store and Column Model.
492 * The View will be bound to the new objects and refreshed.
493 * @param {Roo.data.Store} dataSource The new {@link Roo.data.Store} object
494 * @param {Roo.grid.ColumnModel} The new {@link Roo.grid.ColumnModel} object
496 reconfigure : function(dataSource, colModel){
498 this.loadMask.destroy();
499 this.loadMask = new Roo.LoadMask(this.container,
500 Roo.apply({store:dataSource}, this.loadMask));
502 this.view.bind(dataSource, colModel);
503 this.dataSource = dataSource;
504 this.colModel = colModel;
505 this.view.refresh(true);
509 onKeyDown : function(e){
510 this.fireEvent("keydown", e);
515 * @param {Boolean} removeEl True to remove the element
517 destroy : function(removeEl, keepListeners){
519 this.loadMask.destroy();
521 var c = this.container;
522 c.removeAllListeners();
524 this.colModel.purgeListeners();
526 this.purgeListeners();
529 if(removeEl === true){
535 processEvent : function(name, e){
536 // does this fire select???
537 //Roo.log('grid:processEvent ' + name);
539 if (name != 'touchstart' ) {
540 this.fireEvent(name, e);
543 var t = e.getTarget();
545 var header = v.findHeaderIndex(t);
546 if(header !== false){
547 var ename = name == 'touchstart' ? 'click' : name;
549 this.fireEvent("header" + ename, this, header, e);
551 var row = v.findRowIndex(t);
552 var cell = v.findCellIndex(t);
553 if (name == 'touchstart') {
554 // first touch is always a click.
555 // hopefull this happens after selection is updated.?
558 if (typeof(this.selModel.getSelectedCell) != 'undefined') {
559 var cs = this.selModel.getSelectedCell();
560 if (row == cs[0] && cell == cs[1]){
564 if (typeof(this.selModel.getSelections) != 'undefined') {
565 var cs = this.selModel.getSelections();
566 var ds = this.dataSource;
567 if (cs.length == 1 && ds.getAt(row) == cs[0]){
578 this.fireEvent("row" + name, this, row, e);
580 this.fireEvent("cell" + name, this, row, cell, e);
587 onClick : function(e){
588 this.processEvent("click", e);
591 onTouchStart : function(e){
592 this.processEvent("touchstart", e);
596 onContextMenu : function(e, t){
597 this.processEvent("contextmenu", e);
601 onDblClick : function(e){
602 this.processEvent("dblclick", e);
606 walkCells : function(row, col, step, fn, scope){
607 var cm = this.colModel, clen = cm.getColumnCount();
608 var ds = this.dataSource, rlen = ds.getCount(), first = true;
620 if(fn.call(scope || this, row, col, cm) === true){
638 if(fn.call(scope || this, row, col, cm) === true){
650 getSelections : function(){
651 return this.selModel.getSelections();
655 * Causes the grid to manually recalculate its dimensions. Generally this is done automatically,
656 * but if manual update is required this method will initiate it.
658 autoSize : function(){
661 if(this.view.adjustForScroll){
662 this.view.adjustForScroll();
668 * Returns the grid's underlying element.
669 * @return {Element} The element
671 getGridEl : function(){
672 return this.container;
675 // private for compatibility, overridden by editor grid
676 stopEditing : function(){},
679 * Returns the grid's SelectionModel.
680 * @return {SelectionModel}
682 getSelectionModel : function(){
684 this.selModel = new Roo.grid.RowSelectionModel();
686 return this.selModel;
690 * Returns the grid's DataSource.
691 * @return {DataSource}
693 getDataSource : function(){
694 return this.dataSource;
698 * Returns the grid's ColumnModel.
699 * @return {ColumnModel}
701 getColumnModel : function(){
702 return this.colModel;
706 * Returns the grid's GridView object.
709 getView : function(){
711 this.view = new Roo.grid.GridView(this.viewConfig);
716 * Called to get grid's drag proxy text, by default returns this.ddText.
719 getDragDropText : function(){
720 var count = this.selModel.getCount();
721 return String.format(this.ddText, count, count == 1 ? '' : 's');
725 * Configures the text is the drag proxy (defaults to "%0 selected row(s)").
726 * %0 is replaced with the number of selected rows.
729 Roo.grid.Grid.prototype.ddText = "{0} selected row{1}";