Commits

Anonymous committed 1c4abc4

preserving initially defined options in <select>

Comments (0)

Files changed (2)

htraf/examples20/index.html

 </head>
 <body>
     <h3>Select a School</h3>
-    <select id="school" multiple
-        data-htsql="/school{code, name}?exists(department)"></select>
+    <select id="school"
+        data-htsql="/school{code, name}?exists(department)">
+        <option></option>
+    </select>
     <div style="width: 500px; height: 350px;" 
         data-htsql="/program{title, count(student)}
                     ?school_code=$school&count(student)>0" 

htraf/js/htraf.widgets.js

 
 $.fn._valOrig = $.fn.val;
 $.fn.val = function(value) {
-    var origSelector = 'input,select,textarea';
+    var origSelector = 'input,textarea';
     if(value !== undefined)
         return this.each(function() {
             if($(this).is(origSelector) || !$(this).data('htraf'))
  
 // {{{ Widgets: htraf.select
 $.widget("htraf.select", $.htraf.Base, {
+    _create: function() {
+        $.htraf.Base.prototype._create.call(this);
+        this.keepOptions = this.element[0].options.length;
+    },
+
     clear: function() {
-        this.element[0].options.length = 0;
+        if(this.keepOptions)
+            this.element[0].selectedIndex = 0;
+        this.element[0].options.length = this.keepOptions || 0;
     },
 
     render: function() {
-        var select = this.element[0], data = this.data.data;
+        var select = this.element[0], 
+            data = this.data.data,
+            keep = this.keepOptions;
         for(var i = data.length - 1; i >= 0; i--) {
-            select.options[i] = new Option(data[i][1] || data[i][0], 
-                                           data[i][0] || '',
-                                           i == 0, i == 0); 
+            var row = data[i];
+            select.options[i + keep] = new Option(
+                row[1] === undefined  ? row[0]:row[1], 
+                row[0] === null ? row[0]:'',
+                i + keep == 0, i + keep == 0); 
+        }
+        for(var i = keep - 1; i >= 0; i--) {
+            var option = select.options[i],
+                value = option.value == '' ? null:option.value;
+            this.data.data.unshift([value, option.text]);
         }
         if(!data.length)
             this._trigger('empty');
         this._trigger('change');
+    },
+
+    _getRow: function() {
+        return this.data.data[this.element[0].selectedIndex] || null; 
+    },
+
+    setValue: function(value) {
+        value = value === null ? '':value + '';          
+        this.element._valOrig(value);
     }
 });
 // }}}