浏览代码

fix #468: can't reset `select()`

wangweimin 2 年之前
父节点
当前提交
8c02602cb6
共有 3 个文件被更改,包括 22 次插入4 次删除
  1. 8 2
      webiojs/src/handlers/input.ts
  2. 4 0
      webiojs/src/models/input/base.ts
  3. 10 2
      webiojs/src/models/input/select.ts

+ 8 - 2
webiojs/src/handlers/input.ts

@@ -214,14 +214,20 @@ class FormController {
             body.append(item.create_element());
             body.append(item.create_element());
         }
         }
 
 
+        element.on('reset', 'form', function (e) {
+            for (let name in that.name2input) {
+                that.name2input[name].on_reset(e);
+            }
+        });
+
         // submit event
         // submit event
         element.on('submit', 'form', function (e) {
         element.on('submit', 'form', function (e) {
             e.preventDefault(); // avoid to execute the actual submit of the form.
             e.preventDefault(); // avoid to execute the actual submit of the form.
 
 
             element.find('button').prop("disabled", true);
             element.find('button').prop("disabled", true);
 
 
-            for (let name in that.name2input){
-                if (!that.name2input[name].check_valid()){
+            for (let name in that.name2input) {
+                if (!that.name2input[name].check_valid()) {
                     element.find('button').prop("disabled", false);
                     element.find('button').prop("disabled", false);
                     return error_alert(t("error_in_input"));
                     return error_alert(t("error_in_input"));
                 }
                 }

+ 4 - 0
webiojs/src/models/input/base.ts

@@ -42,6 +42,10 @@ export class InputItem {
 
 
     }
     }
 
 
+    // invoked when the form is reset
+    on_reset(e: any) {
+    }
+
     /*
     /*
     * input_idx: 更新作用对象input标签的索引, -1 为不指定对象
     * input_idx: 更新作用对象input标签的索引, -1 为不指定对象
     * attributes:更新值字典
     * attributes:更新值字典

+ 10 - 2
webiojs/src/models/input/select.ts

@@ -39,13 +39,13 @@ export class Select extends InputItem {
         // @ts-ignore
         // @ts-ignore
         this.element.find('select').selectpicker();
         this.element.find('select').selectpicker();
 
 
-        if(spec.onblur) {
+        if (spec.onblur) {
             // blur事件时,发送当前值到服务器
             // blur事件时,发送当前值到服务器
             this.element.find('select').on("blur", (e) => {
             this.element.find('select').on("blur", (e) => {
                 this.on_input_event("blur", this);
                 this.on_input_event("blur", this);
             });
             });
         }
         }
-        if(spec.onchange){
+        if (spec.onchange) {
             this.element.find('select').on("change", (e) => {
             this.element.find('select').on("change", (e) => {
                 this.on_input_event("change", this);
                 this.on_input_event("change", this);
             });
             });
@@ -102,6 +102,14 @@ export class Select extends InputItem {
         this.update_input_helper(-1, attributes);
         this.update_input_helper(-1, attributes);
     }
     }
 
 
+    on_reset(e: any) {
+        // need to wait some time to get the select element be reset, and then update `selectpicker`
+        setTimeout(() => {
+            // @ts-ignore
+            this.element.find('select').selectpicker('render');
+        }, 100)
+    }
+
     get_value(): any {
     get_value(): any {
         let raw_val = this.element.find('select').val();
         let raw_val = this.element.find('select').val();
         if (this.spec.multiple) {
         if (this.spec.multiple) {