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)
19 * @cfg {String} groupId the checkbox group id // normal just use for checkbox
22 * Create a new CheckBox
23 * @param {Object} config The config object
26 Roo.bootstrap.CheckBox = function(config){
27 Roo.bootstrap.CheckBox.superclass.constructor.call(this, config);
32 * Fires when the element is checked or unchecked.
33 * @param {Roo.bootstrap.CheckBox} this This input
34 * @param {Boolean} checked The new checked value
41 Roo.extend(Roo.bootstrap.CheckBox, Roo.bootstrap.Input, {
43 inputType: 'checkbox',
51 getAutoCreate : function()
53 var align = (!this.labelAlign) ? this.parentLabelAlign() : this.labelAlign;
59 cfg.cls = 'form-group ' + this.inputType; //input-group
62 cfg.cls += ' ' + this.inputType + '-inline';
68 type : this.inputType,
69 value : this.inputType == 'radio' ? this.inputValue : ((!this.checked) ? this.valueOff : this.inputValue),
70 cls : 'roo-' + this.inputType, //'form-box',
71 placeholder : this.placeholder || ''
75 if (this.weight) { // Validity check?
76 cfg.cls += " " + this.inputType + "-" + this.weight;
84 input.checked = this.checked;
88 input.name = this.name;
92 input.cls += ' input-' + this.size;
97 ['xs','sm','md','lg'].map(function(size){
99 cfg.cls += ' col-' + size + '-' + settings[size];
105 var inputblock = input;
110 if (this.before || this.after) {
120 cls : 'input-group-addon',
125 inputblock.cn.push(input);
130 cls : 'input-group-addon',
137 if (align ==='left' && this.fieldLabel.length) {
138 Roo.log("left and has label");
144 cls : 'control-label col-md-' + this.labelWidth,
145 html : this.fieldLabel
149 cls : "col-md-" + (12 - this.labelWidth),
156 } else if ( this.fieldLabel.length) {
161 tag: this.boxLabel ? 'span' : 'label',
163 cls: 'control-label box-input-label',
164 //cls : 'input-group-addon',
165 html : this.fieldLabel
175 Roo.log(" no label && no align");
176 cfg.cn = [ inputblock ] ;
183 //'for': id, // box label is handled by onclick - so no for...
189 boxLabelCfg.tooltip = this.tooltip;
192 cfg.cn.push(boxLabelCfg);
202 * return the real input element.
206 return this.el.select('input.roo-' + this.inputType,true).first();
211 return this.el.select('label.control-label',true).first();
217 return this.labelEl();
220 initEvents : function()
222 // Roo.bootstrap.CheckBox.superclass.initEvents.call(this);
225 Roo.bootstrap.CheckBox.register(this);
228 this.inputEl().on('click', this.onClick, this);
231 this.el.select('label.box-label',true).first().on('click', this.onClick, this);
234 this.startValue = this.getValue();
240 this.setChecked(!this.checked);
243 setChecked : function(state,suppressEvent)
245 this.startValue = this.getValue();
247 if(this.inputType == 'radio'){
249 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
250 e.dom.checked = false;
253 this.inputEl().dom.checked = true;
255 this.inputEl().dom.value = this.inputValue;
257 if(suppressEvent !== true){
258 this.fireEvent('check', this, true);
264 this.checked = state;
266 this.inputEl().dom.checked = state;
268 this.inputEl().dom.value = state ? this.inputValue : this.valueOff;
270 if(suppressEvent !== true){
271 this.fireEvent('check', this, state);
275 getValue : function()
277 if(this.inputType == 'radio'){
278 return this.getGroupValue();
281 return this.inputEl().getValue();
285 getGroupValue : function()
287 if(typeof(this.el.up('form').child('input[name='+this.name+']:checked', true)) == 'undefined'){
291 return this.el.up('form').child('input[name='+this.name+']:checked', true).value;
294 setValue : function(v,suppressEvent)
296 if(this.inputType == 'radio'){
297 this.setGroupValue(v, suppressEvent);
301 this.setChecked(((typeof(v) == 'undefined') ? this.checked : (String(v) === String(this.inputValue))), suppressEvent);
304 setGroupValue : function(v, suppressEvent)
306 this.startValue = this.getValue();
308 Roo.each(this.el.up('form').select('input[name='+this.name+']', true).elements, function(e){
309 e.dom.checked = false;
311 if(e.dom.value == v){
312 e.dom.checked = true;
316 if(suppressEvent !== true){
317 this.fireEvent('check', this, true);
323 validate : function()
327 (this.inputType == 'radio' && this.getValue().length) ||
328 (this.inputType == 'checkbox' && this.getValue() == this.inputValue)
338 Roo.apply(Roo.bootstrap.CheckBox, {
343 * register a CheckBox Group
344 * @param {Roo.bootstrap.CheckBox} the CheckBox to add
346 register : function(checkbox)
348 if(typeof(this.groups[checkbox.groupId]) == 'undefined'){
349 this.groups[checkbox.groupId] = {};
352 if(this.groups[checkbox.groupId].hasOwnProperty(checkbox.name)){
356 this.groups[checkbox.groupId][checkbox.name] = checkbox;
360 * fetch a CheckBox Group based on the group ID
361 * @param {string} the group ID
362 * @returns {Roo.bootstrap.CheckBox} the CheckBox group
364 get: function(groupId) {
365 if (typeof(this.groups[groupId]) == 'undefined') {
369 return this.groups[groupId] ;