3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.CheckboxGroup"></div>/**
10 * @class Ext.form.CheckboxGroup
11 * @extends Ext.form.Field
12 * <p>A grouping container for {@link Ext.form.Checkbox} controls.</p>
13 * <p>Sample usage:</p>
15 var myCheckboxGroup = new Ext.form.CheckboxGroup({
17 xtype: 'checkboxgroup',
18 fieldLabel: 'Single Column',
19 itemCls: 'x-check-group-alt',
20 // Put all controls in a single column with width 100%
23 {boxLabel: 'Item 1', name: 'cb-col-1'},
24 {boxLabel: 'Item 2', name: 'cb-col-2', checked: true},
25 {boxLabel: 'Item 3', name: 'cb-col-3'}
30 * Creates a new CheckboxGroup
31 * @param {Object} config Configuration options
32 * @xtype checkboxgroup
34 Ext.form.CheckboxGroup = Ext.extend(Ext.form.Field, {
35 <div id="cfg-Ext.form.CheckboxGroup-items"></div>/**
36 * @cfg {Array} items An Array of {@link Ext.form.Checkbox Checkbox}es or Checkbox config objects
37 * to arrange in the group.
39 <div id="cfg-Ext.form.CheckboxGroup-columns"></div>/**
40 * @cfg {String/Number/Array} columns Specifies the number of columns to use when displaying grouped
41 * checkbox/radio controls using automatic layout. This config can take several types of values:
42 * <ul><li><b>'auto'</b> : <p class="sub-desc">The controls will be rendered one per column on one row and the width
43 * of each column will be evenly distributed based on the width of the overall field container. This is the default.</p></li>
44 * <li><b>Number</b> : <p class="sub-desc">If you specific a number (e.g., 3) that number of columns will be
45 * created and the contained controls will be automatically distributed based on the value of {@link #vertical}.</p></li>
46 * <li><b>Array</b> : Object<p class="sub-desc">You can also specify an array of column widths, mixing integer
47 * (fixed width) and float (percentage width) values as needed (e.g., [100, .25, .75]). Any integer values will
48 * be rendered first, then any float values will be calculated as a percentage of the remaining space. Float
49 * values do not have to add up to 1 (100%) although if you want the controls to take up the entire field
50 * container you should do so.</p></li></ul>
53 <div id="cfg-Ext.form.CheckboxGroup-vertical"></div>/**
54 * @cfg {Boolean} vertical True to distribute contained controls across columns, completely filling each column
55 * top to bottom before starting on the next column. The number of controls in each column will be automatically
56 * calculated to keep columns as even as possible. The default value is false, so that controls will be added
57 * to columns one at a time, completely filling each row left to right before starting on the next row.
60 <div id="cfg-Ext.form.CheckboxGroup-allowBlank"></div>/**
61 * @cfg {Boolean} allowBlank False to validate that at least one item in the group is checked (defaults to true).
62 * If no items are selected at validation time, {@link @blankText} will be used as the error text.
65 <div id="cfg-Ext.form.CheckboxGroup-blankText"></div>/**
66 * @cfg {String} blankText Error text to display if the {@link #allowBlank} validation fails (defaults to "You must
67 * select at least one item in this group")
69 blankText : "You must select at least one item in this group",
72 defaultType : 'checkbox',
75 groupCls : 'x-form-check-group',
78 initComponent: function(){
80 <div id="event-Ext.form.CheckboxGroup-change"></div>/**
82 * Fires when the state of a child checkbox changes.
83 * @param {Ext.form.CheckboxGroup} this
84 * @param {Array} checked An array containing the checked boxes.
88 this.on('change', this.validate, this);
89 Ext.form.CheckboxGroup.superclass.initComponent.call(this);
93 onRender : function(ct, position){
102 bufferResize: false // Default this to false, since it doesn't really have a proper ownerCt.
106 defaultType: this.defaultType,
114 if(this.items[0].items){
116 // The container has standard ColumnLayout configs, so pass them in directly
118 Ext.apply(panelCfg, {
119 layoutConfig: {columns: this.items.length},
120 defaults: this.defaults,
123 for(var i=0, len=this.items.length; i<len; i++){
124 Ext.applyIf(this.items[i], colCfg);
129 // The container has field item configs, so we have to generate the column
130 // panels first then move the items into the columns as needed.
132 var numCols, cols = [];
134 if(typeof this.columns == 'string'){ // 'auto' so create a col per item
135 this.columns = this.items.length;
137 if(!Ext.isArray(this.columns)){
139 for(var i=0; i<this.columns; i++){
140 cs.push((100/this.columns)*.01); // distribute by even %
145 numCols = this.columns.length;
147 // Generate the column configs with the correct width setting
148 for(var i=0; i<numCols; i++){
149 var cc = Ext.apply({items:[]}, colCfg);
150 cc[this.columns[i] <= 1 ? 'columnWidth' : 'width'] = this.columns[i];
152 cc.defaults = Ext.apply(cc.defaults || {}, this.defaults)
157 // Distribute the original items into the columns
159 var rows = Math.ceil(this.items.length / numCols), ri = 0;
160 for(var i=0, len=this.items.length; i<len; i++){
161 if(i>0 && i%rows==0){
164 if(this.items[i].fieldLabel){
165 this.items[i].hideLabel = false;
167 cols[ri].items.push(this.items[i]);
170 for(var i=0, len=this.items.length; i<len; i++){
171 var ci = i % numCols;
172 if(this.items[i].fieldLabel){
173 this.items[i].hideLabel = false;
175 cols[ci].items.push(this.items[i]);
179 Ext.apply(panelCfg, {
180 layoutConfig: {columns: numCols},
185 this.panel = new Ext.Container(panelCfg);
186 this.panel.ownerCt = this;
187 this.el = this.panel.getEl();
189 if(this.forId && this.itemCls){
190 var l = this.el.up(this.itemCls).child('label', true);
192 l.setAttribute('htmlFor', this.forId);
196 var fields = this.panel.findBy(function(c){
197 return c.isFormField;
200 this.items = new Ext.util.MixedCollection();
201 this.items.addAll(fields);
203 Ext.form.CheckboxGroup.superclass.onRender.call(this, ct, position);
206 initValue : function(){
208 this.setValue.apply(this, this.buffered ? this.value : [this.value]);
209 delete this.buffered;
214 afterRender : function(){
215 Ext.form.CheckboxGroup.superclass.afterRender.call(this);
216 this.eachItem(function(item){
217 item.on('check', this.fireChecked, this);
223 doLayout: function(){
224 //ugly method required to layout hidden items
226 this.panel.forceLayout = this.ownerCt.forceLayout;
227 this.panel.doLayout();
232 fireChecked: function(){
234 this.eachItem(function(item){
239 this.fireEvent('change', this, arr);
243 validateValue : function(value){
244 if(!this.allowBlank){
246 this.eachItem(function(f){
248 return (blank = false);
252 this.markInvalid(this.blankText);
261 //override the behaviour to check sub items.
262 if (this.disabled || !this.rendered) {
267 this.eachItem(function(item){
277 onDisable : function(){
278 this.eachItem(function(item){
284 onEnable : function(){
285 this.eachItem(function(item){
291 doLayout: function(){
293 this.panel.forceLayout = this.ownerCt.forceLayout;
294 this.panel.doLayout();
299 onResize : function(w, h){
300 this.panel.setSize(w, h);
301 this.panel.doLayout();
304 // inherit docs from Field
306 this.eachItem(function(c){
311 // Defer the clearInvalid so if BaseForm's collection is being iterated it will be called AFTER it is complete.
312 // Important because reset is being called on both the group and the individual items.
318 <div id="method-Ext.form.CheckboxGroup-setValue"></div>/**
319 * {@link Ext.form.Checkbox#setValue Set the value(s)} of an item or items
320 * in the group. Examples illustrating how this method may be called:
322 // call with name and value
323 myCheckboxGroup.setValue('cb-col-1', true);
324 // call with an array of boolean values
325 myCheckboxGroup.setValue([true, false, false]);
326 // call with an object literal specifying item:value pairs
327 myCheckboxGroup.setValue({
331 // use comma separated string to set items with name to true (checked)
332 myCheckboxGroup.setValue('cb-col-1,cb-col-3');
334 * See {@link Ext.form.Checkbox#setValue} for additional information.
335 * @param {Mixed} id The checkbox to check, or as described by example shown.
336 * @param {Boolean} value (optional) The value to set the item.
337 * @return {Ext.form.CheckboxGroup} this
339 setValue: function(){
341 this.onSetValue.apply(this, arguments);
343 this.buffered = true;
344 this.value = arguments;
349 onSetValue: function(id, value){
350 if(arguments.length == 1){
352 // an array of boolean values
353 Ext.each(id, function(val, idx){
354 var item = this.items.itemAt(idx);
359 }else if(Ext.isObject(id)){
360 // set of name/value pairs
362 var f = this.getBox(i);
368 this.setValueForItem(id);
371 var f = this.getBox(id);
379 beforeDestroy: function(){
380 Ext.destroy(this.panel);
381 Ext.form.CheckboxGroup.superclass.beforeDestroy.call(this);
385 setValueForItem : function(val){
386 val = String(val).split(',');
387 this.eachItem(function(item){
388 if(val.indexOf(item.inputValue)> -1){
395 getBox : function(id){
397 this.eachItem(function(f){
398 if(id == f || f.dataIndex == id || f.id == id || f.getName() == id){
406 <div id="method-Ext.form.CheckboxGroup-getValue"></div>/**
407 * Gets an array of the selected {@link Ext.form.Checkbox} in the group.
408 * @return {Array} An array of the selected checkboxes.
410 getValue : function(){
412 this.eachItem(function(item){
421 eachItem: function(fn){
422 if(this.items && this.items.each){
423 this.items.each(fn, this);
427 <div id="cfg-Ext.form.CheckboxGroup-name"></div>/**
432 <div id="method-Ext.form.CheckboxGroup-getRawValue"></div>/**
433 * @method getRawValue
436 getRawValue : Ext.emptyFn,
438 <div id="method-Ext.form.CheckboxGroup-setRawValue"></div>/**
439 * @method setRawValue
442 setRawValue : Ext.emptyFn
446 Ext.reg('checkboxgroup', Ext.form.CheckboxGroup);