Sfoglia il codice sorgente

feat: `input_update()` support update `value` of select, radio and checkbox

wangweimin 4 anni fa
parent
commit
1fd2dbfa57

+ 5 - 3
pywebio/input.py

@@ -55,6 +55,9 @@ Functions list
    * - `input_group <pywebio.input.input_group>`
      - Input group
 
+   * - `input_update <pywebio.input.input_update>`
+     - Update input item
+
 
 Functions doc
 --------------
@@ -84,7 +87,7 @@ SELECT = 'select'
 TEXTAREA = 'textarea'
 
 __all__ = ['TEXT', 'NUMBER', 'FLOAT', 'PASSWORD', 'URL', 'DATE', 'TIME', 'input', 'textarea', 'select',
-           'checkbox', 'radio', 'actions', 'file_upload', 'input_group']
+           'checkbox', 'radio', 'actions', 'file_upload', 'input_group', 'input_update']
 
 
 def _parse_args(kwargs, excludes=()):
@@ -670,8 +673,7 @@ def input_update(name=None, **spec):
        Optional, default is the name of input field which triggers ``onchange``
     :param spec: The input parameters need to be updated.
        Note that those parameters can not be updated:
-       ``type``, ``name``, ``validate``, ``action``, ``code``, ``onchange``, ``multiple`` in all input,
-       and ``value`` in select, radio and checkbox.
+       ``type``, ``name``, ``validate``, ``action``, ``code``, ``onchange``, ``multiple``
 
     An example of implementing dependent input items in an input group:
 

+ 15 - 1
webiojs/src/models/input/checkbox_radio.ts

@@ -84,6 +84,21 @@ export class CheckboxRadio extends InputItem {
             });
         }
 
+        if ('value' in attributes) {
+            this.element.find('input:checked').prop('checked', false);
+            let values: any[] = attributes.value;
+            if (this.spec.type === 'radio') {
+                values = [attributes.value];
+            }
+            this.element.find('input').each(function (index) {
+                let item_val = JSON.parse($(this).val() as string);
+                if (values.indexOf(item_val) != -1) {
+                    $(this).prop('checked', true);
+                }
+            });
+            delete attributes['value'];
+        }
+
         if ('options' in attributes) {
             this.spec.options = attributes.options;
             let spec = this.setup_spec();
@@ -115,7 +130,6 @@ export class CheckboxRadio extends InputItem {
             $.each(value_arr, function (idx, val) {
                 if (val.name === that.spec.name)
                     res.push(JSON.parse(val.value as string));
-                console.log(JSON.parse(val.value as string), typeof JSON.parse(val.value as string));
             });
             return res;
         }

+ 15 - 0
webiojs/src/models/input/select.ts

@@ -81,6 +81,21 @@ export class Select extends InputItem {
             delete attributes['options'];
         }
 
+        if ('value' in attributes) {
+            this.element.find('option').prop('selected', false);
+            let values: any[] = attributes.value;
+            if (!this.spec.multiple) {
+                values = [attributes.value];
+            }
+            this.element.find('option').each(function (index) {
+                let item_val = JSON.parse($(this).val() as string);
+                if (values.indexOf(item_val) != -1) {
+                    $(this).prop('selected', true);
+                }
+            });
+            delete attributes['value'];
+        }
+
         this.update_input_helper(-1, attributes);
     }