1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-form.field.Checkbox-method-constructor'><span id='Ext-form.field.Checkbox'>/**
2 </span></span> * @class Ext.form.field.Checkbox
3 * @extends Ext.form.field.Base
5 Single checkbox field. Can be used as a direct replacement for traditional checkbox fields. Also serves as a
6 parent class for {@link Ext.form.field.Radio radio buttons}.
8 __Labeling:__ In addition to the {@link Ext.form.Labelable standard field labeling options}, checkboxes
9 may be given an optional {@link #boxLabel} which will be displayed immediately after checkbox. Also see
10 {@link Ext.form.CheckboxGroup} for a convenient method of grouping related checkboxes.
13 The main value of a checkbox is a boolean, indicating whether or not the checkbox is checked.
14 The following values will check the checkbox:
20 Any other value will uncheck the checkbox.
22 In addition to the main boolean value, you may also specify a separate {@link #inputValue}. This will be
23 sent as the parameter value when the form is {@link Ext.form.Basic#submit submitted}. You will want to set
24 this value if you have multiple checkboxes with the same {@link #name}. If not specified, the value `on`
26 {@img Ext.form.Checkbox/Ext.form.Checkbox.png Ext.form.Checkbox Checkbox component}
29 Ext.create('Ext.form.Panel', {
32 title : 'Pizza Order',
35 xtype : 'fieldcontainer',
36 fieldLabel : 'Toppings',
37 defaultType: 'checkboxfield',
40 boxLabel : 'Anchovies',
45 boxLabel : 'Artichoke Hearts',
63 var checkbox = Ext.getCmp('checkbox3');
64 checkbox.setValue(true);
71 var checkbox1 = Ext.getCmp('checkbox1'),
72 checkbox2 = Ext.getCmp('checkbox2'),
73 checkbox3 = Ext.getCmp('checkbox3');
75 checkbox1.setValue(true);
76 checkbox2.setValue(true);
77 checkbox3.setValue(true);
83 var checkbox1 = Ext.getCmp('checkbox1'),
84 checkbox2 = Ext.getCmp('checkbox2'),
85 checkbox3 = Ext.getCmp('checkbox3');
87 checkbox1.setValue(false);
88 checkbox2.setValue(false);
89 checkbox3.setValue(false);
93 renderTo: Ext.getBody()
97 * Creates a new Checkbox
98 * @param {Object} config Configuration options
99 * @xtype checkboxfield
100 * @docauthor Robert Dougan <rob@sencha.com>
103 Ext.define('Ext.form.field.Checkbox', {
104 extend: 'Ext.form.field.Base',
105 alias: ['widget.checkboxfield', 'widget.checkbox'],
106 alternateClassName: 'Ext.form.Checkbox',
107 requires: ['Ext.XTemplate', 'Ext.form.CheckboxManager'],
110 '<tpl if="boxLabel && boxLabelAlign == \'before\'">',
111 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
113 // Creates not an actual checkbox, but a button which is given aria role="checkbox" and
114 // styled with a custom checkbox image. This allows greater control and consistency in
115 // styling, and using a button allows it to gain focus and handle keyboard nav properly.
116 '<input type="button" id="{id}" ',
117 '<tpl if="tabIdx">tabIndex="{tabIdx}" </tpl>',
118 'class="{fieldCls} {typeCls}" autocomplete="off" hidefocus="true" />',
119 '<tpl if="boxLabel && boxLabelAlign == \'after\'">',
120 '<label class="{boxLabelCls} {boxLabelCls}-{boxLabelAlign}" for="{id}">{boxLabel}</label>',
123 disableFormats: true,
130 <span id='Ext-form.field.Checkbox-cfg-focusCls'> /**
131 </span> * @cfg {String} focusCls The CSS class to use when the checkbox receives focus
132 * (defaults to <tt>'x-form-cb-focus'</tt>)
134 focusCls: Ext.baseCSSPrefix + 'form-cb-focus',
136 <span id='Ext-form.field.Checkbox-cfg-fieldCls'> /**
137 </span> * @cfg {String} fieldCls The default CSS class for the checkbox (defaults to <tt>'x-form-field'</tt>)
140 <span id='Ext-form.field.Checkbox-cfg-fieldBodyCls'> /**
141 </span> * @cfg {String} fieldBodyCls
142 * An extra CSS class to be applied to the body content element in addition to {@link #fieldBodyCls}.
143 * Defaults to 'x-form-cb-wrap.
145 fieldBodyCls: Ext.baseCSSPrefix + 'form-cb-wrap',
147 <span id='Ext-form.field.Checkbox-cfg-checked'> /**
148 </span> * @cfg {Boolean} checked <tt>true</tt> if the checkbox should render initially checked (defaults to <tt>false</tt>)
152 <span id='Ext-form.field.Checkbox-cfg-checkedCls'> /**
153 </span> * @cfg {String} checkedCls The CSS class added to the component's main element when it is in the checked state.
155 checkedCls: Ext.baseCSSPrefix + 'form-cb-checked',
157 <span id='Ext-form.field.Checkbox-cfg-boxLabel'> /**
158 </span> * @cfg {String} boxLabel An optional text label that will appear next to the checkbox. Whether it appears before
159 * or after the checkbox is determined by the {@link #boxLabelAlign} config (defaults to after).
162 <span id='Ext-form.field.Checkbox-cfg-boxLabelCls'> /**
163 </span> * @cfg {String} boxLabelCls The CSS class to be applied to the {@link #boxLabel} element
165 boxLabelCls: Ext.baseCSSPrefix + 'form-cb-label',
167 <span id='Ext-form.field.Checkbox-cfg-boxLabelAlign'> /**
168 </span> * @cfg {String} boxLabelAlign The position relative to the checkbox where the {@link #boxLabel} should
169 * appear. Recognized values are <tt>'before'</tt> and <tt>'after'</tt>. Defaults to <tt>'after'</tt>.
171 boxLabelAlign: 'after',
173 <span id='Ext-form.field.Checkbox-cfg-inputValue'> /**
174 </span> * @cfg {String} inputValue The value that should go into the generated input element's value attribute and
175 * should be used as the parameter value when submitting as part of a form. Defaults to <tt>"on"</tt>.
179 <span id='Ext-form.field.Checkbox-cfg-uncheckedValue'> /**
180 </span> * @cfg {String} uncheckedValue If configured, this will be submitted as the checkbox's value during form
181 * submit if the checkbox is unchecked. By default this is undefined, which results in nothing being
182 * submitted for the checkbox field when the form is submitted (the default behavior of HTML checkboxes).
185 <span id='Ext-form.field.Checkbox-cfg-handler'> /**
186 </span> * @cfg {Function} handler A function called when the {@link #checked} value changes (can be used instead of
187 * handling the {@link #change change event}). The handler is passed the following parameters:
188 * <div class="mdetail-params"><ul>
189 * <li><b>checkbox</b> : Ext.form.field.Checkbox<div class="sub-desc">The Checkbox being toggled.</div></li>
190 * <li><b>checked</b> : Boolean<div class="sub-desc">The new checked state of the checkbox.</div></li>
191 * </ul></div>
194 <span id='Ext-form.field.Checkbox-cfg-scope'> /**
195 </span> * @cfg {Object} scope An object to use as the scope ('this' reference) of the {@link #handler} function
196 * (defaults to this Checkbox).
200 checkChangeEvents: [],
201 inputType: 'checkbox',
202 ariaRole: 'checkbox',
207 initComponent: function(){
208 this.callParent(arguments);
209 this.getManager().add(this);
212 initValue: function() {
214 checked = !!me.checked;
216 <span id='Ext-form.field.Checkbox-property-originalValue'> /**
217 </span> * The original value of the field as configured in the {@link #checked} configuration, or
218 * as loaded by the last form load operation if the form's {@link Ext.form.Basic#trackResetOnLoad trackResetOnLoad}
219 * setting is <code>true</code>.
221 * @property originalValue
223 me.originalValue = me.lastValue = checked;
225 // Set the initial checked state
226 me.setValue(checked);
230 onRender : function(ct, position) {
232 Ext.applyIf(me.renderSelectors, {
233 <span id='Ext-form.field.Checkbox-property-boxLabelEl'> /**
234 </span> * @property boxLabelEl
235 * @type Ext.core.Element
236 * A reference to the label element created for the {@link #boxLabel}. Only present if the
237 * component has been rendered and has a boxLabel configured.
239 boxLabelEl: 'label.' + me.boxLabelCls
241 Ext.applyIf(me.subTplData, {
242 boxLabel: me.boxLabel,
243 boxLabelCls: me.boxLabelCls,
244 boxLabelAlign: me.boxLabelAlign
247 me.callParent(arguments);
250 initEvents: function() {
253 me.mon(me.inputEl, 'click', me.onBoxClick, me);
256 <span id='Ext-form.field.Checkbox-method-onBoxClick'> /**
257 </span> * @private Handle click on the checkbox button
259 onBoxClick: function(e) {
261 if (!me.disabled && !me.readOnly) {
262 this.setValue(!this.checked);
266 <span id='Ext-form.field.Checkbox-method-getRawValue'> /**
267 </span> * Returns the checked state of the checkbox.
268 * @return {Boolean} True if checked, else false
270 getRawValue: function() {
274 <span id='Ext-form.field.Checkbox-method-getValue'> /**
275 </span> * Returns the checked state of the checkbox.
276 * @return {Boolean} True if checked, else false
278 getValue: function() {
282 <span id='Ext-form.field.Checkbox-method-getSubmitValue'> /**
283 </span> * Returns the submit value for the checkbox which can be used when submitting forms.
284 * @return {Boolean/null} True if checked; otherwise either the {@link #uncheckedValue} or null.
286 getSubmitValue: function() {
287 return this.checked ? this.inputValue : (this.uncheckedValue || null);
290 getModelData: function() {
291 return this.getSubmitData();
294 <span id='Ext-form.field.Checkbox-method-setRawValue'> /**
295 </span> * Sets the checked state of the checkbox.
296 * @param {Boolean/String} value The following values will check the checkbox:
297 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
298 * Any other value will uncheck the checkbox.
299 * @return {Boolean} the new checked state of the checkbox
301 setRawValue: function(value) {
303 inputEl = me.inputEl,
304 inputValue = me.inputValue,
305 checked = (value === true || value === 'true' || value === '1' ||
306 ((Ext.isString(value) && inputValue) ? value == inputValue : me.onRe.test(value)));
309 inputEl.dom.setAttribute('aria-checked', checked);
310 me[checked ? 'addCls' : 'removeCls'](me.checkedCls);
313 me.checked = me.rawValue = checked;
317 <span id='Ext-form.field.Checkbox-method-setValue'> /**
318 </span> * Sets the checked state of the checkbox, and invokes change detection.
319 * @param {Boolean/String} checked The following values will check the checkbox:
320 * <code>true, 'true', '1', or 'on'</code>, as well as a String that matches the {@link #inputValue}.
321 * Any other value will uncheck the checkbox.
322 * @return {Ext.form.field.Checkbox} this
324 setValue: function(checked) {
327 // If an array of strings is passed, find all checkboxes in the group with the same name as this
328 // one and check all those whose inputValue is in the array, unchecking all the others. This is to
329 // facilitate setting values from Ext.form.Basic#setValues, but is not publicly documented as we
330 // don't want users depending on this behavior.
331 if (Ext.isArray(checked)) {
332 me.getManager().getByName(me.name).each(function(cb) {
333 cb.setValue(Ext.Array.contains(checked, cb.inputValue));
336 me.callParent(arguments);
343 valueToRaw: function(value) {
344 // No extra conversion for checkboxes
348 <span id='Ext-form.field.Checkbox-method-onChange'> /**
350 * Called when the checkbox's checked state changes. Invokes the {@link #handler} callback
351 * function if specified.
353 onChange: function(newVal, oldVal) {
355 handler = me.handler;
357 handler.call(me.scope || me, me, newVal);
359 me.callParent(arguments);
363 getManager: function() {
364 return Ext.form.CheckboxManager;
367 onEnable: function() {
369 inputEl = me.inputEl;
372 // Can still be disabled if the field is readOnly
373 inputEl.dom.disabled = me.readOnly;
377 setReadOnly: function(readOnly) {
379 inputEl = me.inputEl;
381 // Set the button to disabled when readonly
382 inputEl.dom.disabled = readOnly || me.disabled;
384 me.readOnly = readOnly;
387 <span id='Ext-form.field.Checkbox-method-getBodyNaturalWidth'> /**
388 </span> * @protected Calculate and return the natural width of the bodyEl. It's possible that the initial
389 * rendering will cause the boxLabel to wrap and give us a bad width, so we must prevent wrapping
392 getBodyNaturalWidth: function() {
397 bodyEl.setStyle(ws, 'nowrap');
398 width = bodyEl.getWidth();
399 bodyEl.setStyle(ws, '');
404 </pre></pre></body></html>