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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-form-field-Checkbox-method-constructor'><span id='Ext-form-field-Checkbox'>/**
19 </span></span> * @class Ext.form.field.Checkbox
20 * @extends Ext.form.field.Base
22 Single checkbox field. Can be used as a direct replacement for traditional checkbox fields. Also serves as a
23 parent class for {@link Ext.form.field.Radio radio buttons}.
25 __Labeling:__ In addition to the {@link Ext.form.Labelable standard field labeling options}, checkboxes
26 may be given an optional {@link #boxLabel} which will be displayed immediately after checkbox. Also see
27 {@link Ext.form.CheckboxGroup} for a convenient method of grouping related checkboxes.
30 The main value of a checkbox is a boolean, indicating whether or not the checkbox is checked.
31 The following values will check the checkbox:
37 Any other value will uncheck the checkbox.
39 In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be
40 sent as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set
41 this value if you have multiple checkboxes with the same {@link #name}. If not specified, the value `on`
43 {@img Ext.form.Checkbox/Ext.form.Checkbox.png Ext.form.Checkbox Checkbox component}
46 Ext.create('Ext.form.Panel', {
49 title : 'Pizza Order',
52 xtype : 'fieldcontainer',
53 fieldLabel : 'Toppings',
54 defaultType: 'checkboxfield',
57 boxLabel : 'Anchovies',
62 boxLabel : 'Artichoke Hearts',
80 var checkbox = Ext.getCmp('checkbox3');
81 checkbox.setValue(true);
88 var checkbox1 = Ext.getCmp('checkbox1'),
89 checkbox2 = Ext.getCmp('checkbox2'),
90 checkbox3 = Ext.getCmp('checkbox3');
92 checkbox1.setValue(true);
93 checkbox2.setValue(true);
94 checkbox3.setValue(true);
100 var checkbox1 = Ext.getCmp('checkbox1'),
101 checkbox2 = Ext.getCmp('checkbox2'),
102 checkbox3 = Ext.getCmp('checkbox3');
104 checkbox1.setValue(false);
105 checkbox2.setValue(false);
106 checkbox3.setValue(false);
110 renderTo: Ext.getBody()
114 * Creates a new Checkbox
115 * @param {Object} config Configuration options
116 * @xtype checkboxfield
117 * @docauthor Robert Dougan <rob@sencha.com>
120 Ext.define('Ext.form.field.Checkbox', {
121 extend: 'Ext.form.field.Base',
122 alias: ['widget.checkboxfield', 'widget.checkbox'],
123 alternateClassName: 'Ext.form.Checkbox',
124 requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager'],
127 '<tpl if="boxLabel && boxLabelAlign == \'before\'">',
128 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
130 // Creates not an actual checkbox, but a button which is given aria role="checkbox" and
131 // styled with a custom checkbox image. This allows greater control and consistency in
132 // styling, and using a button allows it to gain focus and handle keyboard nav properly.
133 '<input type="button" id="{id}" ',
134 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
135 'class="{fieldCls} {typeCls}" autocomplete="off" hidefocus="true" />',
136 '<tpl if="boxLabel && boxLabelAlign == \'after\'">',
137 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
140 disableFormats: true,
147 <span id='Ext-form-field-Checkbox-cfg-focusCls'> /**
148 </span> * @cfg {String} focusCls The CSS class to use when the checkbox receives focus
149 * (defaults to <tt>'x-form-cb-focus'</tt>)
151 focusCls: Ext.baseCSSPrefix + 'form-cb-focus',
153 <span id='Ext-form-field-Checkbox-cfg-fieldCls'> /**
154 </span> * @cfg {String} fieldCls The default CSS class for the checkbox (defaults to <tt>'x-form-field'</tt>)
157 <span id='Ext-form-field-Checkbox-cfg-fieldBodyCls'> /**
158 </span> * @cfg {String} fieldBodyCls
159 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
160 * Defaults to 'x-form-cb-wrap.
162 fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
164 <span id='Ext-form-field-Checkbox-cfg-checked'> /**
165 </span> * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
169 <span id='Ext-form-field-Checkbox-cfg-checkedCls'> /**
170 </span> * @cfg {String} checkedCls The CSS class added to the component's main element when it is in the checked state.
172 checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
174 <span id='Ext-form-field-Checkbox-cfg-boxLabel'> /**
175 </span> * @cfg {String} boxLabel An optional text label that will appear next to the checkbox. Whether it appears before
176 * or after the checkbox is determined by the {@link #boxLabelAlign} config (defaults to after).
179 <span id='Ext-form-field-Checkbox-cfg-boxLabelCls'> /**
180 </span> * @cfg {String} boxLabelCls The CSS class to be applied to the {@link #boxLabel} element
182 boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
184 <span id='Ext-form-field-Checkbox-cfg-boxLabelAlign'> /**
185 </span> * @cfg {String} boxLabelAlign The position relative to the checkbox where the {@link #boxLabel} should
186 * appear. Recognized values are <tt>'before'</tt> and <tt>'after'</tt>. Defaults to <tt>'after'</tt>.
188 boxLabelAlign: 'after',
190 <span id='Ext-form-field-Checkbox-cfg-inputValue'> /**
191 </span> * @cfg {String} inputValue The value that should go into the generated input element's value attribute and
192 * should be used as the parameter value when submitting as part of a form. Defaults to <tt>"on"</tt>.
196 <span id='Ext-form-field-Checkbox-cfg-uncheckedValue'> /**
197 </span> * @cfg {String} uncheckedValue If configured, this will be submitted as the checkbox's value during form
198 * submit if the checkbox is unchecked. By default this is undefined, which results in nothing being
199 * submitted for the checkbox field when the form is submitted (the default behavior of HTML checkboxes).
202 <span id='Ext-form-field-Checkbox-cfg-handler'> /**
203 </span> * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
204 * handling the {@link #change change event}). The handler is passed the following parameters:
205 * <div class="mdetail-params"><ul>
206 * <li><b>checkbox</b> : Ext.form.field.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
207 * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
208 * </ul></div>
211 <span id='Ext-form-field-Checkbox-cfg-scope'> /**
212 </span> * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
213 * (defaults to this Checkbox).
217 checkChangeEvents: [],
218 inputType: 'checkbox',
219 ariaRole: 'checkbox',
224 initComponent: function(){
225 this.callParent(arguments);
226 this.getManager().add(this);
229 initValue: function() {
231 checked = !!me.checked;
233 <span id='Ext-form-field-Checkbox-property-originalValue'> /**
234 </span> * The original value of the field as configured in the {@link #checked} configuration, or
235 * as loaded by the last form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
236 * setting is <code>true</code>.
238 * @property originalValue
240 me.originalValue = me.lastValue = checked;
242 // Set the initial checked state
243 me.setValue(checked);
247 onRender : function(ct, position) {
249 Ext.applyIf(me.renderSelectors, {
250 <span id='Ext-form-field-Checkbox-property-boxLabelEl'> /**
251 </span> * @property boxLabelEl
252 * @type Ext.core.Element
253 * A reference to the label element created for the {@link #boxLabel}. Only present if the
254 * component has been rendered and has a boxLabel configured.
256 boxLabelEl: 'label.' + me.boxLabelCls
258 Ext.applyIf(me.subTplData, {
259 boxLabel: me.boxLabel,
260 boxLabelCls: me.boxLabelCls,
261 boxLabelAlign: me.boxLabelAlign
264 me.callParent(arguments);
267 initEvents: function() {
270 me.mon(me.inputEl, 'click', me.onBoxClick, me);
273 <span id='Ext-form-field-Checkbox-method-onBoxClick'> /**
274 </span> * @private Handle click on the checkbox button
276 onBoxClick: function(e) {
278 if (!me.disabled && !me.readOnly) {
279 this.setValue(!this.checked);
283 <span id='Ext-form-field-Checkbox-method-getRawValue'> /**
284 </span> * Returns the checked state of the checkbox.
285 * @return {Boolean} True if checked, else false
287 getRawValue: function() {
291 <span id='Ext-form-field-Checkbox-method-getValue'> /**
292 </span> * Returns the checked state of the checkbox.
293 * @return {Boolean} True if checked, else false
295 getValue: function() {
299 <span id='Ext-form-field-Checkbox-method-getSubmitValue'> /**
300 </span> * Returns the submit value for the checkbox which can be used when submitting forms.
301 * @return {Boolean/null} True if checked; otherwise either the {@link #uncheckedValue} or null.
303 getSubmitValue: function() {
304 var unchecked = this.uncheckedValue,
305 uncheckedVal = Ext.isDefined(unchecked) ? unchecked : null;
306 return this.checked ? this.inputValue : unchecked;
309 <span id='Ext-form-field-Checkbox-method-setRawValue'> /**
310 </span> * Sets the checked state of the checkbox.
311 * @param {Boolean/String} value The following values will check the checkbox:
312 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
313 * Any other value will uncheck the checkbox.
314 * @return {Boolean} the new checked state of the checkbox
316 setRawValue: function(value) {
318 inputEl = me.inputEl,
319 inputValue = me.inputValue,
320 checked = (value === true || value === 'true' || value === '1' ||
321 ((Ext.isString(value) && inputValue) ? value == inputValue : me.onRe.test(value)));
324 inputEl.dom.setAttribute('aria-checked', checked);
325 me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
328 me.checked = me.rawValue = checked;
332 <span id='Ext-form-field-Checkbox-method-setValue'> /**
333 </span> * Sets the checked state of the checkbox, and invokes change detection.
334 * @param {Boolean/String} checked The following values will check the checkbox:
335 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
336 * Any other value will uncheck the checkbox.
337 * @return {Ext.form.field.Checkbox} this
339 setValue: function(checked) {
342 // If an array of strings is passed, find all checkboxes in the group with the same name as this
343 // one and check all those whose inputValue is in the array, unchecking all the others. This is to
344 // facilitate setting values from Ext.form.Basic#setValues, but is not publicly documented as we
345 // don't want users depending on this behavior.
346 if (Ext.isArray(checked)) {
347 me.getManager().getByName(me.name).each(function(cb) {
348 cb.setValue(Ext.Array.contains(checked, cb.inputValue));
351 me.callParent(arguments);
358 valueToRaw: function(value) {
359 // No extra conversion for checkboxes
363 <span id='Ext-form-field-Checkbox-method-onChange'> /**
365 * Called when the checkbox's checked state changes. Invokes the {@link #handler} callback
366 * function if specified.
368 onChange: function(newVal, oldVal) {
370 handler = me.handler;
372 handler.call(me.scope || me, me, newVal);
374 me.callParent(arguments);
378 getManager: function() {
379 return Ext.form.CheckboxManager;
382 onEnable: function() {
384 inputEl = me.inputEl;
387 // Can still be disabled if the field is readOnly
388 inputEl.dom.disabled = me.readOnly;
392 setReadOnly: function(readOnly) {
394 inputEl = me.inputEl;
396 // Set the button to disabled when readonly
397 inputEl.dom.disabled = readOnly || me.disabled;
399 me.readOnly = readOnly;
402 <span id='Ext-form-field-Checkbox-method-getBodyNaturalWidth'> /**
403 </span> * @protected Calculate and return the natural width of the bodyEl. It's possible that the initial
404 * rendering will cause the boxLabel to wrap and give us a bad width, so we must prevent wrapping
407 getBodyNaturalWidth: function() {
412 bodyEl.setStyle(ws, 'nowrap');
413 width = bodyEl.getWidth();
414 bodyEl.setStyle(ws, '');