allow string based values for comboboxarray
[roojs1] / Roo / form / ComboCheck.js
index cd718a1..a6b8aeb 100644 (file)
@@ -10,6 +10,9 @@
  * @class Roo.form.ComboCheck
  * @extends Roo.form.ComboBox
  * A combobox for multiple select items.
+ *
+ * FIXME - could do with a reset button..
+ * 
  * @constructor
  * Create a new ComboCheck
  * @param {Object} config Configuration options
@@ -27,9 +30,9 @@ Roo.form.ComboCheck = function(config){
         if ((typeof(_t[e]) == 'undefined' ) || !_t[e].length) {
             throw "Roo.form.ComboCheck : missing value for: " + e;
         }
-    })
+    });
+    
     
-   
 };
 
 Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
@@ -50,14 +53,11 @@ Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
 
             
             this.tpl =  new Roo.Template({
-                html :  '<div class="'+cls+'-item x-menu-check-item{' + this.valueField + ':this.checked}">' +
+                html :  '<div class="'+cls+'-item x-menu-check-item">' +
                    '<img class="x-menu-item-icon" style="margin: 0px;" src="' + Roo.BLANK_IMAGE_URL + '">' + 
                    '<span>{' + this.displayField + '}</span>' +
-                    '</div>',
-                checked: function (value, allValues) {
-                    var ar = _t.getValueArray();
-                    return ar.indexOf(value) > -1 ? ' x-menu-item-checked' : ''
-                }
+                    '</div>' 
+                
             });
         }
  
@@ -65,7 +65,15 @@ Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
         Roo.form.ComboCheck.superclass.onRender.call(this, ct, position);
         this.view.singleSelect = false;
         this.view.multiSelect = true;
-        this.view.onClick = function() {}
+        this.view.toggleSelect = true;
+        this.pageTb.add(new Roo.Toolbar.Fill(), {
+            
+            text: 'Done',
+            handler: function()
+            {
+                _t.collapse();
+            }
+        });
     },
     
     onViewOver : function(e, t){
@@ -76,52 +84,109 @@ Roo.extend(Roo.form.ComboCheck, Roo.form.ComboBox, {
     
     onViewClick : function(doFocus,index){
         return;
-        if(this.inKeyMode){ // prevent key nav and mouse over conflicts
-            return;
-        }
         
-        if (this.view.isSelected(index)) {
-            this.view.unselect(index);
-            return;
-        }
-        this.view.select(index, true);
+    },
+    select: function () {
+        //Roo.log("SELECT CALLED");
+    },
+     
+    selectByValue : function(xv, scrollIntoView){
+        var ar = this.getValueArray();
+        var sels = [];
         
-        return;
-        var index = this.view.getSelectedIndexes()[0];
-        var r = this.store.getAt(index);
-        if(r){
-            this.onSelect(r, index);
-        }
-        if(doFocus !== false && !this.blockFocus){
-            this.el.focus();
-        }
+        Roo.each(ar, function(v) {
+            if(v === undefined || v === null){
+                return;
+            }
+            var r = this.findRecord(this.valueField, v);
+            if(r){
+                sels.push(this.store.indexOf(r))
+                
+            }
+        },this);
+        this.view.select(sels);
+        return false;
     },
+    
+    
     
     onSelect : function(record, index){
-        if(this.fireEvent('beforeselect', this, record, index) !== false){
-            
-            
-            this.setFromData(index > -1 ? record.data : false);
-            
-            
-            
-            //this.collapse();
-            this.fireEvent('select', this, record, index);
+       // Roo.log("onselect Called");
+       // this is only called by the clear button now..
+        this.view.clearSelections();
+        this.setValue('[]');
+        if (this.value != this.valueBefore) {
+            this.fireEvent('change', this, this.value, this.valueBefore);
+            this.valueBefore = this.value;
         }
     },
     getValueArray : function()
     {
         var ar = [] ;
+        
         try {
-            var ret = Roo.decode(thist.getValue());
-            ret = typeof(ar) == 'Array' ? ar : []; //?? valid?
-            return ret;
+            //Roo.log(this.value);
+            if (typeof(this.value) == 'undefined') {
+                return [];
+            }
+            var ar = Roo.decode(this.value);
+            return  ar instanceof Array ? ar : []; //?? valid?
+            
         } catch(e) {
-            Roo.log("Roo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
+            Roo.log(e + "\nRoo.form.ComboCheck:getValueArray  invalid data:" + this.getValue());
             return [];
         }
+         
+    },
+    expand : function ()
+    {
         
+        Roo.form.ComboCheck.superclass.expand.call(this);
+        this.valueBefore = typeof(this.value) == 'undefined' ? '' : this.value;
+        //this.valueBefore = typeof(this.valueBefore) == 'undefined' ? '' : this.valueBefore;
         
-    } 
+
+    },
+    
+    collapse : function(){
+        Roo.form.ComboCheck.superclass.collapse.call(this);
+        var sl = this.view.getSelectedIndexes();
+        var st = this.store;
+        var nv = [];
+        var tv = [];
+        var r;
+        Roo.each(sl, function(i) {
+            r = st.getAt(i);
+            nv.push(r.get(this.valueField));
+        },this);
+        this.setValue(Roo.encode(nv));
+        if (this.value != this.valueBefore) {
+
+            this.fireEvent('change', this, this.value, this.valueBefore);
+            this.valueBefore = this.value;
+        }
+        
+    },
+    
+    setValue : function(v){
+        // Roo.log(v);
+        this.value = v;
+        
+        var vals = this.getValueArray();
+        var tv = [];
+        Roo.each(vals, function(k) {
+            var r = this.findRecord(this.valueField, k);
+            if(r){
+                tv.push(r.data[this.displayField]);
+            }else if(this.valueNotFoundText !== undefined){
+                tv.push( this.valueNotFoundText );
+            }
+        },this);
+       // Roo.log(tv);
+        
+        Roo.form.ComboBox.superclass.setValue.call(this, tv.join(', '));
+        this.hiddenField.value = v;
+        this.value = v;
+    }
+    
 });
\ No newline at end of file