3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.data.Field"></div>/**
15 * @class Ext.data.Field
16 * <p>This class encapsulates the field definition information specified in the field definition objects
17 * passed to {@link Ext.data.Record#create}.</p>
18 * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}
19 * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>
21 Ext.data.Field = function(config){
22 if(typeof config == "string"){
23 config = {name: config};
25 Ext.apply(this, config);
31 var st = Ext.data.SortTypes;
32 // named sortTypes are supported, here we look them up
33 if(typeof this.sortType == "string"){
34 this.sortType = st[this.sortType];
37 // set default sortType for strings and dates
41 this.sortType = st.asUCString;
44 this.sortType = st.asDate;
47 this.sortType = st.none;
52 var stripRe = /[\$,%]/g;
54 // prebuilt conversion function for this field, instead of
55 // switching every time we're reading a value
57 var cv, dateFormat = this.dateFormat;
62 cv = function(v){ return v; };
65 cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };
69 return v !== undefined && v !== null && v !== '' ?
70 parseInt(String(v).replace(stripRe, ""), 10) : '';
75 return v !== undefined && v !== null && v !== '' ?
76 parseFloat(String(v).replace(stripRe, ""), 10) : '';
81 cv = function(v){ return v === true || v === "true" || v == 1; };
92 if(dateFormat == "timestamp"){
93 return new Date(v*1000);
95 if(dateFormat == "time"){
96 return new Date(parseInt(v, 10));
98 return Date.parseDate(v, dateFormat);
100 var parsed = Date.parse(v);
101 return parsed ? new Date(parsed) : null;
110 Ext.data.Field.prototype = {
111 <div id="cfg-Ext.data.Field-name"></div>/**
113 * The name by which the field is referenced within the Record. This is referenced by, for example,
114 * the <tt>dataIndex</tt> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
115 * <p>Note: In the simplest case, if no properties other than <tt>name</tt> are required, a field
116 * definition may consist of just a String for the field name.</p>
118 <div id="cfg-Ext.data.Field-type"></div>/**
120 * (Optional) The data type for conversion to displayable value if <tt>{@link Ext.data.Field#convert convert}</tt>
121 * has not been specified. Possible values are
122 * <div class="mdetail-params"><ul>
123 * <li>auto (Default, implies no conversion)</li>
128 * <li>date</li></ul></div>
130 <div id="cfg-Ext.data.Field-convert"></div>/**
131 * @cfg {Function} convert
132 * (Optional) A function which converts the value provided by the Reader into an object that will be stored
133 * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
134 * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
135 * the configured <tt>{@link Ext.data.Field#defaultValue defaultValue}</tt>.</div></li>
136 * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
137 * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
138 * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
141 // example of convert function
142 function fullName(v, record){
143 return record.name.last + ', ' + record.name.first;
146 function location(v, record){
147 return !record.city ? '' : (record.city + ', ' + record.state);
150 var Dude = Ext.data.Record.create([
151 {name: 'fullname', convert: fullName},
152 {name: 'firstname', mapping: 'name.first'},
153 {name: 'lastname', mapping: 'name.last'},
154 {name: 'city', defaultValue: 'homeless'},
156 {name: 'location', convert: location}
159 // create the data store
160 var store = new Ext.data.Store({
161 reader: new Ext.data.JsonReader(
165 totalProperty: 'total'
173 name: { first: 'Fat', last: 'Albert' }
174 // notice no city, state provided in data object
177 name: { first: 'Barney', last: 'Rubble' },
178 city: 'Bedrock', state: 'Stoneridge'
181 name: { first: 'Cliff', last: 'Claven' },
182 city: 'Boston', state: 'MA'
187 <div id="cfg-Ext.data.Field-dateFormat"></div>/**
188 * @cfg {String} dateFormat
189 * (Optional) A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
190 * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
191 * javascript millisecond timestamp.
194 <div id="cfg-Ext.data.Field-defaultValue"></div>/**
195 * @cfg {Mixed} defaultValue
196 * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
197 * when the item referenced by the <tt>{@link Ext.data.Field#mapping mapping}</tt> does not exist in the data
198 * object (i.e. undefined). (defaults to "")
201 <div id="cfg-Ext.data.Field-mapping"></div>/**
202 * @cfg {String/Number} mapping
203 * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
204 * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
205 * If the path expression is the same as the field name, the mapping may be omitted.</p>
206 * <p>The form of the mapping expression depends on the Reader being used.</p>
207 * <div class="mdetail-params"><ul>
208 * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
209 * 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>
210 * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
211 * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
212 * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
213 * of the field's value. Defaults to the field specification's Array position.</div></li>
215 * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
216 * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
217 * return the desired data.</p>
220 <div id="cfg-Ext.data.Field-sortType"></div>/**
221 * @cfg {Function} sortType
222 * (Optional) A function which converts a Field's value to a comparable value in order to ensure
223 * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
224 * sort example:<pre><code>
225 // current sort after sort we want
226 // +-+------+ +-+------+
227 // |1|First | |1|First |
228 // |2|Last | |3|Second|
229 // |3|Second| |2|Last |
230 // +-+------+ +-+------+
232 sortType: function(value) {
233 switch (value.toLowerCase()) // native toLowerCase():
235 case 'first': return 1;
236 case 'second': return 2;
243 <div id="cfg-Ext.data.Field-sortDir"></div>/**
244 * @cfg {String} sortDir
245 * (Optional) Initial direction to sort (<tt>"ASC"</tt> or <tt>"DESC"</tt>). Defaults to
249 <div id="cfg-Ext.data.Field-allowBlank"></div>/**
250 * @cfg {Boolean} allowBlank
251 * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <tt>true</tt>.
252 * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
253 * to evaluate to <tt>false</tt>.