Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Action3.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5   <title>The source code</title>
6   <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7   <script type="text/javascript" src="../prettify/prettify.js"></script>
8   <style type="text/css">
9     .highlight { display: block; background-color: #ddd; }
10   </style>
11   <script type="text/javascript">
12     function highlight() {
13       document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
14     }
15   </script>
16 </head>
17 <body onload="prettyPrint(); highlight();">
18   <pre class="prettyprint lang-js"><span id='Ext-grid-column-Action'>/**
19 </span> * @class Ext.grid.column.Action
20  * @extends Ext.grid.column.Column
21  * &lt;p&gt;A Grid header type which renders an icon, or a series of icons in a grid cell, and offers a scoped click
22  * handler for each icon.&lt;/p&gt;
23  *
24  * {@img Ext.grid.column.Action/Ext.grid.column.Action.png Ext.grid.column.Action grid column}
25  *  
26  * ## Code
27  *     Ext.create('Ext.data.Store', {
28  *         storeId:'employeeStore',
29  *         fields:['firstname', 'lastname', 'senority', 'dep', 'hired'],
30  *         data:[
31  *             {firstname:&quot;Michael&quot;, lastname:&quot;Scott&quot;},
32  *             {firstname:&quot;Dwight&quot;, lastname:&quot;Schrute&quot;},
33  *             {firstname:&quot;Jim&quot;, lastname:&quot;Halpert&quot;},
34  *             {firstname:&quot;Kevin&quot;, lastname:&quot;Malone&quot;},
35  *             {firstname:&quot;Angela&quot;, lastname:&quot;Martin&quot;}                        
36  *         ]
37  *     });
38  *     
39  *     Ext.create('Ext.grid.Panel', {
40  *         title: 'Action Column Demo',
41  *         store: Ext.data.StoreManager.lookup('employeeStore'),
42  *         columns: [
43  *             {text: 'First Name',  dataIndex:'firstname'},
44  *             {text: 'Last Name',  dataIndex:'lastname'},
45  *             {
46  *                 xtype:'actioncolumn', 
47  *                 width:50,
48  *                 items: [{
49  *                     icon: 'images/edit.png',  // Use a URL in the icon config
50  *                     tooltip: 'Edit',
51  *                     handler: function(grid, rowIndex, colIndex) {
52  *                         var rec = grid.getStore().getAt(rowIndex);
53  *                         alert(&quot;Edit &quot; + rec.get('firstname'));
54  *                     }
55  *                 },{
56  *                     icon: 'images/delete.png',
57  *                     tooltip: 'Delete',
58  *                     handler: function(grid, rowIndex, colIndex) {
59  *                         var rec = grid.getStore().getAt(rowIndex);
60  *                         alert(&quot;Terminate &quot; + rec.get('firstname'));
61  *                     }                
62  *                 }]
63  *             }
64  *         ],
65  *         width: 250,
66  *         renderTo: Ext.getBody()
67  *     });
68  * &lt;p&gt;The action column can be at any index in the columns array, and a grid can have any number of
69  * action columns. &lt;/p&gt;
70  * @xtype actioncolumn
71  */
72 Ext.define('Ext.grid.column.Action', {
73     extend: 'Ext.grid.column.Column',
74     alias: ['widget.actioncolumn'],
75     alternateClassName: 'Ext.grid.ActionColumn',
76
77 <span id='Ext-grid-column-Action-cfg-icon'>    /**
78 </span>     * @cfg {String} icon
79      * The URL of an image to display as the clickable element in the column. 
80      * Optional - defaults to &lt;code&gt;{@link Ext#BLANK_IMAGE_URL Ext.BLANK_IMAGE_URL}&lt;/code&gt;.
81      */
82 <span id='Ext-grid-column-Action-cfg-iconCls'>    /**
83 </span>     * @cfg {String} iconCls
84      * A CSS class to apply to the icon image. To determine the class dynamically, configure the Column with a &lt;code&gt;{@link #getClass}&lt;/code&gt; function.
85      */
86 <span id='Ext-grid-column-Action-cfg-handler'>    /**
87 </span>     * @cfg {Function} handler A function called when the icon is clicked.
88      * The handler is passed the following parameters:&lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
89      * &lt;li&gt;&lt;code&gt;view&lt;/code&gt; : TableView&lt;div class=&quot;sub-desc&quot;&gt;The owning TableView.&lt;/div&gt;&lt;/li&gt;
90      * &lt;li&gt;&lt;code&gt;rowIndex&lt;/code&gt; : Number&lt;div class=&quot;sub-desc&quot;&gt;The row index clicked on.&lt;/div&gt;&lt;/li&gt;
91      * &lt;li&gt;&lt;code&gt;colIndex&lt;/code&gt; : Number&lt;div class=&quot;sub-desc&quot;&gt;The column index clicked on.&lt;/div&gt;&lt;/li&gt;
92      * &lt;li&gt;&lt;code&gt;item&lt;/code&gt; : Object&lt;div class=&quot;sub-desc&quot;&gt;The clicked item (or this Column if multiple 
93      * {@link #items} were not configured).&lt;/div&gt;&lt;/li&gt;
94      * &lt;li&gt;&lt;code&gt;e&lt;/code&gt; : Event&lt;div class=&quot;sub-desc&quot;&gt;The click event.&lt;/div&gt;&lt;/li&gt;
95      * &lt;/ul&gt;&lt;/div&gt;
96      */
97 <span id='Ext-grid-column-Action-cfg-scope'>    /**
98 </span>     * @cfg {Object} scope The scope (&lt;tt&gt;&lt;b&gt;this&lt;/b&gt;&lt;/tt&gt; reference) in which the &lt;code&gt;{@link #handler}&lt;/code&gt;
99      * and &lt;code&gt;{@link #getClass}&lt;/code&gt; fuctions are executed. Defaults to this Column.
100      */
101 <span id='Ext-grid-column-Action-cfg-tooltip'>    /**
102 </span>     * @cfg {String} tooltip A tooltip message to be displayed on hover. {@link Ext.tip.QuickTipManager#init Ext.tip.QuickTipManager} must have 
103      * been initialized.
104      */
105 <span id='Ext-grid-column-Action-cfg-stopSelection'>    /**
106 </span>     * @cfg {Boolean} stopSelection Defaults to &lt;code&gt;true&lt;/code&gt;. Prevent grid &lt;i&gt;row&lt;/i&gt; selection upon mousedown.
107      */
108 <span id='Ext-grid-column-Action-cfg-getClass'>    /**
109 </span>     * @cfg {Function} getClass A function which returns the CSS class to apply to the icon image.
110      * The function is passed the following parameters:&lt;ul&gt;
111      *     &lt;li&gt;&lt;b&gt;v&lt;/b&gt; : Object&lt;p class=&quot;sub-desc&quot;&gt;The value of the column's configured field (if any).&lt;/p&gt;&lt;/li&gt;
112      *     &lt;li&gt;&lt;b&gt;metadata&lt;/b&gt; : Object&lt;p class=&quot;sub-desc&quot;&gt;An object in which you may set the following attributes:&lt;ul&gt;
113      *         &lt;li&gt;&lt;b&gt;css&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;A CSS class name to add to the cell's TD element.&lt;/p&gt;&lt;/li&gt;
114      *         &lt;li&gt;&lt;b&gt;attr&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;An HTML attribute definition string to apply to the data container element &lt;i&gt;within&lt;/i&gt; the table cell
115      *         (e.g. 'style=&quot;color:red;&quot;').&lt;/p&gt;&lt;/li&gt;
116      *     &lt;/ul&gt;&lt;/p&gt;&lt;/li&gt;
117      *     &lt;li&gt;&lt;b&gt;r&lt;/b&gt; : Ext.data.Record&lt;p class=&quot;sub-desc&quot;&gt;The Record providing the data.&lt;/p&gt;&lt;/li&gt;
118      *     &lt;li&gt;&lt;b&gt;rowIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The row index..&lt;/p&gt;&lt;/li&gt;
119      *     &lt;li&gt;&lt;b&gt;colIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The column index.&lt;/p&gt;&lt;/li&gt;
120      *     &lt;li&gt;&lt;b&gt;store&lt;/b&gt; : Ext.data.Store&lt;p class=&quot;sub-desc&quot;&gt;The Store which is providing the data Model.&lt;/p&gt;&lt;/li&gt;
121      * &lt;/ul&gt;
122      */
123 <span id='Ext-grid-column-Action-cfg-items'>    /**
124 </span>     * @cfg {Array} items An Array which may contain multiple icon definitions, each element of which may contain:
125      * &lt;div class=&quot;mdetail-params&quot;&gt;&lt;ul&gt;
126      * &lt;li&gt;&lt;code&gt;icon&lt;/code&gt; : String&lt;div class=&quot;sub-desc&quot;&gt;The url of an image to display as the clickable element 
127      * in the column.&lt;/div&gt;&lt;/li&gt;
128      * &lt;li&gt;&lt;code&gt;iconCls&lt;/code&gt; : String&lt;div class=&quot;sub-desc&quot;&gt;A CSS class to apply to the icon image.
129      * To determine the class dynamically, configure the item with a &lt;code&gt;getClass&lt;/code&gt; function.&lt;/div&gt;&lt;/li&gt;
130      * &lt;li&gt;&lt;code&gt;getClass&lt;/code&gt; : Function&lt;div class=&quot;sub-desc&quot;&gt;A function which returns the CSS class to apply to the icon image.
131      * The function is passed the following parameters:&lt;ul&gt;
132      *     &lt;li&gt;&lt;b&gt;v&lt;/b&gt; : Object&lt;p class=&quot;sub-desc&quot;&gt;The value of the column's configured field (if any).&lt;/p&gt;&lt;/li&gt;
133      *     &lt;li&gt;&lt;b&gt;metadata&lt;/b&gt; : Object&lt;p class=&quot;sub-desc&quot;&gt;An object in which you may set the following attributes:&lt;ul&gt;
134      *         &lt;li&gt;&lt;b&gt;css&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;A CSS class name to add to the cell's TD element.&lt;/p&gt;&lt;/li&gt;
135      *         &lt;li&gt;&lt;b&gt;attr&lt;/b&gt; : String&lt;p class=&quot;sub-desc&quot;&gt;An HTML attribute definition string to apply to the data container element &lt;i&gt;within&lt;/i&gt; the table cell
136      *         (e.g. 'style=&quot;color:red;&quot;').&lt;/p&gt;&lt;/li&gt;
137      *     &lt;/ul&gt;&lt;/p&gt;&lt;/li&gt;
138      *     &lt;li&gt;&lt;b&gt;r&lt;/b&gt; : Ext.data.Record&lt;p class=&quot;sub-desc&quot;&gt;The Record providing the data.&lt;/p&gt;&lt;/li&gt;
139      *     &lt;li&gt;&lt;b&gt;rowIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The row index..&lt;/p&gt;&lt;/li&gt;
140      *     &lt;li&gt;&lt;b&gt;colIndex&lt;/b&gt; : Number&lt;p class=&quot;sub-desc&quot;&gt;The column index.&lt;/p&gt;&lt;/li&gt;
141      *     &lt;li&gt;&lt;b&gt;store&lt;/b&gt; : Ext.data.Store&lt;p class=&quot;sub-desc&quot;&gt;The Store which is providing the data Model.&lt;/p&gt;&lt;/li&gt;
142      * &lt;/ul&gt;&lt;/div&gt;&lt;/li&gt;
143      * &lt;li&gt;&lt;code&gt;handler&lt;/code&gt; : Function&lt;div class=&quot;sub-desc&quot;&gt;A function called when the icon is clicked.&lt;/div&gt;&lt;/li&gt;
144      * &lt;li&gt;&lt;code&gt;scope&lt;/code&gt; : Scope&lt;div class=&quot;sub-desc&quot;&gt;The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the 
145      * &lt;code&gt;handler&lt;/code&gt; and &lt;code&gt;getClass&lt;/code&gt; functions are executed. Fallback defaults are this Column's
146      * configured scope, then this Column.&lt;/div&gt;&lt;/li&gt;
147      * &lt;li&gt;&lt;code&gt;tooltip&lt;/code&gt; : String&lt;div class=&quot;sub-desc&quot;&gt;A tooltip message to be displayed on hover. 
148      * {@link Ext.tip.QuickTipManager#init Ext.tip.QuickTipManager} must have been initialized.&lt;/div&gt;&lt;/li&gt;
149      * &lt;/ul&gt;&lt;/div&gt;
150      */
151     header: '&amp;#160;',
152
153     actionIdRe: /x-action-col-(\d+)/,
154
155 <span id='Ext-grid-column-Action-cfg-altText'>    /**
156 </span>     * @cfg {String} altText The alt text to use for the image element. Defaults to &lt;tt&gt;''&lt;/tt&gt;.
157      */
158     altText: '',
159     
160     sortable: false,
161
162     constructor: function(config) {
163         var me = this,
164             cfg = Ext.apply({}, config),
165             items = cfg.items || [me],
166             l = items.length,
167             i,
168             item;
169
170         // This is a Container. Delete the items config to be reinstated after construction.
171         delete cfg.items;
172         me.callParent([cfg]);
173
174         // Items is an array property of ActionColumns
175         me.items = items;
176
177 //      Renderer closure iterates through items creating an &lt;img&gt; element for each and tagging with an identifying 
178 //      class name x-action-col-{n}
179         me.renderer = function(v, meta) {
180 //          Allow a configured renderer to create initial value (And set the other values in the &quot;metadata&quot; argument!)
181             v = Ext.isFunction(cfg.renderer) ? cfg.renderer.apply(this, arguments)||'' : '';
182
183             meta.tdCls += ' ' + Ext.baseCSSPrefix + 'action-col-cell';
184             for (i = 0; i &lt; l; i++) {
185                 item = items[i];
186                 v += '&lt;img alt=&quot;' + me.altText + '&quot; src=&quot;' + (item.icon || Ext.BLANK_IMAGE_URL) +
187                     '&quot; class=&quot;' + Ext.baseCSSPrefix + 'action-col-icon ' + Ext.baseCSSPrefix + 'action-col-' + String(i) + ' ' +  (item.iconCls || '') + 
188                     ' ' + (Ext.isFunction(item.getClass) ? item.getClass.apply(item.scope||me.scope||me, arguments) : (me.iconCls || '')) + '&quot;' +
189                     ((item.tooltip) ? ' data-qtip=&quot;' + item.tooltip + '&quot;' : '') + ' /&gt;';
190             }
191             return v;
192         };
193     },
194
195     destroy: function() {
196         delete this.items;
197         delete this.renderer;
198         return this.callParent(arguments);
199     },
200
201 <span id='Ext-grid-column-Action-method-processEvent'>    /**
202 </span>     * @private
203      * Process and refire events routed from the GridView's processEvent method.
204      * Also fires any configured click handlers. By default, cancels the mousedown event to prevent selection.
205      * Returns the event handler's status to allow canceling of GridView's bubbling process.
206      */
207     processEvent : function(type, view, cell, recordIndex, cellIndex, e){
208         var me = this,
209             match = e.getTarget().className.match(me.actionIdRe),
210             item, fn;
211         if (match) {
212             item = me.items[parseInt(match[1], 10)];
213             if (item) {
214                 if (type == 'click') {
215                     fn = item.handler || me.handler;
216                     if (fn) {
217                         fn.call(item.scope || me.scope || me, view, recordIndex, cellIndex, item, e);
218                     }
219                 } else if (type == 'mousedown' &amp;&amp; item.stopSelection !== false) {
220                     return false;
221                 }
222             }
223         }
224         return me.callParent(arguments);
225     },
226
227     cascade: function(fn, scope) {
228         fn.call(scope||this, this);
229     },
230
231     // Private override because this cannot function as a Container, and it has an items property which is an Array, NOT a MixedCollection.
232     getRefItems: function() {
233         return [];
234     }
235 });</pre>
236 </body>
237 </html>