3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.CheckboxGroup
9 * @extends Ext.form.Field
10 * <p>A grouping container for {@link Ext.form.Checkbox} controls.</p>
11 * <p>Sample usage:</p>
13 var myCheckboxGroup = new Ext.form.CheckboxGroup({
15 xtype: 'checkboxgroup',
16 fieldLabel: 'Single Column',
17 itemCls: 'x-check-group-alt',
18 // Put all controls in a single column with width 100%
21 {boxLabel: 'Item 1', name: 'cb-col-1'},
22 {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
23 {boxLabel: 'Item 3', name: 'cb-col-3'}
28 * Creates a new CheckboxGroup
29 * @param {Object} config Configuration options
30 * @xtype checkboxgroup
32 Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
34 * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
35 * to arrange in the group.
38 * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
39 * checkbox/radio controls using automatic layout. This config can take several types of values:
40 * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
41 * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
42 * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
43 * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
44 * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
45 * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
46 * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
47 * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
48 * container you should do so.</p></li></ul>
52 * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
53 * top to bottom before starting on the next column. The number of controls in each column will be automatically
54 * calculated to keep columns as even as possible. The default value is false, so that controls will be added
55 * to columns one at a time, completely filling each row left to right before starting on the next row.
59 * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
60 * If no items are selected at validation time, {@link @blankText} will be used as the error text.
64 * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
65 * select at least one item in this group")
67 blankText : "You must select at least one item in this group",
70 defaultType : 'checkbox',
73 groupCls : 'x-form-check-group',
76 initComponent: function(){
80 * Fires when the state of a child checkbox changes.
81 * @param {Ext.form.CheckboxGroup} this
82 * @param {Array} checked An array containing the checked boxes.
86 Ext.form.CheckboxGroup.superclass.initComponent.call(this);
90 onRender : function(ct, position){
99 defaultType: this.defaultType,
108 if(this.items[0].items){
110 // The container has standard ColumnLayout configs, so pass them in directly
112 Ext.apply(panelCfg, {
113 layoutConfig: {columns: this.items.length},
114 defaults: this.defaults,
117 for(var i=0, len=this.items.length; i<len; i++){
118 Ext.applyIf(this.items[i], colCfg);
123 // The container has field item configs, so we have to generate the column
124 // panels first then move the items into the columns as needed.
126 var numCols, cols = [];
128 if(typeof this.columns == 'string'){ // 'auto' so create a col per item
129 this.columns = this.items.length;
131 if(!Ext.isArray(this.columns)){
133 for(var i=0; i<this.columns; i++){
134 cs.push((100/this.columns)*.01); // distribute by even %
139 numCols = this.columns.length;
141 // Generate the column configs with the correct width setting
142 for(var i=0; i<numCols; i++){
143 var cc = Ext.apply({items:[]}, colCfg);
144 cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
146 cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
151 // Distribute the original items into the columns
153 var rows = Math.ceil(this.items.length / numCols), ri = 0;
154 for(var i=0, len=this.items.length; i<len; i++){
155 if(i>0 && i%rows==0){
158 if(this.items[i].fieldLabel){
159 this.items[i].hideLabel = false;
161 cols[ri].items.push(this.items[i]);
164 for(var i=0, len=this.items.length; i<len; i++){
165 var ci = i % numCols;
166 if(this.items[i].fieldLabel){
167 this.items[i].hideLabel = false;
169 cols[ci].items.push(this.items[i]);
173 Ext.apply(panelCfg, {
174 layoutConfig: {columns: numCols},
179 this.panel = new Ext.Panel(panelCfg);
180 this.panel.ownerCt = this;
181 this.el = this.panel.getEl();
183 if(this.forId && this.itemCls){
184 var l = this.el.up(this.itemCls).child('label', true);
186 l.setAttribute('htmlFor', this.forId);
190 var fields = this.panel.findBy(function(c){
191 return c.isFormField;
194 this.items = new Ext.util.MixedCollection();
195 this.items.addAll(fields);
197 Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
200 afterRender : function(){
201 Ext.form.CheckboxGroup.superclass.afterRender.call(this);
203 this.setValue.apply(this, this.values);
206 this.eachItem(function(item){
207 item.on('check', this.fireChecked, this);
213 doLayout: function(){
214 //ugly method required to layout hidden items
216 this.panel.forceLayout = this.ownerCt.forceLayout;
217 this.panel.doLayout();
222 fireChecked: function(){
224 this.eachItem(function(item){
229 this.fireEvent('change', this, arr);
233 validateValue : function(value){
234 if(!this.allowBlank){
236 this.eachItem(function(f){
238 return (blank = false);
242 this.markInvalid(this.blankText);
250 onDisable : function(){
251 this.eachItem(function(item){
257 onEnable : function(){
258 this.eachItem(function(item){
264 doLayout: function(){
266 this.panel.forceLayout = this.ownerCt.forceLayout;
267 this.panel.doLayout();
272 onResize : function(w, h){
273 this.panel.setSize(w, h);
274 this.panel.doLayout();
277 // inherit docs from Field
279 Ext.form.CheckboxGroup.superclass.reset.call(this);
280 this.eachItem(function(c){
288 * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
289 * in the group. Examples illustrating how this method may be called:
291 // call with name and value
292 myCheckboxGroup.setValue('cb-col-1', true);
293 // call with an array of boolean values
294 myCheckboxGroup.setValue([true, false, false]);
295 // call with an object literal specifying item:value pairs
296 myCheckboxGroup.setValue({
300 // use comma separated string to set items with name to true (checked)
301 myCheckboxGroup.setValue('cb-col-1,cb-col-3');
303 * See {@link Ext.form.Checkbox#setValue} for additional information.
304 * @param {Mixed} id The checkbox to check, or as described by example shown.
305 * @param {Boolean} value (optional) The value to set the item.
306 * @return {Ext.form.CheckboxGroup} this
308 setValue : function(id, value){
310 if(arguments.length == 1){
312 //an array of boolean values
313 Ext.each(id, function(val, idx){
314 var item = this.items.itemAt(idx);
319 }else if(Ext.isObject(id)){
320 //set of name/value pairs
322 var f = this.getBox(i);
328 this.setValueForItem(id);
331 var f = this.getBox(id);
337 this.values = arguments;
343 onDestroy: function(){
344 Ext.destroy(this.panel);
345 Ext.form.CheckboxGroup.superclass.onDestroy.call(this);
349 setValueForItem : function(val){
350 val = String(val).split(',');
351 this.eachItem(function(item){
352 if(val.indexOf(item.inputValue)> -1){
359 getBox : function(id){
361 this.eachItem(function(f){
362 if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
371 * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
372 * @return {Array} An array of the selected checkboxes.
374 getValue : function(){
376 this.eachItem(function(item){
385 eachItem: function(fn){
386 if(this.items && this.items.each){
387 this.items.each(fn, this);
399 initValue : Ext.emptyFn,
404 getValue : Ext.emptyFn,
406 * @method getRawValue
409 getRawValue : Ext.emptyFn,
412 * @method setRawValue
415 setRawValue : Ext.emptyFn
419 Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);