Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / grid / column / Template.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.grid.column.Template
17  * @extends Ext.grid.column.Column
18  * 
19  * A Column definition class which renders a value by processing a {@link Ext.data.Model Model}'s
20  * {@link Ext.data.Model#data data} using a {@link #tpl configured} {@link Ext.XTemplate XTemplate}.
21  * 
22  *  {@img Ext.grid.column.Template/Ext.grid.column.Template.png Ext.grid.column.Template grid column}
23  * 
24  * ## Code
25  *     Ext.create('Ext.data.Store', {
26  *         storeId:'employeeStore',
27  *         fields:['firstname', 'lastname', 'senority', 'department'],
28  *         groupField: 'department',
29  *         data:[
30  *             {firstname:"Michael", lastname:"Scott", senority:7, department:"Manangement"},
31  *             {firstname:"Dwight", lastname:"Schrute", senority:2, department:"Sales"},
32  *             {firstname:"Jim", lastname:"Halpert", senority:3, department:"Sales"},
33  *             {firstname:"Kevin", lastname:"Malone", senority:4, department:"Accounting"},
34  *             {firstname:"Angela", lastname:"Martin", senority:5, department:"Accounting"}                        
35  *         ]
36  *     });
37  *     
38  *     Ext.create('Ext.grid.Panel', {
39  *         title: 'Column Template Demo',
40  *         store: Ext.data.StoreManager.lookup('employeeStore'),
41  *         columns: [
42  *             {text: 'Full Name',  xtype:'templatecolumn', tpl:'{firstname} {lastname}', flex:1},
43  *             {text: 'Deparment (Yrs)', xtype:'templatecolumn', tpl:'{department} ({senority})'}
44  *         ],
45  *         height: 200,
46  *         width: 300,
47  *         renderTo: Ext.getBody()
48  *     });
49  * 
50  * @markdown
51  */
52 Ext.define('Ext.grid.column.Template', {
53     extend: 'Ext.grid.column.Column',
54     alias: ['widget.templatecolumn'],
55     requires: ['Ext.XTemplate'],
56     alternateClassName: 'Ext.grid.TemplateColumn',
57
58     /**
59      * @cfg {String/XTemplate} tpl
60      * An {@link Ext.XTemplate XTemplate}, or an XTemplate <i>definition string</i> to use to process a
61      * {@link Ext.data.Model Model}'s {@link Ext.data.Model#data data} to produce a column's rendered value.
62      */
63     constructor: function(cfg){
64         var me = this,
65             tpl;
66             
67         me.callParent(arguments);
68         tpl = me.tpl = (!Ext.isPrimitive(me.tpl) && me.tpl.compile) ? me.tpl : Ext.create('Ext.XTemplate', me.tpl);
69
70         me.renderer = function(value, p, record) {
71             var data = Ext.apply({}, record.data, record.getAssociatedData());
72             return tpl.apply(data);
73         };
74     }
75 });
76