3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.grid.PropertyRecord
9 * A specific {@link Ext.data.Record} type that represents a name/value pair and is made to work with the
10 * {@link Ext.grid.PropertyGrid}. Typically, PropertyRecords do not need to be created directly as they can be
11 * created implicitly by simply using the appropriate data configs either via the {@link Ext.grid.PropertyGrid#source}
12 * config property or by calling {@link Ext.grid.PropertyGrid#setSource}. However, if the need arises, these records
13 * can also be created explicitly as shwon below. Example usage:
15 var rec = new Ext.grid.PropertyRecord({
17 value: new Date(Date.parse('05/26/1972'))
19 // Add record to an already populated grid
20 grid.store.addSorted(rec);
23 * @param {Object} config A data object in the format: {name: [name], value: [value]}. The specified value's type
24 * will be read automatically by the grid to determine the type of editor to use when displaying it.
26 Ext.grid.PropertyRecord = Ext.data.Record.create([
27 {name:'name',type:'string'}, 'value'
31 * @class Ext.grid.PropertyStore
32 * @extends Ext.util.Observable
33 * A custom wrapper for the {@link Ext.grid.PropertyGrid}'s {@link Ext.data.Store}. This class handles the mapping
34 * between the custom data source objects supported by the grid and the {@link Ext.grid.PropertyRecord} format
35 * required for compatibility with the underlying store. Generally this class should not need to be used directly --
36 * the grid's data should be accessed from the underlying store via the {@link #store} property.
38 * @param {Ext.grid.Grid} grid The grid this store will be bound to
39 * @param {Object} source The source data config object
41 Ext.grid.PropertyStore = function(grid, source){
43 this.store = new Ext.data.Store({
44 recordType : Ext.grid.PropertyRecord
46 this.store.on('update', this.onUpdate, this);
48 this.setSource(source);
50 Ext.grid.PropertyStore.superclass.constructor.call(this);
52 Ext.extend(Ext.grid.PropertyStore, Ext.util.Observable, {
53 // protected - should only be called by the grid. Use grid.setSource instead.
54 setSource : function(o){
56 this.store.removeAll();
59 if(this.isEditableValue(o[k])){
60 data.push(new Ext.grid.PropertyRecord({name: k, value: o[k]}, k));
63 this.store.loadRecords({records: data}, {}, true);
67 onUpdate : function(ds, record, type){
68 if(type == Ext.data.Record.EDIT){
69 var v = record.data.value;
70 var oldValue = record.modified.value;
71 if(this.grid.fireEvent('beforepropertychange', this.source, record.id, v, oldValue) !== false){
72 this.source[record.id] = v;
74 this.grid.fireEvent('propertychange', this.source, record.id, v, oldValue);
82 getProperty : function(row){
83 return this.store.getAt(row);
87 isEditableValue: function(val){
91 return !(Ext.isObject(val) || Ext.isFunction(val));
95 setValue : function(prop, value){
96 this.source[prop] = value;
97 this.store.getById(prop).set('value', value);
100 // protected - should only be called by the grid. Use grid.getSource instead.
101 getSource : function(){
107 * @class Ext.grid.PropertyColumnModel
108 * @extends Ext.grid.ColumnModel
109 * A custom column model for the {@link Ext.grid.PropertyGrid}. Generally it should not need to be used directly.
111 * @param {Ext.grid.Grid} grid The grid this store will be bound to
112 * @param {Object} source The source data config object
114 Ext.grid.PropertyColumnModel = function(grid, store){
119 g.PropertyColumnModel.superclass.constructor.call(this, [
120 {header: this.nameText, width:50, sortable: true, dataIndex:'name', id: 'name', menuDisabled:true},
121 {header: this.valueText, width:50, resizable:false, dataIndex: 'value', id: 'value', menuDisabled:true}
125 var bfield = new f.Field({
126 autoCreate: {tag: 'select', children: [
127 {tag: 'option', value: 'true', html: 'true'},
128 {tag: 'option', value: 'false', html: 'false'}
130 getValue : function(){
131 return this.el.dom.value == 'true';
135 'date' : new g.GridEditor(new f.DateField({selectOnFocus:true})),
136 'string' : new g.GridEditor(new f.TextField({selectOnFocus:true})),
137 'number' : new g.GridEditor(new f.NumberField({selectOnFocus:true, style:'text-align:left;'})),
138 'boolean' : new g.GridEditor(bfield, {
142 this.renderCellDelegate = this.renderCell.createDelegate(this);
143 this.renderPropDelegate = this.renderProp.createDelegate(this);
146 Ext.extend(Ext.grid.PropertyColumnModel, Ext.grid.ColumnModel, {
147 // private - strings used for locale support
150 dateFormat : 'm/j/Y',
153 renderDate : function(dateVal){
154 return dateVal.dateFormat(this.dateFormat);
158 renderBool : function(bVal){
159 return bVal ? 'true' : 'false';
163 isCellEditable : function(colIndex, rowIndex){
164 return colIndex == 1;
168 getRenderer : function(col){
170 this.renderCellDelegate : this.renderPropDelegate;
174 renderProp : function(v){
175 return this.getPropertyName(v);
179 renderCell : function(val){
182 rv = this.renderDate(val);
183 }else if(typeof val == 'boolean'){
184 rv = this.renderBool(val);
186 return Ext.util.Format.htmlEncode(rv);
190 getPropertyName : function(name){
191 var pn = this.grid.propertyNames;
192 return pn && pn[name] ? pn[name] : name;
196 getCellEditor : function(colIndex, rowIndex){
197 var p = this.store.getProperty(rowIndex),
200 if(this.grid.customEditors[n]){
201 return this.grid.customEditors[n];
204 return this.editors.date;
205 }else if(typeof val == 'number'){
206 return this.editors.number;
207 }else if(typeof val == 'boolean'){
208 return this.editors['boolean'];
210 return this.editors.string;
215 destroy : function(){
216 Ext.grid.PropertyColumnModel.superclass.destroy.call(this);
217 for(var ed in this.editors){
224 * @class Ext.grid.PropertyGrid
225 * @extends Ext.grid.EditorGridPanel
226 * A specialized grid implementation intended to mimic the traditional property grid as typically seen in
227 * development IDEs. Each row in the grid represents a property of some object, and the data is stored
228 * as a set of name/value pairs in {@link Ext.grid.PropertyRecord}s. Example usage:
230 var grid = new Ext.grid.PropertyGrid({
231 title: 'Properties Grid',
236 "(name)": "My Object",
237 "Created": new Date(Date.parse('10/15/2006')),
240 "Description": "A test object"
245 * @param {Object} config The grid config object
247 Ext.grid.PropertyGrid = Ext.extend(Ext.grid.EditorGridPanel, {
249 * @cfg {Object} propertyNames An object containing property name/display name pairs.
250 * If specified, the display name will be shown in the name column instead of the property name.
253 * @cfg {Object} source A data object to use as the data source of the grid (see {@link #setSource} for details).
256 * @cfg {Object} customEditors An object containing name/value pairs of custom editor type definitions that allow
257 * the grid to support additional types of editable fields. By default, the grid supports strongly-typed editing
258 * of strings, dates, numbers and booleans using built-in form editors, but any custom type can be supported and
259 * associated with a custom input control by specifying a custom editor. The name of the editor
260 * type should correspond with the name of the property that will use the editor. Example usage:
262 var grid = new Ext.grid.PropertyGrid({
265 'Start Time': new Ext.grid.GridEditor(new Ext.form.TimeField({selectOnFocus:true}))
268 'Start Time': '10:00 AM'
274 // private config overrides
275 enableColumnMove:false,
277 trackMouseOver: false,
279 enableHdMenu : false,
285 initComponent : function(){
286 this.customEditors = this.customEditors || {};
287 this.lastEditRow = null;
288 var store = new Ext.grid.PropertyStore(this);
289 this.propStore = store;
290 var cm = new Ext.grid.PropertyColumnModel(this, store);
291 store.store.sort('name', 'ASC');
294 * @event beforepropertychange
295 * Fires before a property value changes. Handlers can return false to cancel the property change
296 * (this will internally call {@link Ext.data.Record#reject} on the property's record).
297 * @param {Object} source The source data object for the grid (corresponds to the same object passed in
298 * as the {@link #source} config property).
299 * @param {String} recordId The record's id in the data store
300 * @param {Mixed} value The current edited property value
301 * @param {Mixed} oldValue The original property value prior to editing
303 'beforepropertychange',
305 * @event propertychange
306 * Fires after a property value has changed.
307 * @param {Object} source The source data object for the grid (corresponds to the same object passed in
308 * as the {@link #source} config property).
309 * @param {String} recordId The record's id in the data store
310 * @param {Mixed} value The current edited property value
311 * @param {Mixed} oldValue The original property value prior to editing
316 this.ds = store.store;
317 Ext.grid.PropertyGrid.superclass.initComponent.call(this);
319 this.mon(this.selModel, 'beforecellselect', function(sm, rowIndex, colIndex){
321 this.startEditing.defer(200, this, [rowIndex, 1]);
328 onRender : function(){
329 Ext.grid.PropertyGrid.superclass.onRender.apply(this, arguments);
331 this.getGridEl().addClass('x-props-grid');
335 afterRender: function(){
336 Ext.grid.PropertyGrid.superclass.afterRender.apply(this, arguments);
338 this.setSource(this.source);
343 * Sets the source data object containing the property data. The data object can contain one or more name/value
344 * pairs representing all of the properties of an object to display in the grid, and this data will automatically
345 * be loaded into the grid's {@link #store}. The values should be supplied in the proper data type if needed,
346 * otherwise string type will be assumed. If the grid already contains data, this method will replace any
347 * existing data. See also the {@link #source} config value. Example usage:
350 "(name)": "My Object",
351 "Created": new Date(Date.parse('10/15/2006')), // date type
352 "Available": false, // boolean type
353 "Version": .01, // decimal type
354 "Description": "A test object"
357 * @param {Object} source The data object
359 setSource : function(source){
360 this.propStore.setSource(source);
364 * Gets the source data object containing the property data. See {@link #setSource} for details regarding the
365 * format of the data object.
366 * @return {Object} The data object
368 getSource : function(){
369 return this.propStore.getSource();
372 Ext.reg("propertygrid", Ext.grid.PropertyGrid);