3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.data.Field"></div>/**
9 * @class Ext.data.Field
10 * <p>This class encapsulates the field definition information specified in the field definition objects
11 * passed to {@link Ext.data.Record#create}.</p>
12 * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}
13 * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>
15 Ext.data.Field = function(config){
16 if(typeof config == "string"){
17 config = {name: config};
19 Ext.apply(this, config);
25 var st = Ext.data.SortTypes;
26 // named sortTypes are supported, here we look them up
27 if(typeof this.sortType == "string"){
28 this.sortType = st[this.sortType];
31 // set default sortType for strings and dates
35 this.sortType = st.asUCString;
38 this.sortType = st.asDate;
41 this.sortType = st.none;
46 var stripRe = /[\$,%]/g;
48 // prebuilt conversion function for this field, instead of
49 // switching every time we're reading a value
51 var cv, dateFormat = this.dateFormat;
56 cv = function(v){ return v; };
59 cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };
63 return v !== undefined && v !== null && v !== '' ?
64 parseInt(String(v).replace(stripRe, ""), 10) : '';
69 return v !== undefined && v !== null && v !== '' ?
70 parseFloat(String(v).replace(stripRe, ""), 10) : '';
75 cv = function(v){ return v === true || v === "true" || v == 1; };
86 if(dateFormat == "timestamp"){
87 return new Date(v*1000);
89 if(dateFormat == "time"){
90 return new Date(parseInt(v, 10));
92 return Date.parseDate(v, dateFormat);
94 var parsed = Date.parse(v);
95 return parsed ? new Date(parsed) : null;
104 Ext.data.Field.prototype = {
105 <div id="cfg-Ext.data.Field-name"></div>/**
107 * The name by which the field is referenced within the Record. This is referenced by, for example,
108 * the <tt>dataIndex</tt> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
109 * <p>Note: In the simplest case, if no properties other than <tt>name</tt> are required, a field
110 * definition may consist of just a String for the field name.</p>
112 <div id="cfg-Ext.data.Field-type"></div>/**
114 * (Optional) The data type for conversion to displayable value if <tt>{@link Ext.data.Field#convert convert}</tt>
115 * has not been specified. Possible values are
116 * <div class="mdetail-params"><ul>
117 * <li>auto (Default, implies no conversion)</li>
122 * <li>date</li></ul></div>
124 <div id="cfg-Ext.data.Field-convert"></div>/**
125 * @cfg {Function} convert
126 * (Optional) A function which converts the value provided by the Reader into an object that will be stored
127 * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
128 * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
129 * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
130 * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
131 * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
132 * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
135 // example of convert function
136 function fullName(v, record){
137 return record.name.last + ', ' + record.name.first;
140 function location(v, record){
141 return !record.city ? '' : (record.city + ', ' + record.state);
144 var Dude = Ext.data.Record.create([
145 {name: 'fullname', convert: fullName},
146 {name: 'firstname', mapping: 'name.first'},
147 {name: 'lastname', mapping: 'name.last'},
148 {name: 'city', defaultValue: 'homeless'},
150 {name: 'location', convert: location}
153 // create the data store
154 var store = new Ext.data.Store({
155 reader: new Ext.data.JsonReader(
159 totalProperty: 'total'
167 name: { first: 'Fat', last: 'Albert' }
168 // notice no city, state provided in data object
171 name: { first: 'Barney', last: 'Rubble' },
172 city: 'Bedrock', state: 'Stoneridge'
175 name: { first: 'Cliff', last: 'Claven' },
176 city: 'Boston', state: 'MA'
181 <div id="cfg-Ext.data.Field-dateFormat"></div>/**
182 * @cfg {String} dateFormat
183 * (Optional) A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
184 * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
185 * javascript millisecond timestamp.
188 <div id="cfg-Ext.data.Field-defaultValue"></div>/**
189 * @cfg {Mixed} defaultValue
190 * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
191 * when the item referenced by the <tt>{@link Ext.data.Field#mapping mapping}</tt> does not exist in the data
192 * object (i.e. undefined). (defaults to "")
195 <div id="cfg-Ext.data.Field-mapping"></div>/**
196 * @cfg {String/Number} mapping
197 * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
198 * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
199 * If the path expression is the same as the field name, the mapping may be omitted.</p>
200 * <p>The form of the mapping expression depends on the Reader being used.</p>
201 * <div class="mdetail-params"><ul>
202 * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
203 * expression to reference the data from an element of the data item's {@link Ext.data.JsonReader#root root} Array. Defaults to the field name.</div></li>
204 * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
205 * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
206 * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
207 * of the field's value. Defaults to the field specification's Array position.</div></li>
209 * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
210 * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
211 * return the desired data.</p>
214 <div id="cfg-Ext.data.Field-sortType"></div>/**
215 * @cfg {Function} sortType
216 * (Optional) A function which converts a Field's value to a comparable value in order to ensure
217 * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
218 * sort example:<pre><code>
219 // current sort after sort we want
220 // +-+------+ +-+------+
221 // |1|First | |1|First |
222 // |2|Last | |3|Second|
223 // |3|Second| |2|Last |
224 // +-+------+ +-+------+
226 sortType: function(value) {
227 switch (value.toLowerCase()) // native toLowerCase():
229 case 'first': return 1;
230 case 'second': return 2;
237 <div id="cfg-Ext.data.Field-sortDir"></div>/**
238 * @cfg {String} sortDir
239 * (Optional) Initial direction to sort (<tt>"ASC"</tt> or <tt>"DESC"</tt>). Defaults to
243 <div id="cfg-Ext.data.Field-allowBlank"></div>/**
244 * @cfg {Boolean} allowBlank
245 * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <tt>true</tt>.
246 * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
247 * to evaluate to <tt>false</tt>.