2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
10
* @class Ext.form.CheckboxGroup
11
* @extends Ext.form.Field
12
* A grouping container for {@link Ext.form.Checkbox} controls.
14
* Creates a new CheckboxGroup
15
* @param {Object} config Configuration options
16
* @xtype checkboxgroup
18
Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
20
* @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
21
* to arrange in the group.
24
* @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
25
* checkbox/radio controls using automatic layout. This config can take several types of values:
26
* <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
27
* of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
28
* <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
29
* created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
30
* <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
31
* (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
32
* be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
33
* values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
34
* container you should do so.</p></li></ul>
38
* @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
39
* top to bottom before starting on the next column. The number of controls in each column will be automatically
40
* calculated to keep columns as even as possible. The default value is false, so that controls will be added
41
* to columns one at a time, completely filling each row left to right before starting on the next row.
45
* @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
46
* If no items are selected at validation time, {@link @blankText} will be used as the error text.
50
* @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
51
* select at least one item in this group")
53
blankText : "You must select at least one item in this group",
56
defaultType : 'checkbox',
59
groupCls: 'x-form-check-group',
62
onRender : function(ct, position){
71
defaultType: this.defaultType,
80
if(this.items[0].items){
82
// The container has standard ColumnLayout configs, so pass them in directly
85
layoutConfig: {columns: this.items.length},
86
defaults: this.defaults,
89
for(var i=0, len=this.items.length; i<len; i++){
90
Ext.applyIf(this.items[i], colCfg);
95
// The container has field item configs, so we have to generate the column
96
// panels first then move the items into the columns as needed.
98
var numCols, cols = [];
100
if(typeof this.columns == 'string'){ // 'auto' so create a col per item
101
this.columns = this.items.length;
103
if(!Ext.isArray(this.columns)){
105
for(var i=0; i<this.columns; i++){
106
cs.push((100/this.columns)*.01); // distribute by even %
111
numCols = this.columns.length;
113
// Generate the column configs with the correct width setting
114
for(var i=0; i<numCols; i++){
115
var cc = Ext.apply({items:[]}, colCfg);
116
cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
118
cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
123
// Distribute the original items into the columns
125
var rows = Math.ceil(this.items.length / numCols), ri = 0;
126
for(var i=0, len=this.items.length; i<len; i++){
127
if(i>0 && i%rows==0){
130
if(this.items[i].fieldLabel){
131
this.items[i].hideLabel = false;
133
cols[ri].items.push(this.items[i]);
136
for(var i=0, len=this.items.length; i<len; i++){
137
var ci = i % numCols;
138
if(this.items[i].fieldLabel){
139
this.items[i].hideLabel = false;
141
cols[ci].items.push(this.items[i]);
145
Ext.apply(panelCfg, {
146
layoutConfig: {columns: numCols},
151
this.panel = new Ext.Panel(panelCfg);
152
this.el = this.panel.getEl();
154
if(this.forId && this.itemCls){
155
var l = this.el.up(this.itemCls).child('label', true);
157
l.setAttribute('htmlFor', this.forId);
161
var fields = this.panel.findBy(function(c){
162
return c.isFormField;
165
this.items = new Ext.util.MixedCollection();
166
this.items.addAll(fields);
168
Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
171
afterRender: function(){
172
Ext.form.CheckboxGroup.superclass.afterRender.call(this);
174
this.setValue.apply(this, this.values);
180
validateValue : function(value){
181
if(!this.allowBlank){
183
this.items.each(function(f){
185
return blank = false;
189
this.markInvalid(this.blankText);
197
onDisable : function(){
198
this.items.each(function(item){
204
onEnable : function(){
205
this.items.each(function(item){
211
onResize : function(w, h){
212
this.panel.setSize(w, h);
213
this.panel.doLayout();
216
// inherit docs from Field
218
Ext.form.CheckboxGroup.superclass.reset.call(this);
219
this.items.each(function(c){
227
* Sets the checked radio in the group.
228
* @param {Mixed} id The checkbox to check. This can also be an array of boolean values, or an object literal containing the set of values, eg:
230
group.setValues([true, true, false, true, false]);
237
* @param {Boolean} value (optional) The value to set the radio.
238
* @return {Ext.form.RadioGroup} this
240
setValue : function(id, value){
242
if(arguments.length == 1){
244
//an array of boolean values
245
Ext.each(id, function(val, idx){
246
var item = this.items.itemAt(idx);
251
}else if(Ext.isObject(id)){
252
//set of name/value pairs
254
var f = this.getBox(i);
261
var f = this.getBox(id);
267
this.values = arguments;
272
getBox: function(id){
274
this.items.each(function(f){
275
if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
284
* Gets an array of the selected {@link Ext.form.Checkbox} in the group.
285
* @return {Array} An array of the selected checkboxes.
287
getValue: function(){
290
this.items.each(function(item){
307
initValue : Ext.emptyFn,
312
getValue : Ext.emptyFn,
314
* @method getRawValue
317
getRawValue : Ext.emptyFn,
320
* @method setRawValue
323
setRawValue : Ext.emptyFn
327
Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);