3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.form.CheckboxGroup"></div>/**
\r
15 * @class Ext.form.CheckboxGroup
\r
16 * @extends Ext.form.Field
\r
17 * <p>A grouping container for {@link Ext.form.Checkbox} controls.</p>
\r
18 * <p>Sample usage:</p>
\r
20 var myCheckboxGroup = new Ext.form.CheckboxGroup({
\r
22 xtype: 'checkboxgroup',
\r
23 fieldLabel: 'Single Column',
\r
24 itemCls: 'x-check-group-alt',
\r
25 // Put all controls in a single column with width 100%
\r
28 {boxLabel: 'Item 1', name: 'cb-col-1'},
\r
29 {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
\r
30 {boxLabel: 'Item 3', name: 'cb-col-3'}
\r
35 * Creates a new CheckboxGroup
\r
36 * @param {Object} config Configuration options
\r
37 * @xtype checkboxgroup
\r
39 Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
\r
40 <div id="cfg-Ext.form.CheckboxGroup-items"></div>/**
\r
41 * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
\r
42 * to arrange in the group.
\r
44 <div id="cfg-Ext.form.CheckboxGroup-columns"></div>/**
\r
45 * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
\r
46 * checkbox/radio controls using automatic layout. This config can take several types of values:
\r
47 * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
\r
48 * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
\r
49 * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
\r
50 * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
\r
51 * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
\r
52 * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
\r
53 * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
\r
54 * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
\r
55 * container you should do so.</p></li></ul>
\r
58 <div id="cfg-Ext.form.CheckboxGroup-vertical"></div>/**
\r
59 * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
\r
60 * top to bottom before starting on the next column. The number of controls in each column will be automatically
\r
61 * calculated to keep columns as even as possible. The default value is false, so that controls will be added
\r
62 * to columns one at a time, completely filling each row left to right before starting on the next row.
\r
65 <div id="cfg-Ext.form.CheckboxGroup-allowBlank"></div>/**
\r
66 * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
\r
67 * If no items are selected at validation time, {@link @blankText} will be used as the error text.
\r
70 <div id="cfg-Ext.form.CheckboxGroup-blankText"></div>/**
\r
71 * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
\r
72 * select at least one item in this group")
\r
74 blankText : "You must select at least one item in this group",
\r
77 defaultType : 'checkbox',
\r
80 groupCls : 'x-form-check-group',
\r
83 initComponent: function(){
\r
85 <div id="event-Ext.form.CheckboxGroup-change"></div>/**
\r
87 * Fires when the state of a child checkbox changes.
\r
88 * @param {Ext.form.CheckboxGroup} this
\r
89 * @param {Array} checked An array containing the checked boxes.
\r
93 Ext.form.CheckboxGroup.superclass.initComponent.call(this);
\r
97 onRender : function(ct, position){
\r
101 cls: this.groupCls,
\r
105 bufferResize: false // Default this to false, since it doesn't really have a proper ownerCt.
\r
108 defaultType: this.defaultType,
\r
117 if(this.items[0].items){
\r
119 // The container has standard ColumnLayout configs, so pass them in directly
\r
121 Ext.apply(panelCfg, {
\r
122 layoutConfig: {columns: this.items.length},
\r
123 defaults: this.defaults,
\r
126 for(var i=0, len=this.items.length; i<len; i++){
\r
127 Ext.applyIf(this.items[i], colCfg);
\r
132 // The container has field item configs, so we have to generate the column
\r
133 // panels first then move the items into the columns as needed.
\r
135 var numCols, cols = [];
\r
137 if(typeof this.columns == 'string'){ // 'auto' so create a col per item
\r
138 this.columns = this.items.length;
\r
140 if(!Ext.isArray(this.columns)){
\r
142 for(var i=0; i<this.columns; i++){
\r
143 cs.push((100/this.columns)*.01); // distribute by even %
\r
148 numCols = this.columns.length;
\r
150 // Generate the column configs with the correct width setting
\r
151 for(var i=0; i<numCols; i++){
\r
152 var cc = Ext.apply({items:[]}, colCfg);
\r
153 cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
\r
155 cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
\r
160 // Distribute the original items into the columns
\r
162 var rows = Math.ceil(this.items.length / numCols), ri = 0;
\r
163 for(var i=0, len=this.items.length; i<len; i++){
\r
164 if(i>0 && i%rows==0){
\r
167 if(this.items[i].fieldLabel){
\r
168 this.items[i].hideLabel = false;
\r
170 cols[ri].items.push(this.items[i]);
\r
173 for(var i=0, len=this.items.length; i<len; i++){
\r
174 var ci = i % numCols;
\r
175 if(this.items[i].fieldLabel){
\r
176 this.items[i].hideLabel = false;
\r
178 cols[ci].items.push(this.items[i]);
\r
182 Ext.apply(panelCfg, {
\r
183 layoutConfig: {columns: numCols},
\r
188 this.panel = new Ext.Panel(panelCfg);
\r
189 this.panel.ownerCt = this;
\r
190 this.el = this.panel.getEl();
\r
192 if(this.forId && this.itemCls){
\r
193 var l = this.el.up(this.itemCls).child('label', true);
\r
195 l.setAttribute('htmlFor', this.forId);
\r
199 var fields = this.panel.findBy(function(c){
\r
200 return c.isFormField;
\r
203 this.items = new Ext.util.MixedCollection();
\r
204 this.items.addAll(fields);
\r
206 Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
\r
209 initValue : function(){
\r
211 this.setValue.apply(this, this.buffered ? this.value : [this.value]);
\r
212 delete this.buffered;
\r
217 afterRender : function(){
\r
218 Ext.form.CheckboxGroup.superclass.afterRender.call(this);
\r
219 this.eachItem(function(item){
\r
220 item.on('check', this.fireChecked, this);
\r
221 item.inGroup = true;
\r
226 doLayout: function(){
\r
227 //ugly method required to layout hidden items
\r
229 this.panel.forceLayout = this.ownerCt.forceLayout;
\r
230 this.panel.doLayout();
\r
235 fireChecked: function(){
\r
237 this.eachItem(function(item){
\r
242 this.fireEvent('change', this, arr);
\r
246 validateValue : function(value){
\r
247 if(!this.allowBlank){
\r
249 this.eachItem(function(f){
\r
251 return (blank = false);
\r
255 this.markInvalid(this.blankText);
\r
263 isDirty: function(){
\r
264 //override the behaviour to check sub items.
\r
265 if (this.disabled || !this.rendered) {
\r
270 this.eachItem(function(item){
\r
271 if(item.isDirty()){
\r
280 onDisable : function(){
\r
281 this.eachItem(function(item){
\r
287 onEnable : function(){
\r
288 this.eachItem(function(item){
\r
294 doLayout: function(){
\r
296 this.panel.forceLayout = this.ownerCt.forceLayout;
\r
297 this.panel.doLayout();
\r
302 onResize : function(w, h){
\r
303 this.panel.setSize(w, h);
\r
304 this.panel.doLayout();
\r
307 // inherit docs from Field
\r
308 reset : function(){
\r
309 Ext.form.CheckboxGroup.superclass.reset.call(this);
\r
310 this.eachItem(function(c){
\r
317 <div id="method-Ext.form.CheckboxGroup-setValue"></div>/**
\r
318 * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
\r
319 * in the group. Examples illustrating how this method may be called:
\r
321 // call with name and value
\r
322 myCheckboxGroup.setValue('cb-col-1', true);
\r
323 // call with an array of boolean values
\r
324 myCheckboxGroup.setValue([true, false, false]);
\r
325 // call with an object literal specifying item:value pairs
\r
326 myCheckboxGroup.setValue({
\r
330 // use comma separated string to set items with name to true (checked)
\r
331 myCheckboxGroup.setValue('cb-col-1,cb-col-3');
\r
333 * See {@link Ext.form.Checkbox#setValue} for additional information.
\r
334 * @param {Mixed} id The checkbox to check, or as described by example shown.
\r
335 * @param {Boolean} value (optional) The value to set the item.
\r
336 * @return {Ext.form.CheckboxGroup} this
\r
338 setValue: function(){
\r
340 this.onSetValue.apply(this, arguments);
\r
342 this.buffered = true;
\r
343 this.value = arguments;
\r
348 onSetValue: function(id, value){
\r
349 if(arguments.length == 1){
\r
350 if(Ext.isArray(id)){
\r
351 // an array of boolean values
\r
352 Ext.each(id, function(val, idx){
\r
353 var item = this.items.itemAt(idx);
\r
355 item.setValue(val);
\r
358 }else if(Ext.isObject(id)){
\r
359 // set of name/value pairs
\r
361 var f = this.getBox(i);
\r
367 this.setValueForItem(id);
\r
370 var f = this.getBox(id);
\r
378 onDestroy: function(){
\r
379 Ext.destroy(this.panel);
\r
380 Ext.form.CheckboxGroup.superclass.onDestroy.call(this);
\r
384 setValueForItem : function(val){
\r
385 val = String(val).split(',');
\r
386 this.eachItem(function(item){
\r
387 if(val.indexOf(item.inputValue)> -1){
\r
388 item.setValue(true);
\r
394 getBox : function(id){
\r
396 this.eachItem(function(f){
\r
397 if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
\r
405 <div id="method-Ext.form.CheckboxGroup-getValue"></div>/**
\r
406 * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
\r
407 * @return {Array} An array of the selected checkboxes.
\r
409 getValue : function(){
\r
411 this.eachItem(function(item){
\r
420 eachItem: function(fn){
\r
421 if(this.items && this.items.each){
\r
422 this.items.each(fn, this);
\r
426 <div id="cfg-Ext.form.CheckboxGroup-name"></div>/**
\r
427 * @cfg {String} name
\r
431 <div id="method-Ext.form.CheckboxGroup-getRawValue"></div>/**
\r
432 * @method getRawValue
\r
435 getRawValue : Ext.emptyFn,
\r
437 <div id="method-Ext.form.CheckboxGroup-setRawValue"></div>/**
\r
438 * @method setRawValue
\r
441 setRawValue : Ext.emptyFn
\r
445 Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);
\r