Upgrade to ExtJS 4.0.7 - Released 10/19/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  * A Column definition class which renders a value by processing a {@link Ext.data.Model Model}'s
17  * {@link Ext.data.Model#persistenceProperty data} using a {@link #tpl configured}
18  * {@link Ext.XTemplate XTemplate}.
19  * 
20  *     @example
21  *     Ext.create('Ext.data.Store', {
22  *         storeId:'employeeStore',
23  *         fields:['firstname', 'lastname', 'senority', 'department'],
24  *         groupField: 'department',
25  *         data:[
26  *             { firstname: "Michael", lastname: "Scott",   senority: 7, department: "Manangement" },
27  *             { firstname: "Dwight",  lastname: "Schrute", senority: 2, department: "Sales" },
28  *             { firstname: "Jim",     lastname: "Halpert", senority: 3, department: "Sales" },
29  *             { firstname: "Kevin",   lastname: "Malone",  senority: 4, department: "Accounting" },
30  *             { firstname: "Angela",  lastname: "Martin",  senority: 5, department: "Accounting" }                        
31  *         ]
32  *     });
33  *     
34  *     Ext.create('Ext.grid.Panel', {
35  *         title: 'Column Template Demo',
36  *         store: Ext.data.StoreManager.lookup('employeeStore'),
37  *         columns: [
38  *             { text: 'Full Name',       xtype: 'templatecolumn', tpl: '{firstname} {lastname}', flex:1 },
39  *             { text: 'Deparment (Yrs)', xtype: 'templatecolumn', tpl: '{department} ({senority})' }
40  *         ],
41  *         height: 200,
42  *         width: 300,
43  *         renderTo: Ext.getBody()
44  *     });
45  */
46 Ext.define('Ext.grid.column.Template', {
47     extend: 'Ext.grid.column.Column',
48     alias: ['widget.templatecolumn'],
49     requires: ['Ext.XTemplate'],
50     alternateClassName: 'Ext.grid.TemplateColumn',
51
52     /**
53      * @cfg {String/Ext.XTemplate} tpl
54      * An {@link Ext.XTemplate XTemplate}, or an XTemplate *definition string* to use to process a
55      * {@link Ext.data.Model Model}'s {@link Ext.data.Model#persistenceProperty data} to produce a
56      * column's rendered value.
57      */
58
59     constructor: function(cfg){
60         var me = this,
61             tpl;
62             
63         me.callParent(arguments);
64         tpl = me.tpl = (!Ext.isPrimitive(me.tpl) && me.tpl.compile) ? me.tpl : Ext.create('Ext.XTemplate', me.tpl);
65
66         me.renderer = function(value, p, record) {
67             var data = Ext.apply({}, record.data, record.getAssociatedData());
68             return tpl.apply(data);
69         };
70     }
71 });
72