3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.form.CheckboxGroup"></div>/**
9 * @class Ext.form.CheckboxGroup
10 * @extends Ext.form.Field
11 * <p>A grouping container for {@link Ext.form.Checkbox} controls.</p>
12 * <p>Sample usage:</p>
14 var myCheckboxGroup = new Ext.form.CheckboxGroup({
16 xtype: 'checkboxgroup',
17 fieldLabel: 'Single Column',
18 itemCls: 'x-check-group-alt',
19 // Put all controls in a single column with width 100%
22 {boxLabel: 'Item 1', name: 'cb-col-1'},
23 {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
24 {boxLabel: 'Item 3', name: 'cb-col-3'}
29 * Creates a new CheckboxGroup
30 * @param {Object} config Configuration options
31 * @xtype checkboxgroup
33 Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
34 <div id="cfg-Ext.form.CheckboxGroup-items"></div>/**
35 * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
36 * to arrange in the group.
38 <div id="cfg-Ext.form.CheckboxGroup-columns"></div>/**
39 * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
40 * checkbox/radio controls using automatic layout. This config can take several types of values:
41 * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
42 * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
43 * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
44 * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
45 * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
46 * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
47 * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
48 * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
49 * container you should do so.</p></li></ul>
52 <div id="cfg-Ext.form.CheckboxGroup-vertical"></div>/**
53 * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
54 * top to bottom before starting on the next column. The number of controls in each column will be automatically
55 * calculated to keep columns as even as possible. The default value is false, so that controls will be added
56 * to columns one at a time, completely filling each row left to right before starting on the next row.
59 <div id="cfg-Ext.form.CheckboxGroup-allowBlank"></div>/**
60 * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
61 * If no items are selected at validation time, {@link @blankText} will be used as the error text.
64 <div id="cfg-Ext.form.CheckboxGroup-blankText"></div>/**
65 * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
66 * select at least one item in this group")
68 blankText : "You must select at least one item in this group",
71 defaultType : 'checkbox',
74 groupCls : 'x-form-check-group',
77 initComponent: function(){
79 <div id="event-Ext.form.CheckboxGroup-change"></div>/**
81 * Fires when the state of a child checkbox changes.
82 * @param {Ext.form.CheckboxGroup} this
83 * @param {Array} checked An array containing the checked boxes.
87 Ext.form.CheckboxGroup.superclass.initComponent.call(this);
91 onRender : function(ct, position){
100 defaultType: this.defaultType,
109 if(this.items[0].items){
111 // The container has standard ColumnLayout configs, so pass them in directly
113 Ext.apply(panelCfg, {
114 layoutConfig: {columns: this.items.length},
115 defaults: this.defaults,
118 for(var i=0, len=this.items.length; i<len; i++){
119 Ext.applyIf(this.items[i], colCfg);
124 // The container has field item configs, so we have to generate the column
125 // panels first then move the items into the columns as needed.
127 var numCols, cols = [];
129 if(typeof this.columns == 'string'){ // 'auto' so create a col per item
130 this.columns = this.items.length;
132 if(!Ext.isArray(this.columns)){
134 for(var i=0; i<this.columns; i++){
135 cs.push((100/this.columns)*.01); // distribute by even %
140 numCols = this.columns.length;
142 // Generate the column configs with the correct width setting
143 for(var i=0; i<numCols; i++){
144 var cc = Ext.apply({items:[]}, colCfg);
145 cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
147 cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
152 // Distribute the original items into the columns
154 var rows = Math.ceil(this.items.length / numCols), ri = 0;
155 for(var i=0, len=this.items.length; i<len; i++){
156 if(i>0 && i%rows==0){
159 if(this.items[i].fieldLabel){
160 this.items[i].hideLabel = false;
162 cols[ri].items.push(this.items[i]);
165 for(var i=0, len=this.items.length; i<len; i++){
166 var ci = i % numCols;
167 if(this.items[i].fieldLabel){
168 this.items[i].hideLabel = false;
170 cols[ci].items.push(this.items[i]);
174 Ext.apply(panelCfg, {
175 layoutConfig: {columns: numCols},
180 this.panel = new Ext.Panel(panelCfg);
181 this.panel.ownerCt = this;
182 this.el = this.panel.getEl();
184 if(this.forId && this.itemCls){
185 var l = this.el.up(this.itemCls).child('label', true);
187 l.setAttribute('htmlFor', this.forId);
191 var fields = this.panel.findBy(function(c){
192 return c.isFormField;
195 this.items = new Ext.util.MixedCollection();
196 this.items.addAll(fields);
198 Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
201 afterRender : function(){
202 Ext.form.CheckboxGroup.superclass.afterRender.call(this);
204 this.setValue.apply(this, this.values);
207 this.eachItem(function(item){
208 item.on('check', this.fireChecked, this);
214 doLayout: function(){
215 //ugly method required to layout hidden items
217 this.panel.forceLayout = this.ownerCt.forceLayout;
218 this.panel.doLayout();
223 fireChecked: function(){
225 this.eachItem(function(item){
230 this.fireEvent('change', this, arr);
234 validateValue : function(value){
235 if(!this.allowBlank){
237 this.eachItem(function(f){
239 return (blank = false);
243 this.markInvalid(this.blankText);
251 onDisable : function(){
252 this.eachItem(function(item){
258 onEnable : function(){
259 this.eachItem(function(item){
265 doLayout: function(){
267 this.panel.forceLayout = this.ownerCt.forceLayout;
268 this.panel.doLayout();
273 onResize : function(w, h){
274 this.panel.setSize(w, h);
275 this.panel.doLayout();
278 // inherit docs from Field
280 Ext.form.CheckboxGroup.superclass.reset.call(this);
281 this.eachItem(function(c){
288 <div id="method-Ext.form.CheckboxGroup-setValue"></div>/**
289 * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
290 * in the group. Examples illustrating how this method may be called:
292 // call with name and value
293 myCheckboxGroup.setValue('cb-col-1', true);
294 // call with an array of boolean values
295 myCheckboxGroup.setValue([true, false, false]);
296 // call with an object literal specifying item:value pairs
297 myCheckboxGroup.setValue({
301 // use comma separated string to set items with name to true (checked)
302 myCheckboxGroup.setValue('cb-col-1,cb-col-3');
304 * See {@link Ext.form.Checkbox#setValue} for additional information.
305 * @param {Mixed} id The checkbox to check, or as described by example shown.
306 * @param {Boolean} value (optional) The value to set the item.
307 * @return {Ext.form.CheckboxGroup} this
309 setValue : function(id, value){
311 if(arguments.length == 1){
313 //an array of boolean values
314 Ext.each(id, function(val, idx){
315 var item = this.items.itemAt(idx);
320 }else if(Ext.isObject(id)){
321 //set of name/value pairs
323 var f = this.getBox(i);
329 this.setValueForItem(id);
332 var f = this.getBox(id);
338 this.values = arguments;
344 onDestroy: function(){
345 Ext.destroy(this.panel);
346 Ext.form.CheckboxGroup.superclass.onDestroy.call(this);
350 setValueForItem : function(val){
351 val = String(val).split(',');
352 this.eachItem(function(item){
353 if(val.indexOf(item.inputValue)> -1){
360 getBox : function(id){
362 this.eachItem(function(f){
363 if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
372 * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
373 * @return {Array} An array of the selected checkboxes.
375 getValue : function(){
377 this.eachItem(function(item){
386 eachItem: function(fn){
387 if(this.items && this.items.each){
388 this.items.each(fn, this);
392 <div id="cfg-Ext.form.CheckboxGroup-name"></div>/**
396 <div id="method-Ext.form.CheckboxGroup-initValue"></div>/**
400 initValue : Ext.emptyFn,
401 <div id="method-Ext.form.CheckboxGroup-getValue"></div>/**
405 getValue : Ext.emptyFn,
406 <div id="method-Ext.form.CheckboxGroup-getRawValue"></div>/**
407 * @method getRawValue
410 getRawValue : Ext.emptyFn,
412 <div id="method-Ext.form.CheckboxGroup-setRawValue"></div>/**
413 * @method setRawValue
416 setRawValue : Ext.emptyFn
420 Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);