Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / DataField.html
1 <html>
2 <head>
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>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
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>
20  */
21 Ext.data.Field = function(config){
22     if(typeof config == "string"){
23         config = {name: config};
24     }
25     Ext.apply(this, config);
26
27     if(!this.type){
28         this.type = "auto";
29     }
30
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];
35     }
36
37     // set default sortType for strings and dates
38     if(!this.sortType){
39         switch(this.type){
40             case "string":
41                 this.sortType = st.asUCString;
42                 break;
43             case "date":
44                 this.sortType = st.asDate;
45                 break;
46             default:
47                 this.sortType = st.none;
48         }
49     }
50
51     // define once
52     var stripRe = /[\$,%]/g;
53
54     // prebuilt conversion function for this field, instead of
55     // switching every time we're reading a value
56     if(!this.convert){
57         var cv, dateFormat = this.dateFormat;
58         switch(this.type){
59             case "":
60             case "auto":
61             case undefined:
62                 cv = function(v){ return v; };
63                 break;
64             case "string":
65                 cv = function(v){ return (v === undefined || v === null) ? '' : String(v); };
66                 break;
67             case "int":
68                 cv = function(v){
69                     return v !== undefined && v !== null && v !== '' ?
70                            parseInt(String(v).replace(stripRe, ""), 10) : '';
71                     };
72                 break;
73             case "float":
74                 cv = function(v){
75                     return v !== undefined && v !== null && v !== '' ?
76                            parseFloat(String(v).replace(stripRe, ""), 10) : '';
77                     };
78                 break;
79             case "bool":
80             case "boolean":
81                 cv = function(v){ return v === true || v === "true" || v == 1; };
82                 break;
83             case "date":
84                 cv = function(v){
85                     if(!v){
86                         return '';
87                     }
88                     if(Ext.isDate(v)){
89                         return v;
90                     }
91                     if(dateFormat){
92                         if(dateFormat == "timestamp"){
93                             return new Date(v*1000);
94                         }
95                         if(dateFormat == "time"){
96                             return new Date(parseInt(v, 10));
97                         }
98                         return Date.parseDate(v, dateFormat);
99                     }
100                     var parsed = Date.parse(v);
101                     return parsed ? new Date(parsed) : null;
102                 };
103              break;
104
105         }
106         this.convert = cv;
107     }
108 };
109
110 Ext.data.Field.prototype = {
111     <div id="cfg-Ext.data.Field-name"></div>/**
112      * @cfg {String} name
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>
117      */
118     <div id="cfg-Ext.data.Field-type"></div>/**
119      * @cfg {String} type
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>
124      * <li>string</li>
125      * <li>int</li>
126      * <li>float</li>
127      * <li>boolean</li>
128      * <li>date</li></ul></div>
129      */
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>
139      * </ul></div>
140      * <pre><code>
141 // example of convert function
142 function fullName(v, record){
143     return record.name.last + ', ' + record.name.first;
144 }
145
146 function location(v, record){
147     return !record.city ? '' : (record.city + ', ' + record.state);
148 }
149
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'},
155     'state',
156     {name: 'location',  convert: location}
157 ]);
158
159 // create the data store
160 var store = new Ext.data.Store({
161     reader: new Ext.data.JsonReader(
162         {
163             idProperty: 'key',
164             root: 'daRoot',  
165             totalProperty: 'total'
166         },
167         Dude  // recordType
168     )
169 });
170
171 var myData = [
172     { key: 1,
173       name: { first: 'Fat',    last:  'Albert' }
174       // notice no city, state provided in data object
175     },
176     { key: 2,
177       name: { first: 'Barney', last:  'Rubble' },
178       city: 'Bedrock', state: 'Stoneridge'
179     },
180     { key: 3,
181       name: { first: 'Cliff',  last:  'Claven' },
182       city: 'Boston',  state: 'MA'
183     }
184 ];
185      * </code></pre>
186      */
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.
192      */
193     dateFormat: null,
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 "")
199      */
200     defaultValue: "",
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>
214      * </ul></div>
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>
218      */
219     mapping: null,
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 // +-+------+          +-+------+
231
232 sortType: function(value) {
233    switch (value.toLowerCase()) // native toLowerCase():
234    {
235       case 'first': return 1;
236       case 'second': return 2;
237       default: return 3;
238    }
239 }
240      * </code></pre>
241      */
242     sortType : null,
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
246      * <tt>"ASC"</tt>.
247      */
248     sortDir : "ASC",
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>.
254          */
255         allowBlank : true
256 };</pre>
257 </body>
258 </html>