3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.form.CheckboxGroup
\r
9 * @extends Ext.form.Field
\r
10 * <p>A grouping container for {@link Ext.form.Checkbox} controls.</p>
\r
11 * <p>Sample usage:</p>
\r
13 var myCheckboxGroup = new Ext.form.CheckboxGroup({
\r
15 xtype: 'checkboxgroup',
\r
16 fieldLabel: 'Single Column',
\r
17 itemCls: 'x-check-group-alt',
\r
18 // Put all controls in a single column with width 100%
\r
21 {boxLabel: 'Item 1', name: 'cb-col-1'},
\r
22 {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
\r
23 {boxLabel: 'Item 3', name: 'cb-col-3'}
\r
28 * Creates a new CheckboxGroup
\r
29 * @param {Object} config Configuration options
\r
30 * @xtype checkboxgroup
\r
32 Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
\r
34 * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
\r
35 * to arrange in the group.
\r
38 * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
\r
39 * checkbox/radio controls using automatic layout. This config can take several types of values:
\r
40 * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
\r
41 * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
\r
42 * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
\r
43 * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
\r
44 * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
\r
45 * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
\r
46 * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
\r
47 * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
\r
48 * container you should do so.</p></li></ul>
\r
52 * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
\r
53 * top to bottom before starting on the next column. The number of controls in each column will be automatically
\r
54 * calculated to keep columns as even as possible. The default value is false, so that controls will be added
\r
55 * to columns one at a time, completely filling each row left to right before starting on the next row.
\r
59 * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
\r
60 * If no items are selected at validation time, {@link @blankText} will be used as the error text.
\r
64 * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
\r
65 * select at least one item in this group")
\r
67 blankText : "You must select at least one item in this group",
\r
70 defaultType : 'checkbox',
\r
73 groupCls : 'x-form-check-group',
\r
76 initComponent: function(){
\r
80 * Fires when the state of a child checkbox changes.
\r
81 * @param {Ext.form.CheckboxGroup} this
\r
82 * @param {Array} checked An array containing the checked boxes.
\r
86 Ext.form.CheckboxGroup.superclass.initComponent.call(this);
\r
90 onRender : function(ct, position){
\r
98 bufferResize: false // Default this to false, since it doesn't really have a proper ownerCt.
\r
101 defaultType: this.defaultType,
\r
110 if(this.items[0].items){
\r
112 // The container has standard ColumnLayout configs, so pass them in directly
\r
114 Ext.apply(panelCfg, {
\r
115 layoutConfig: {columns: this.items.length},
\r
116 defaults: this.defaults,
\r
119 for(var i=0, len=this.items.length; i<len; i++){
\r
120 Ext.applyIf(this.items[i], colCfg);
\r
125 // The container has field item configs, so we have to generate the column
\r
126 // panels first then move the items into the columns as needed.
\r
128 var numCols, cols = [];
\r
130 if(typeof this.columns == 'string'){ // 'auto' so create a col per item
\r
131 this.columns = this.items.length;
\r
133 if(!Ext.isArray(this.columns)){
\r
135 for(var i=0; i<this.columns; i++){
\r
136 cs.push((100/this.columns)*.01); // distribute by even %
\r
141 numCols = this.columns.length;
\r
143 // Generate the column configs with the correct width setting
\r
144 for(var i=0; i<numCols; i++){
\r
145 var cc = Ext.apply({items:[]}, colCfg);
\r
146 cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
\r
148 cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
\r
153 // Distribute the original items into the columns
\r
155 var rows = Math.ceil(this.items.length / numCols), ri = 0;
\r
156 for(var i=0, len=this.items.length; i<len; i++){
\r
157 if(i>0 && i%rows==0){
\r
160 if(this.items[i].fieldLabel){
\r
161 this.items[i].hideLabel = false;
\r
163 cols[ri].items.push(this.items[i]);
\r
166 for(var i=0, len=this.items.length; i<len; i++){
\r
167 var ci = i % numCols;
\r
168 if(this.items[i].fieldLabel){
\r
169 this.items[i].hideLabel = false;
\r
171 cols[ci].items.push(this.items[i]);
\r
175 Ext.apply(panelCfg, {
\r
176 layoutConfig: {columns: numCols},
\r
181 this.panel = new Ext.Panel(panelCfg);
\r
182 this.panel.ownerCt = this;
\r
183 this.el = this.panel.getEl();
\r
185 if(this.forId && this.itemCls){
\r
186 var l = this.el.up(this.itemCls).child('label', true);
\r
188 l.setAttribute('htmlFor', this.forId);
\r
192 var fields = this.panel.findBy(function(c){
\r
193 return c.isFormField;
\r
196 this.items = new Ext.util.MixedCollection();
\r
197 this.items.addAll(fields);
\r
199 Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
\r
202 initValue : function(){
\r
204 this.setValue.apply(this, this.buffered ? this.value : [this.value]);
\r
205 delete this.buffered;
\r
210 afterRender : function(){
\r
211 Ext.form.CheckboxGroup.superclass.afterRender.call(this);
\r
212 this.eachItem(function(item){
\r
213 item.on('check', this.fireChecked, this);
\r
214 item.inGroup = true;
\r
219 doLayout: function(){
\r
220 //ugly method required to layout hidden items
\r
222 this.panel.forceLayout = this.ownerCt.forceLayout;
\r
223 this.panel.doLayout();
\r
228 fireChecked: function(){
\r
230 this.eachItem(function(item){
\r
235 this.fireEvent('change', this, arr);
\r
239 validateValue : function(value){
\r
240 if(!this.allowBlank){
\r
242 this.eachItem(function(f){
\r
244 return (blank = false);
\r
248 this.markInvalid(this.blankText);
\r
256 isDirty: function(){
\r
257 //override the behaviour to check sub items.
\r
258 if (this.disabled || !this.rendered) {
\r
263 this.eachItem(function(item){
\r
264 if(item.isDirty()){
\r
273 onDisable : function(){
\r
274 this.eachItem(function(item){
\r
280 onEnable : function(){
\r
281 this.eachItem(function(item){
\r
287 doLayout: function(){
\r
289 this.panel.forceLayout = this.ownerCt.forceLayout;
\r
290 this.panel.doLayout();
\r
295 onResize : function(w, h){
\r
296 this.panel.setSize(w, h);
\r
297 this.panel.doLayout();
\r
300 // inherit docs from Field
\r
301 reset : function(){
\r
302 Ext.form.CheckboxGroup.superclass.reset.call(this);
\r
303 this.eachItem(function(c){
\r
311 * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
\r
312 * in the group. Examples illustrating how this method may be called:
\r
314 // call with name and value
\r
315 myCheckboxGroup.setValue('cb-col-1', true);
\r
316 // call with an array of boolean values
\r
317 myCheckboxGroup.setValue([true, false, false]);
\r
318 // call with an object literal specifying item:value pairs
\r
319 myCheckboxGroup.setValue({
\r
323 // use comma separated string to set items with name to true (checked)
\r
324 myCheckboxGroup.setValue('cb-col-1,cb-col-3');
\r
326 * See {@link Ext.form.Checkbox#setValue} for additional information.
\r
327 * @param {Mixed} id The checkbox to check, or as described by example shown.
\r
328 * @param {Boolean} value (optional) The value to set the item.
\r
329 * @return {Ext.form.CheckboxGroup} this
\r
331 setValue: function(){
\r
333 this.onSetValue.apply(this, arguments);
\r
335 this.buffered = true;
\r
336 this.value = arguments;
\r
341 onSetValue: function(id, value){
\r
342 if(arguments.length == 1){
\r
343 if(Ext.isArray(id)){
\r
344 // an array of boolean values
\r
345 Ext.each(id, function(val, idx){
\r
346 var item = this.items.itemAt(idx);
\r
348 item.setValue(val);
\r
351 }else if(Ext.isObject(id)){
\r
352 // set of name/value pairs
\r
354 var f = this.getBox(i);
\r
360 this.setValueForItem(id);
\r
363 var f = this.getBox(id);
\r
371 onDestroy: function(){
\r
372 Ext.destroy(this.panel);
\r
373 Ext.form.CheckboxGroup.superclass.onDestroy.call(this);
\r
377 setValueForItem : function(val){
\r
378 val = String(val).split(',');
\r
379 this.eachItem(function(item){
\r
380 if(val.indexOf(item.inputValue)> -1){
\r
381 item.setValue(true);
\r
387 getBox : function(id){
\r
389 this.eachItem(function(f){
\r
390 if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
\r
399 * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
\r
400 * @return {Array} An array of the selected checkboxes.
\r
402 getValue : function(){
\r
404 this.eachItem(function(item){
\r
413 eachItem: function(fn){
\r
414 if(this.items && this.items.each){
\r
415 this.items.each(fn, this);
\r
420 * @cfg {String} name
\r
425 * @method getRawValue
\r
428 getRawValue : Ext.emptyFn,
\r
431 * @method setRawValue
\r
434 setRawValue : Ext.emptyFn
\r
438 Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);
\r