Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / SortTypes.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.SortTypes'>/**
2 </span> * @class Ext.data.SortTypes
3  * This class defines a series of static methods that are used on a
4  * {@link Ext.data.Field} for performing sorting. The methods cast the 
5  * underlying values into a data type that is appropriate for sorting on
6  * that particular field.  If a {@link Ext.data.Field#type} is specified, 
7  * the sortType will be set to a sane default if the sortType is not 
8  * explicitly defined on the field. The sortType will make any necessary
9  * modifications to the value and return it.
10  * &lt;ul&gt;
11  * &lt;li&gt;&lt;b&gt;asText&lt;/b&gt; - Removes any tags and converts the value to a string&lt;/li&gt;
12  * &lt;li&gt;&lt;b&gt;asUCText&lt;/b&gt; - Removes any tags and converts the value to an uppercase string&lt;/li&gt;
13  * &lt;li&gt;&lt;b&gt;asUCText&lt;/b&gt; - Converts the value to an uppercase string&lt;/li&gt;
14  * &lt;li&gt;&lt;b&gt;asDate&lt;/b&gt; - Converts the value into Unix epoch time&lt;/li&gt;
15  * &lt;li&gt;&lt;b&gt;asFloat&lt;/b&gt; - Converts the value to a floating point number&lt;/li&gt;
16  * &lt;li&gt;&lt;b&gt;asInt&lt;/b&gt; - Converts the value to an integer number&lt;/li&gt;
17  * &lt;/ul&gt;
18  * &lt;p&gt;
19  * It is also possible to create a custom sortType that can be used throughout
20  * an application.
21  * &lt;pre&gt;&lt;code&gt;
22 Ext.apply(Ext.data.SortTypes, {
23     asPerson: function(person){
24         // expects an object with a first and last name property
25         return person.lastName.toUpperCase() + person.firstName.toLowerCase();
26     }    
27 });
28
29 Ext.define('Employee', {
30     extend: 'Ext.data.Model',
31     fields: [{
32         name: 'person',
33         sortType: 'asPerson'
34     }, {
35         name: 'salary',
36         type: 'float' // sortType set to asFloat
37     }]
38 });
39  * &lt;/code&gt;&lt;/pre&gt;
40  * &lt;/p&gt;
41  * @singleton
42  * @docauthor Evan Trimboli &lt;evan@sencha.com&gt;
43  */
44 Ext.define('Ext.data.SortTypes', {
45     
46     singleton: true,
47     
48 <span id='Ext-data.SortTypes-method-none'>    /**
49 </span>     * Default sort that does nothing
50      * @param {Mixed} s The value being converted
51      * @return {Mixed} The comparison value
52      */
53     none : function(s) {
54         return s;
55     },
56
57 <span id='Ext-data.SortTypes-property-stripTagsRE'>    /**
58 </span>     * The regular expression used to strip tags
59      * @type {RegExp}
60      * @property
61      */
62     stripTagsRE : /&lt;\/?[^&gt;]+&gt;/gi,
63
64 <span id='Ext-data.SortTypes-method-asText'>    /**
65 </span>     * Strips all HTML tags to sort on text only
66      * @param {Mixed} s The value being converted
67      * @return {String} The comparison value
68      */
69     asText : function(s) {
70         return String(s).replace(this.stripTagsRE, &quot;&quot;);
71     },
72
73 <span id='Ext-data.SortTypes-method-asUCText'>    /**
74 </span>     * Strips all HTML tags to sort on text only - Case insensitive
75      * @param {Mixed} s The value being converted
76      * @return {String} The comparison value
77      */
78     asUCText : function(s) {
79         return String(s).toUpperCase().replace(this.stripTagsRE, &quot;&quot;);
80     },
81
82 <span id='Ext-data.SortTypes-method-asUCString'>    /**
83 </span>     * Case insensitive string
84      * @param {Mixed} s The value being converted
85      * @return {String} The comparison value
86      */
87     asUCString : function(s) {
88         return String(s).toUpperCase();
89     },
90
91 <span id='Ext-data.SortTypes-method-asDate'>    /**
92 </span>     * Date sorting
93      * @param {Mixed} s The value being converted
94      * @return {Number} The comparison value
95      */
96     asDate : function(s) {
97         if(!s){
98             return 0;
99         }
100         if(Ext.isDate(s)){
101             return s.getTime();
102         }
103         return Date.parse(String(s));
104     },
105
106 <span id='Ext-data.SortTypes-method-asFloat'>    /**
107 </span>     * Float sorting
108      * @param {Mixed} s The value being converted
109      * @return {Float} The comparison value
110      */
111     asFloat : function(s) {
112         var val = parseFloat(String(s).replace(/,/g, &quot;&quot;));
113         return isNaN(val) ? 0 : val;
114     },
115
116 <span id='Ext-data.SortTypes-method-asInt'>    /**
117 </span>     * Integer sorting
118      * @param {Mixed} s The value being converted
119      * @return {Number} The comparison value
120      */
121     asInt : function(s) {
122         var val = parseInt(String(s).replace(/,/g, &quot;&quot;), 10);
123         return isNaN(val) ? 0 : val;
124     }
125 });</pre></pre></body></html>