Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / docs / source / Label.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-form-Label'>/**
19 </span> * @class Ext.form.Label
20  * @extends Ext.Component
21
22 Produces a standalone `&lt;label /&gt;` element which can be inserted into a form and be associated with a field
23 in that form using the {@link #forId} property.
24
25 **NOTE:** in most cases it will be more appropriate to use the {@link Ext.form.Labelable#fieldLabel fieldLabel}
26 and associated config properties ({@link Ext.form.Labelable#labelAlign}, {@link Ext.form.Labelable#labelWidth},
27 etc.) in field components themselves, as that allows labels to be uniformly sized throughout the form.
28 Ext.form.Label should only be used when your layout can not be achieved with the standard
29 {@link Ext.form.Labelable field layout}.
30
31 You will likely be associating the label with a field component that extends {@link Ext.form.field.Base}, so
32 you should make sure the {@link #forId} is set to the same value as the {@link Ext.form.field.Base#inputId inputId}
33 of that field.
34
35 The label's text can be set using either the {@link #text} or {@link #html} configuration properties; the
36 difference between the two is that the former will automatically escape HTML characters when rendering, while
37 the latter will not.
38 {@img Ext.form.Label/Ext.form.Label.png Ext.form.Label component}
39 #Example usage:#
40
41 This example creates a Label after its associated Text field, an arrangement that cannot currently
42 be achieved using the standard Field layout's labelAlign.
43
44     Ext.create('Ext.form.Panel', {
45         title: 'Field with Label',
46         width: 400,
47         bodyPadding: 10,
48         renderTo: Ext.getBody(),
49         layout: {
50             type: 'hbox',
51             align: 'middle'
52         },
53         items: [{
54             xtype: 'textfield',
55             hideLabel: true,
56             flex: 1
57         }, {
58             xtype: 'label',
59             forId: 'myFieldId',
60             text: 'My Awesome Field',
61             margins: '0 0 0 10'
62         }]
63     });
64
65  * @markdown
66  * @docauthor Jason Johnston &lt;jason@sencha.com&gt;
67  */
68 Ext.define('Ext.form.Label', {
69     extend:'Ext.Component',
70     alias: 'widget.label',
71     requires: ['Ext.util.Format'],
72
73 <span id='Ext-form-Label-cfg-text'>    /**
74 </span>     * @cfg {String} text The plain text to display within the label (defaults to ''). If you need to include HTML
75      * tags within the label's innerHTML, use the {@link #html} config instead.
76      */
77 <span id='Ext-form-Label-cfg-forId'>    /**
78 </span>     * @cfg {String} forId The id of the input element to which this label will be bound via the standard HTML 'for'
79      * attribute. If not specified, the attribute will not be added to the label. In most cases you will be
80      * associating the label with a {@link Ext.form.field.Base} component, so you should make sure this matches
81      * the {@link Ext.form.field.Base#inputId inputId} of that field.
82      */
83 <span id='Ext-form-Label-cfg-html'>    /**
84 </span>     * @cfg {String} html An HTML fragment that will be used as the label's innerHTML (defaults to '').
85      * Note that if {@link #text} is specified it will take precedence and this value will be ignored.
86      */
87     
88     maskOnDisable: false,
89     getElConfig: function(){
90         var me = this;
91         return {
92             tag: 'label', 
93             id: me.id, 
94             htmlFor: me.forId || '',
95             html: me.text ? Ext.util.Format.htmlEncode(me.text) : (me.html || '') 
96         };
97     },
98
99 <span id='Ext-form-Label-method-setText'>    /**
100 </span>     * Updates the label's innerHTML with the specified string.
101      * @param {String} text The new label text
102      * @param {Boolean} encode (optional) False to skip HTML-encoding the text when rendering it
103      * to the label (defaults to true which encodes the value). This might be useful if you want to include
104      * tags in the label's innerHTML rather than rendering them as string literals per the default logic.
105      * @return {Label} this
106      */
107     setText : function(text, encode){
108         var me = this;
109         
110         encode = encode !== false;
111         if(encode) {
112             me.text = text;
113             delete me.html;
114         } else {
115             me.html = text;
116             delete me.text;
117         }
118         
119         if(me.rendered){
120             me.el.dom.innerHTML = encode !== false ? Ext.util.Format.htmlEncode(text) : text;
121         }
122         return this;
123     }
124 });
125
126 </pre>
127 </body>
128 </html>