9 * @class Roo.bootstrap.CheckBox
10 * @extends Roo.bootstrap.Input
11 * Bootstrap CheckBox class
13 * @cfg {String} valueOff The value that should go into the generated input element's value when unchecked.
14 * @cfg {String} inputValue The value that should go into the generated input element's value when checked.
15 * @cfg {String} boxLabel The text that appears beside the checkbox
16 * @cfg {String} weight (primary|warning|info|danger|success) The text that appears beside the checkbox
17 * @cfg {Boolean} checked initnal the element
18 * @cfg {Boolean} inline inline the element (default false)
21 * Create a new CheckBox
22 * @param {Object} config The config object
25 Roo.bootstrap.CheckBox = function(config){
26 Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
31 * Fires when the element is checked or unchecked.
32 * @param {Roo.bootstrap.CheckBox} this This input
33 * @param {Boolean} checked The new checked value
34 * @param {String} oldValue The old value
40 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
42 inputType: 'checkbox',
50 getAutoCreate : function()
52 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
58 cfg.cls = 'form-group ' + this.inputType; //input-group
61 cfg.cls += ' ' + this.inputType + '-inline';
67 type : this.inputType,
68 value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
69 cls : 'roo-' + this.inputType, //'form-box',
70 placeholder : this.placeholder || ''
74 if (this.weight) { // Validity check?
75 cfg.cls += " " + this.inputType + "-" + this.weight;
83 input.checked = this.checked;
87 input.name = this.name;
91 input.cls += ' input-' + this.size;
96 ['xs','sm','md','lg'].map(function(size){
98 cfg.cls += ' col-' + size + '-' + settings[size];
104 var inputblock = input;
109 if (this.before || this.after) {
119 cls : 'input-group-addon',
124 inputblock.cn.push(input);
129 cls : 'input-group-addon',
136 if (align ==='left' && this.fieldLabel.length) {
137 Roo.log("left and has label");
143 cls : 'control-label col-md-' + this.labelWidth,
144 html : this.fieldLabel
148 cls : "col-md-" + (12 - this.labelWidth),
155 } else if ( this.fieldLabel.length) {
160 tag: this.boxLabel ? 'span' : 'label',
162 cls: 'control-label box-input-label',
163 //cls : 'input-group-addon',
164 html : this.fieldLabel
174 Roo.log(" no label && no align");
175 cfg.cn = [ inputblock ] ;
182 //'for': id, // box label is handled by onclick - so no for...
188 boxLabelCfg.tooltip = this.tooltip;
191 cfg.cn.push(boxLabelCfg);
201 * return the real input element.
205 return this.el.select('input.roo-' + this.inputType,true).first();
210 return this.el.select('label.control-label',true).first();
216 return this.labelEl();
219 initEvents : function()
221 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
223 this.inputEl().on('click', this.onClick, this);
225 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
232 this.setChecked(!this.checked);
235 setChecked : function(state,suppressEvent)
237 var old = this.inputEl().dom.checked;
239 if(this.inputType == 'radio'){
241 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
242 e.dom.checked = false;
245 this.inputEl().dom.checked = true;
247 this.inputEl().dom.value = this.inputValue;
249 if(suppressEvent !== true){
250 this.fireEvent('check', this, true, old);
256 this.checked = state;
258 this.inputEl().dom.checked = state;
260 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
262 if(suppressEvent !== true){
263 this.fireEvent('check', this, state, old);
267 getValue : function()
269 if(this.inputType == 'radio'){
270 return this.getGroupValue();
273 return this.inputEl().getValue();
277 getGroupValue : function()
279 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
283 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
286 setValue : function(v,suppressEvent)
288 if(this.inputType == 'radio'){
289 this.setGroupValue(v, suppressEvent);
293 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
296 setGroupValue : function(v, suppressEvent)
298 var old = this.inputEl().dom.checked;
300 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
301 e.dom.checked = false;
303 if(e.dom.value == v){
304 e.dom.checked = true;
308 if(suppressEvent !== true){
309 this.fireEvent('check', this, true, old);