You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
8.8 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.data.Field
  17. * <p>This class encapsulates the field definition information specified in the field definition objects
  18. * passed to {@link Ext.data.Record#create}.</p>
  19. * <p>Developers do not need to instantiate this class. Instances are created by {@link Ext.data.Record.create}
  20. * and cached in the {@link Ext.data.Record#fields fields} property of the created Record constructor's <b>prototype.</b></p>
  21. */
  22. Ext.data.Field = Ext.extend(Object, {
  23. constructor : function(config){
  24. if(Ext.isString(config)){
  25. config = {name: config};
  26. }
  27. Ext.apply(this, config);
  28. var types = Ext.data.Types,
  29. st = this.sortType,
  30. t;
  31. if(this.type){
  32. if(Ext.isString(this.type)){
  33. this.type = Ext.data.Types[this.type.toUpperCase()] || types.AUTO;
  34. }
  35. }else{
  36. this.type = types.AUTO;
  37. }
  38. // named sortTypes are supported, here we look them up
  39. if(Ext.isString(st)){
  40. this.sortType = Ext.data.SortTypes[st];
  41. }else if(Ext.isEmpty(st)){
  42. this.sortType = this.type.sortType;
  43. }
  44. if(!this.convert){
  45. this.convert = this.type.convert;
  46. }
  47. },
  48. /**
  49. * @cfg {String} name
  50. * The name by which the field is referenced within the Record. This is referenced by, for example,
  51. * the <code>dataIndex</code> property in column definition objects passed to {@link Ext.grid.ColumnModel}.
  52. * <p>Note: In the simplest case, if no properties other than <code>name</code> are required, a field
  53. * definition may consist of just a String for the field name.</p>
  54. */
  55. /**
  56. * @cfg {Mixed} type
  57. * (Optional) The data type for automatic conversion from received data to the <i>stored</i> value if <code>{@link Ext.data.Field#convert convert}</code>
  58. * has not been specified. This may be specified as a string value. Possible values are
  59. * <div class="mdetail-params"><ul>
  60. * <li>auto (Default, implies no conversion)</li>
  61. * <li>string</li>
  62. * <li>int</li>
  63. * <li>float</li>
  64. * <li>boolean</li>
  65. * <li>date</li></ul></div>
  66. * <p>This may also be specified by referencing a member of the {@link Ext.data.Types} class.</p>
  67. * <p>Developers may create their own application-specific data types by defining new members of the
  68. * {@link Ext.data.Types} class.</p>
  69. */
  70. /**
  71. * @cfg {Function} convert
  72. * (Optional) A function which converts the value provided by the Reader into an object that will be stored
  73. * in the Record. It is passed the following parameters:<div class="mdetail-params"><ul>
  74. * <li><b>v</b> : Mixed<div class="sub-desc">The data value as read by the Reader, if undefined will use
  75. * the configured <code>{@link Ext.data.Field#defaultValue defaultValue}</code>.</div></li>
  76. * <li><b>rec</b> : Mixed<div class="sub-desc">The data object containing the row as read by the Reader.
  77. * Depending on the Reader type, this could be an Array ({@link Ext.data.ArrayReader ArrayReader}), an object
  78. * ({@link Ext.data.JsonReader JsonReader}), or an XML element ({@link Ext.data.XMLReader XMLReader}).</div></li>
  79. * </ul></div>
  80. * <pre><code>
  81. // example of convert function
  82. function fullName(v, record){
  83. return record.name.last + ', ' + record.name.first;
  84. }
  85. function location(v, record){
  86. return !record.city ? '' : (record.city + ', ' + record.state);
  87. }
  88. var Dude = Ext.data.Record.create([
  89. {name: 'fullname', convert: fullName},
  90. {name: 'firstname', mapping: 'name.first'},
  91. {name: 'lastname', mapping: 'name.last'},
  92. {name: 'city', defaultValue: 'homeless'},
  93. 'state',
  94. {name: 'location', convert: location}
  95. ]);
  96. // create the data store
  97. var store = new Ext.data.Store({
  98. reader: new Ext.data.JsonReader(
  99. {
  100. idProperty: 'key',
  101. root: 'daRoot',
  102. totalProperty: 'total'
  103. },
  104. Dude // recordType
  105. )
  106. });
  107. var myData = [
  108. { key: 1,
  109. name: { first: 'Fat', last: 'Albert' }
  110. // notice no city, state provided in data object
  111. },
  112. { key: 2,
  113. name: { first: 'Barney', last: 'Rubble' },
  114. city: 'Bedrock', state: 'Stoneridge'
  115. },
  116. { key: 3,
  117. name: { first: 'Cliff', last: 'Claven' },
  118. city: 'Boston', state: 'MA'
  119. }
  120. ];
  121. * </code></pre>
  122. */
  123. /**
  124. * @cfg {String} dateFormat
  125. * <p>(Optional) Used when converting received data into a Date when the {@link #type} is specified as <code>"date"</code>.</p>
  126. * <p>A format string for the {@link Date#parseDate Date.parseDate} function, or "timestamp" if the
  127. * value provided by the Reader is a UNIX timestamp, or "time" if the value provided by the Reader is a
  128. * javascript millisecond timestamp. See {@link Date}</p>
  129. */
  130. dateFormat: null,
  131. /**
  132. * @cfg {Boolean} useNull
  133. * <p>(Optional) Use when converting received data into a Number type (either int or float). If the value cannot be parsed,
  134. * null will be used if useNull is true, otherwise the value will be 0. Defaults to <tt>false</tt>
  135. */
  136. useNull: false,
  137. /**
  138. * @cfg {Mixed} defaultValue
  139. * (Optional) The default value used <b>when a Record is being created by a {@link Ext.data.Reader Reader}</b>
  140. * when the item referenced by the <code>{@link Ext.data.Field#mapping mapping}</code> does not exist in the data
  141. * object (i.e. undefined). (defaults to "")
  142. */
  143. defaultValue: "",
  144. /**
  145. * @cfg {String/Number} mapping
  146. * <p>(Optional) A path expression for use by the {@link Ext.data.DataReader} implementation
  147. * that is creating the {@link Ext.data.Record Record} to extract the Field value from the data object.
  148. * If the path expression is the same as the field name, the mapping may be omitted.</p>
  149. * <p>The form of the mapping expression depends on the Reader being used.</p>
  150. * <div class="mdetail-params"><ul>
  151. * <li>{@link Ext.data.JsonReader}<div class="sub-desc">The mapping is a string containing the javascript
  152. * 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>
  153. * <li>{@link Ext.data.XmlReader}<div class="sub-desc">The mapping is an {@link Ext.DomQuery} path to the data
  154. * item relative to the DOM element that represents the {@link Ext.data.XmlReader#record record}. Defaults to the field name.</div></li>
  155. * <li>{@link Ext.data.ArrayReader}<div class="sub-desc">The mapping is a number indicating the Array index
  156. * of the field's value. Defaults to the field specification's Array position.</div></li>
  157. * </ul></div>
  158. * <p>If a more complex value extraction strategy is required, then configure the Field with a {@link #convert}
  159. * function. This is passed the whole row object, and may interrogate it in whatever way is necessary in order to
  160. * return the desired data.</p>
  161. */
  162. mapping: null,
  163. /**
  164. * @cfg {Function} sortType
  165. * (Optional) A function which converts a Field's value to a comparable value in order to ensure
  166. * correct sort ordering. Predefined functions are provided in {@link Ext.data.SortTypes}. A custom
  167. * sort example:<pre><code>
  168. // current sort after sort we want
  169. // +-+------+ +-+------+
  170. // |1|First | |1|First |
  171. // |2|Last | |3|Second|
  172. // |3|Second| |2|Last |
  173. // +-+------+ +-+------+
  174. sortType: function(value) {
  175. switch (value.toLowerCase()) // native toLowerCase():
  176. {
  177. case 'first': return 1;
  178. case 'second': return 2;
  179. default: return 3;
  180. }
  181. }
  182. * </code></pre>
  183. */
  184. sortType : null,
  185. /**
  186. * @cfg {String} sortDir
  187. * (Optional) Initial direction to sort (<code>"ASC"</code> or <code>"DESC"</code>). Defaults to
  188. * <code>"ASC"</code>.
  189. */
  190. sortDir : "ASC",
  191. /**
  192. * @cfg {Boolean} allowBlank
  193. * (Optional) Used for validating a {@link Ext.data.Record record}, defaults to <code>true</code>.
  194. * An empty value here will cause {@link Ext.data.Record}.{@link Ext.data.Record#isValid isValid}
  195. * to evaluate to <code>false</code>.
  196. */
  197. allowBlank : true
  198. });