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;
59 Roo.log(this.colModel);
63 this.selModel = this.sm;
68 this.selModel = Roo.factory(this.selModel, Roo.grid);
69 this.sm = this.selModel;
70 this.sm.xmodule = this.xmodule || false;
72 if (typeof(this.colModel.config) == 'undefined') {
73 this.colModel = new Roo.grid.ColumnModel(this.colModel);
74 this.cm = this.colModel;
75 this.cm.xmodule = this.xmodule || false;
77 if (this.dataSource) {
78 this.dataSource= Roo.factory(this.dataSource, Roo.data);
79 this.ds = this.dataSource;
80 this.ds.xmodule = this.xmodule || false;
87 this.container.setWidth(this.width);
91 this.container.setHeight(this.height);
98 * The raw click event for the entire grid.
99 * @param {Roo.EventObject} e
104 * The raw dblclick event for the entire grid.
105 * @param {Roo.EventObject} e
110 * The raw contextmenu event for the entire grid.
111 * @param {Roo.EventObject} e
113 "contextmenu" : true,
116 * The raw mousedown event for the entire grid.
117 * @param {Roo.EventObject} e
122 * The raw mouseup event for the entire grid.
123 * @param {Roo.EventObject} e
128 * The raw mouseover event for the entire grid.
129 * @param {Roo.EventObject} e
134 * The raw mouseout event for the entire grid.
135 * @param {Roo.EventObject} e
140 * The raw keypress event for the entire grid.
141 * @param {Roo.EventObject} e
146 * The raw keydown event for the entire grid.
147 * @param {Roo.EventObject} e
155 * Fires when a cell is clicked
157 * @param {Number} rowIndex
158 * @param {Number} columnIndex
159 * @param {Roo.EventObject} e
163 * @event celldblclick
164 * Fires when a cell is double clicked
166 * @param {Number} rowIndex
167 * @param {Number} columnIndex
168 * @param {Roo.EventObject} e
170 "celldblclick" : true,
173 * Fires when a row is clicked
175 * @param {Number} rowIndex
176 * @param {Roo.EventObject} e
181 * Fires when a row is double clicked
183 * @param {Number} rowIndex
184 * @param {Roo.EventObject} e
186 "rowdblclick" : true,
189 * Fires when a header is clicked
191 * @param {Number} columnIndex
192 * @param {Roo.EventObject} e
194 "headerclick" : true,
196 * @event headerdblclick
197 * Fires when a header cell is double clicked
199 * @param {Number} columnIndex
200 * @param {Roo.EventObject} e
202 "headerdblclick" : true,
204 * @event rowcontextmenu
205 * Fires when a row is right clicked
207 * @param {Number} rowIndex
208 * @param {Roo.EventObject} e
210 "rowcontextmenu" : true,
212 * @event cellcontextmenu
213 * Fires when a cell is right clicked
215 * @param {Number} rowIndex
216 * @param {Number} cellIndex
217 * @param {Roo.EventObject} e
219 "cellcontextmenu" : true,
221 * @event headercontextmenu
222 * Fires when a header is right clicked
224 * @param {Number} columnIndex
225 * @param {Roo.EventObject} e
227 "headercontextmenu" : true,
230 * Fires when the body element is scrolled
231 * @param {Number} scrollLeft
232 * @param {Number} scrollTop
236 * @event columnresize
237 * Fires when the user resizes a column
238 * @param {Number} columnIndex
239 * @param {Number} newSize
241 "columnresize" : true,
244 * Fires when the user moves a column
245 * @param {Number} oldIndex
246 * @param {Number} newIndex
251 * Fires when row(s) start being dragged
253 * @param {Roo.GridDD} dd The drag drop object
254 * @param {event} e The raw browser event
259 * Fires when a drag operation is complete
261 * @param {Roo.GridDD} dd The drag drop object
262 * @param {event} e The raw browser event
267 * Fires when dragged row(s) are dropped on a valid DD target
269 * @param {Roo.GridDD} dd The drag drop object
270 * @param {String} targetId The target drag drop object
271 * @param {event} e The raw browser event
276 * Fires while row(s) are being dragged. "targetId" is the id of the Yahoo.util.DD object the selected rows are being dragged over.
278 * @param {Roo.GridDD} dd The drag drop object
279 * @param {String} targetId The target drag drop object
280 * @param {event} e The raw browser event
285 * Fires when the dragged row(s) first cross another DD target while being dragged
287 * @param {Roo.GridDD} dd The drag drop object
288 * @param {String} targetId The target drag drop object
289 * @param {event} e The raw browser event
294 * Fires when the dragged row(s) leave another DD target while being dragged
296 * @param {Roo.GridDD} dd The drag drop object
297 * @param {String} targetId The target drag drop object
298 * @param {event} e The raw browser event
303 * Fires when a row is rendered, so you can change add a style to it.
304 * @param {GridView} gridview The grid view
305 * @param {Object} rowcfg contains record rowIndex and rowClass - set rowClass to add a style.
311 * Fires when the grid is rendered
317 Roo.grid.Grid.superclass.constructor.call(this);
319 Roo.extend(Roo.grid.Grid, Roo.util.Observable, {
322 * @cfg {String} ddGroup - drag drop group.
326 * @cfg {Number} minColumnWidth The minimum width a column can be resized to. Default is 25.
331 * @cfg {Boolean} autoSizeColumns True to automatically resize the columns to fit their content
332 * <b>on initial render.</b> It is more efficient to explicitly size the columns
333 * through the ColumnModel's {@link Roo.grid.ColumnModel#width} config option. Default is false.
335 autoSizeColumns : false,
338 * @cfg {Boolean} autoSizeHeaders True to measure headers with column data when auto sizing columns. Default is true.
340 autoSizeHeaders : true,
343 * @cfg {Boolean} monitorWindowResize True to autoSize the grid when the window resizes. Default is true.
345 monitorWindowResize : true,
348 * @cfg {Boolean} maxRowsToMeasure If autoSizeColumns is on, maxRowsToMeasure can be used to limit the number of
349 * rows measured to get a columns size. Default is 0 (all rows).
351 maxRowsToMeasure : 0,
354 * @cfg {Boolean} trackMouseOver True to highlight rows when the mouse is over. Default is true.
356 trackMouseOver : true,
359 * @cfg {Boolean} enableDrag True to enable drag of rows. Default is false. (double check if this is needed?)
363 * @cfg {Boolean} enableDragDrop True to enable drag and drop of rows. Default is false.
365 enableDragDrop : false,
368 * @cfg {Boolean} enableColumnMove True to enable drag and drop reorder of columns. Default is true.
370 enableColumnMove : true,
373 * @cfg {Boolean} enableColumnHide True to enable hiding of columns with the header context menu. Default is true.
375 enableColumnHide : true,
378 * @cfg {Boolean} enableRowHeightSync True to manually sync row heights across locked and not locked rows. Default is false.
380 enableRowHeightSync : false,
383 * @cfg {Boolean} stripeRows True to stripe the rows. Default is true.
388 * @cfg {Boolean} autoHeight True to fit the height of the grid container to the height of the data. Default is false.
393 * @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.
395 autoExpandColumn : false,
398 * @cfg {Number} autoExpandMin The minimum width the autoExpandColumn can have (if enabled).
404 * @cfg {Number} autoExpandMax The maximum width the autoExpandColumn can have (if enabled). Default is 1000.
406 autoExpandMax : 1000,
409 * @cfg {Object} view The {@link Roo.grid.GridView} used by the grid. This can be set before a call to render().
414 * @cfg {Object} loadMask An {@link Roo.LoadMask} config or true to mask the grid while loading. Default is false.
418 * @cfg {Roo.dd.DropTarget} dropTarget An {@link Roo.dd.DropTarget} config
428 * @cfg {Boolean} autoWidth True to set the grid's width to the default total width of the grid's columns instead
429 * of a fixed width. Default is false.
432 * @cfg {Number} maxHeight Sets the maximum height of the grid - ignored if autoHeight is not on.
435 * Called once after all setup has been completed and the grid is ready to be rendered.
436 * @return {Roo.grid.Grid} this
440 var c = this.container;
441 // try to detect autoHeight/width mode
442 if((!c.dom.offsetHeight || c.dom.offsetHeight < 20) || c.getStyle("height") == "auto"){
443 this.autoHeight = true;
445 var view = this.getView();
448 c.on("click", this.onClick, this);
449 c.on("dblclick", this.onDblClick, this);
450 c.on("contextmenu", this.onContextMenu, this);
451 c.on("keydown", this.onKeyDown, this);
453 c.on("touchstart", this.onTouchStart, this);
456 this.relayEvents(c, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
458 this.getSelectionModel().init(this);
463 this.loadMask = new Roo.LoadMask(this.container,
464 Roo.apply({store:this.dataSource}, this.loadMask));
468 if (this.toolbar && this.toolbar.xtype) {
469 this.toolbar.container = this.getView().getHeaderPanel(true);
470 this.toolbar = new Roo.Toolbar(this.toolbar);
472 if (this.footer && this.footer.xtype) {
473 this.footer.dataSource = this.getDataSource();
474 this.footer.container = this.getView().getFooterPanel(true);
475 this.footer = Roo.factory(this.footer, Roo);
477 if (this.dropTarget && this.dropTarget.xtype) {
478 delete this.dropTarget.xtype;
479 this.dropTarget = new Roo.dd.DropTarget(this.getView().mainBody, this.dropTarget);
483 this.rendered = true;
484 this.fireEvent('render', this);
489 * Reconfigures the grid to use a different Store and Column Model.
490 * The View will be bound to the new objects and refreshed.
491 * @param {Roo.data.Store} dataSource The new {@link Roo.data.Store} object
492 * @param {Roo.grid.ColumnModel} The new {@link Roo.grid.ColumnModel} object
494 reconfigure : function(dataSource, colModel){
496 this.loadMask.destroy();
497 this.loadMask = new Roo.LoadMask(this.container,
498 Roo.apply({store:dataSource}, this.loadMask));
500 this.view.bind(dataSource, colModel);
501 this.dataSource = dataSource;
502 this.colModel = colModel;
503 this.view.refresh(true);
507 onKeyDown : function(e){
508 this.fireEvent("keydown", e);
513 * @param {Boolean} removeEl True to remove the element
515 destroy : function(removeEl, keepListeners){
517 this.loadMask.destroy();
519 var c = this.container;
520 c.removeAllListeners();
522 this.colModel.purgeListeners();
524 this.purgeListeners();
527 if(removeEl === true){
533 processEvent : function(name, e){
534 // does this fire select???
535 //Roo.log('grid:processEvent ' + name);
537 if (name != 'touchstart' ) {
538 this.fireEvent(name, e);
541 var t = e.getTarget();
543 var header = v.findHeaderIndex(t);
544 if(header !== false){
545 var ename = name == 'touchstart' ? 'click' : name;
547 this.fireEvent("header" + ename, this, header, e);
549 var row = v.findRowIndex(t);
550 var cell = v.findCellIndex(t);
551 if (name == 'touchstart') {
552 // first touch is always a click.
553 // hopefull this happens after selection is updated.?
556 if (typeof(this.selModel.getSelectedCell) != 'undefined') {
557 var cs = this.selModel.getSelectedCell();
558 if (row == cs[0] && cell == cs[1]){
562 if (typeof(this.selModel.getSelections) != 'undefined') {
563 var cs = this.selModel.getSelections();
564 var ds = this.dataSource;
565 if (cs.length == 1 && ds.getAt(row) == cs[0]){
576 this.fireEvent("row" + name, this, row, e);
578 this.fireEvent("cell" + name, this, row, cell, e);
585 onClick : function(e){
586 this.processEvent("click", e);
589 onTouchStart : function(e){
590 this.processEvent("touchstart", e);
594 onContextMenu : function(e, t){
595 this.processEvent("contextmenu", e);
599 onDblClick : function(e){
600 this.processEvent("dblclick", e);
604 walkCells : function(row, col, step, fn, scope){
605 var cm = this.colModel, clen = cm.getColumnCount();
606 var ds = this.dataSource, rlen = ds.getCount(), first = true;
618 if(fn.call(scope || this, row, col, cm) === true){
636 if(fn.call(scope || this, row, col, cm) === true){
648 getSelections : function(){
649 return this.selModel.getSelections();
653 * Causes the grid to manually recalculate its dimensions. Generally this is done automatically,
654 * but if manual update is required this method will initiate it.
656 autoSize : function(){
659 if(this.view.adjustForScroll){
660 this.view.adjustForScroll();
666 * Returns the grid's underlying element.
667 * @return {Element} The element
669 getGridEl : function(){
670 return this.container;
673 // private for compatibility, overridden by editor grid
674 stopEditing : function(){},
677 * Returns the grid's SelectionModel.
678 * @return {SelectionModel}
680 getSelectionModel : function(){
682 this.selModel = new Roo.grid.RowSelectionModel();
684 return this.selModel;
688 * Returns the grid's DataSource.
689 * @return {DataSource}
691 getDataSource : function(){
692 return this.dataSource;
696 * Returns the grid's ColumnModel.
697 * @return {ColumnModel}
699 getColumnModel : function(){
700 return this.colModel;
704 * Returns the grid's GridView object.
707 getView : function(){
709 this.view = new Roo.grid.GridView(this.viewConfig);
714 * Called to get grid's drag proxy text, by default returns this.ddText.
717 getDragDropText : function(){
718 var count = this.selModel.getCount();
719 return String.format(this.ddText, count, count == 1 ? '' : 's');
723 * Configures the text is the drag proxy (defaults to "%0 selected row(s)").
724 * %0 is replaced with the number of selected rows.
727 Roo.grid.Grid.prototype.ddText = "{0} selected row{1}";