📄 gridview.js
字号:
/*
* Ext JS Library 2.2.1
* Copyright(c) 2006-2009, Ext JS, LLC.
* licensing@extjs.com
*
* http://extjs.com/license
*/
/**
* @class Ext.grid.GridView
* @extends Ext.util.Observable
* <p>This class encapsulates the user interface of an {@link Ext.grid.GridPanel}.
* Methods of this class may be used to access user interface elements to enable
* special display effects. Do not change the DOM structure of the user interface.</p>
* <p>This class does not provide ways to manipulate the underlying data. The data
* model of a Grid is held in an {@link Ext.data.Store}.</p>
* @constructor
* @param {Object} config
*/
Ext.grid.GridView = function(config){
Ext.apply(this, config);
// These events are only used internally by the grid components
this.addEvents(
/**
* @event beforerowremoved
* Internal UI Event. Fired before a row is removed.
* @param {Ext.grid.GridView} view
* @param {Number} rowIndex The index of the row to be removed.
* @param {Ext.data.Record} record The Record to be removed
*/
"beforerowremoved",
/**
* @event beforerowsinserted
* Internal UI Event. Fired before rows are inserted.
* @param {Ext.grid.GridView} view
* @param {Number} firstRow The index of the first row to be inserted.
* @param {Number} lastRow The index of the last row to be inserted.
*/
"beforerowsinserted",
/**
* @event beforerefresh
* Internal UI Event. Fired before the view is refreshed.
* @param {Ext.grid.GridView} view
*/
"beforerefresh",
/**
* @event rowremoved
* Internal UI Event. Fired after a row is removed.
* @param {Ext.grid.GridView} view
* @param {Number} rowIndex The index of the row that was removed.
* @param {Ext.data.Record} record The Record that was removed
*/
"rowremoved",
/**
* @event rowsinserted
* Internal UI Event. Fired after rows are inserted.
* @param {Ext.grid.GridView} view
* @param {Number} firstRow The index of the first inserted.
* @param {Number} lastRow The index of the last row inserted.
*/
"rowsinserted",
/**
* @event rowupdated
* Internal UI Event. Fired after a row has been updated.
* @param {Ext.grid.GridView} view
* @param {Number} firstRow The index of the row updated.
* @param {Ext.data.record} record The Record backing the row updated.
*/
"rowupdated",
/**
* @event refresh
* Internal UI Event. Fired after the GridView's body has been refreshed.
* @param {Ext.grid.GridView} view
*/
"refresh"
);
Ext.grid.GridView.superclass.constructor.call(this);
};
Ext.extend(Ext.grid.GridView, Ext.util.Observable, {
/**
* Override this function to apply custom CSS classes to rows during rendering. You can also supply custom
* parameters to the row template for the current row to customize how it is rendered using the <b>rowParams</b>
* parameter. This function should return the CSS class name (or empty string '' for none) that will be added
* to the row's wrapping div. To apply multiple class names, simply return them space-delimited within the string
* (e.g., 'my-class another-class').
* @param {Record} record The {@link Ext.data.Record} corresponding to the current row
* @param {Number} index The row index
* @param {Object} rowParams A config object that is passed to the row template during rendering that allows
* customization of various aspects of a body row, if applicable. Note that this object will only be applied if
* {@link #enableRowBody} = true, otherwise it will be ignored. The object may contain any of these properties:<ul>
* <li><code>body</code> : String <div class="sub-desc">An HTML fragment to be rendered as the cell's body content (defaults to '').</div></li>
* <li><code>bodyStyle</code> : String <div class="sub-desc">A CSS style string that will be applied to the row's TR style attribute (defaults to '').</div></li>
* <li><code>cols</code> : Number <div class="sub-desc">The column count to apply to the body row's TD colspan attribute (defaults to the current
* column count of the grid).</div></li>
* </ul>
* @param {Store} store The {@link Ext.data.Store} this grid is bound to
* @method getRowClass
* @return {String} a CSS class name to add to the row.
*/
/**
* @cfg {Boolean} enableRowBody True to add a second TR element per row that can be used to provide a row body
* that spans beneath the data row. Use the {@link #getRowClass} method's rowParams config to customize the row body.
*/
/**
* @cfg {String} emptyText Default text to display in the grid body when no rows are available (defaults to '').
*/
/**
* @property dragZone
* @type Ext.grid.GridDragZone
* <p><b>This will only be present if the owning GridPanel was configured with {@link Ext.grid.GridPanel#enableDragDrop enableDragDrop} <tt>true</tt>.</b></p>
* <p><b>This will only be present after the owning GridPanel has been rendered</b>.</p>
* <p>A customized implementation of a {@link Ext.dd.DragZone DragZone} which provides default implementations of the
* template methods of DragZone to enable dragging of the selected rows of a GridPanel. See {@link Ext.grid.GridDragZone} for details.</p>
*/
/**
* @cfg {Boolean} deferEmptyText True to defer emptyText being applied until the store's first load
*/
deferEmptyText: true,
/**
* The amount of space to reserve for the scrollbar (defaults to 19 pixels)
* @type Number
*/
scrollOffset: 19,
/**
* @cfg {Boolean} autoFill True to auto expand the columns to fit the grid <b>when the grid is created</b>.
*/
autoFill: false,
/**
* @cfg {Boolean} forceFit True to auto expand/contract the size of the columns to fit the grid width and prevent horizontal scrolling.
* This option overrides any (@link Ext.grid.ColumnModel#width width} settings in the ColumnModel.
*/
forceFit: false,
/**
* The CSS classes applied to a header when it is sorted. (defaults to ["sort-asc", "sort-desc"])
* @type Array
*/
sortClasses : ["sort-asc", "sort-desc"],
/**
* The text displayed in the "Sort Ascending" menu item
* @type String
*/
sortAscText : "Sort Ascending",
/**
* The text displayed in the "Sort Descending" menu item
* @type String
*/
sortDescText : "Sort Descending",
/**
* The text displayed in the "Columns" menu item
* @type String
*/
columnsText : "Columns",
// private
borderWidth: 2,
tdClass: 'x-grid3-cell',
hdCls: 'x-grid3-hd',
/**
* @cfg {Number} cellSelectorDepth The number of levels to search for cells in event delegation (defaults to 4)
*/
cellSelectorDepth: 4,
/**
* @cfg {Number} rowSelectorDepth The number of levels to search for rows in event delegation (defaults to 10)
*/
rowSelectorDepth: 10,
/**
* @cfg {String} cellSelector The selector used to find cells internally
*/
cellSelector: 'td.x-grid3-cell',
/**
* @cfg {String} rowSelector The selector used to find rows internally
*/
rowSelector: 'div.x-grid3-row',
/* -------------------------------- UI Specific ----------------------------- */
// private
initTemplates : function(){
var ts = this.templates || {};
if(!ts.master){
ts.master = new Ext.Template(
'<div class="x-grid3" hidefocus="true">',
'<div class="x-grid3-viewport">',
'<div class="x-grid3-header"><div class="x-grid3-header-inner"><div class="x-grid3-header-offset">{header}</div></div><div class="x-clear"></div></div>',
'<div class="x-grid3-scroller"><div class="x-grid3-body">{body}</div><a href="#" class="x-grid3-focus" tabIndex="-1"></a></div>',
"</div>",
'<div class="x-grid3-resize-marker"> </div>',
'<div class="x-grid3-resize-proxy"> </div>',
"</div>"
);
}
if(!ts.header){
ts.header = new Ext.Template(
'<table border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
'<thead><tr class="x-grid3-hd-row">{cells}</tr></thead>',
"</table>"
);
}
if(!ts.hcell){
ts.hcell = new Ext.Template(
'<td class="x-grid3-hd x-grid3-cell x-grid3-td-{id} {css}" style="{style}"><div {tooltip} {attr} class="x-grid3-hd-inner x-grid3-hd-{id}" unselectable="on" style="{istyle}">', this.grid.enableHdMenu ? '<a class="x-grid3-hd-btn" href="#"></a>' : '',
'{value}<img class="x-grid3-sort-icon" src="', Ext.BLANK_IMAGE_URL, '" />',
"</div></td>"
);
}
if(!ts.body){
ts.body = new Ext.Template('{rows}');
}
if(!ts.row){
ts.row = new Ext.Template(
'<div class="x-grid3-row {alt}" style="{tstyle}"><table class="x-grid3-row-table" border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
'<tbody><tr>{cells}</tr>',
(this.enableRowBody ? '<tr class="x-grid3-row-body-tr" style="{bodyStyle}"><td colspan="{cols}" class="x-grid3-body-cell" tabIndex="0" hidefocus="on"><div class="x-grid3-row-body">{body}</div></td></tr>' : ''),
'</tbody></table></div>'
);
}
if(!ts.cell){
ts.cell = new Ext.Template(
'<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} {css}" style="{style}" tabIndex="0" {cellAttr}>',
'<div class="x-grid3-cell-inner x-grid3-col-{id}" unselectable="on" {attr}>{value}</div>',
"</td>"
);
}
for(var k in ts){
var t = ts[k];
if(t && typeof t.compile == 'function' && !t.compiled){
t.disableFormats = true;
t.compile();
}
}
this.templates = ts;
this.colRe = new RegExp("x-grid3-td-([^\\s]+)", "");
},
// private
fly : function(el){
if(!this._flyweight){
this._flyweight = new Ext.Element.Flyweight(document.body);
}
this._flyweight.dom = el;
return this._flyweight;
},
// private
getEditorParent : function(){
return this.scroller.dom;
},
// private
initElements : function(){
var E = Ext.Element;
var el = this.grid.getGridEl().dom.firstChild;
var cs = el.childNodes;
this.el = new E(el);
this.mainWrap = new E(cs[0]);
this.mainHd = new E(this.mainWrap.dom.firstChild);
if(this.grid.hideHeaders){
this.mainHd.setDisplayed(false);
}
this.innerHd = this.mainHd.dom.firstChild;
this.scroller = new E(this.mainWrap.dom.childNodes[1]);
if(this.forceFit){
this.scroller.setStyle('overflow-x', 'hidden');
}
/**
* The GridView's body Element which encapsulates all rows in the Grid. {@link Ext.Element Element}. Read-only.
* <p>This Element is only available after the GridPanel has been rendered.</p>
* @type Ext.Element
* @property mainBody
*/
this.mainBody = new E(this.scroller.dom.firstChild);
this.focusEl = new E(this.scroller.dom.childNodes[1]);
this.focusEl.swallowEvent("click", true);
this.resizeMarker = new E(cs[1]);
this.resizeProxy = new E(cs[2]);
},
// private
getRows : function(){
return this.hasRows() ? this.mainBody.dom.childNodes : [];
},
// finder methods, used with delegation
// private
findCell : function(el){
if(!el){
return false;
}
return this.fly(el).findParent(this.cellSelector, this.cellSelectorDepth);
},
// private
findCellIndex : function(el, requiredCls){
var cell = this.findCell(el);
if(cell && (!requiredCls || this.fly(cell).hasClass(requiredCls))){
return this.getCellIndex(cell);
}
return false;
},
// private
getCellIndex : function(el){
if(el){
var m = el.className.match(this.colRe);
if(m && m[1]){
return this.cm.getIndexById(m[1]);
}
}
return false;
},
// private
findHeaderCell : function(el){
var cell = this.findCell(el);
return cell && this.fly(cell).hasClass(this.hdCls) ? cell : null;
},
// private
findHeaderIndex : function(el){
return this.findCellIndex(el, this.hdCls);
},
/**
* Return the HtmlElement representing the grid row which contains the passed element.
* @param {Element} el The target element
* @return The row element, or null if the target element is not within a row of this GridView.
*/
findRow : function(el){
if(!el){
return false;
}
return this.fly(el).findParent(this.rowSelector, this.rowSelectorDepth);
},
/**
* Return the index of the grid row which contains the passed element.
* @param {Element} el The target element
* @return The row index, or <b>false</b> if the target element is not within a row of this GridView.
*/
findRowIndex : function(el){
var r = this.findRow(el);
return r ? r.rowIndex : false;
},
// getter methods for fetching elements dynamically in the grid
/**
* Return the <TR> HtmlElement which represents a Grid row for the specified index.
* @param {Number} index The row index
* @return {HtmlElement} The <TR> element.
*/
getRow : function(row){
return this.getRows()[row];
},
/**
* Returns the grid's <TD> HtmlElement at the specified coordinates.
* @param {Number} row The row index in which to find the cell.
* @param {Number} col The column index of the cell.
* @return {HtmlElement} The <TD> at the specified coordinates.
*/
getCell : function(row, col){
return this.getRow(row).getElementsByTagName('td')[col];
},
/**
* Return the <TD> HtmlElement which represents the Grid's header cell for the specified column index.
* @param {Number} index The column index
* @return {HtmlElement} The <TD> element.
*/
getHeaderCell : function(index){
return this.mainHd.dom.getElementsByTagName('td')[index];
},
// manipulating elements
// private - use getRowClass to apply custom row classes
addRowClass : function(row, cls){
var r = this.getRow(row);
if(r){
this.fly(r).addClass(cls);
}
},
// private
removeRowClass : function(row, cls){
var r = this.getRow(row);
if(r){
this.fly(r).removeClass(cls);
}
},
// private
removeRow : function(row){
Ext.removeNode(this.getRow(row));
this.syncFocusEl(row);
},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -