Browse Source

增加了多选开放选项的情况

Mask96 5 years ago
parent
commit
dde1f7b84b
1 changed files with 34 additions and 15 deletions
  1. 34 15
      src/components/WebQn.vue

+ 34 - 15
src/components/WebQn.vue

@@ -118,8 +118,7 @@ export default {
       lastCheckList: [],
 
       // 选择题选中开放式选项的开放回答值
-      selectedOpenIndex: "",
-      selectedOpenValue: "",
+      selectedOpenList:[]
     };
   },
   mounted: function() {
@@ -269,8 +268,7 @@ export default {
     questionOptions() {
       console.log("call questionOptions");
       let opts = [];
-      let openValue = this.selectedOpenValue;
-      let openIndex = this.selectedOpenIndex;
+      let openList = this.selectedOpenList;
       if (this.upperDatasourceQuestion == 0) {
         opts = this.currentQuestion.options;
       } else {
@@ -315,9 +313,11 @@ export default {
       for (var index = 0; index < opts.length; index++) {
         // let label = `${i + 1}、${opts[i].optionContent}`;
         let label = `${opts[index].optionContent}`;
-        if (openIndex == index && opts[index].open && openValue) {
-          label = label + "(" + openValue + ")";
-        } 
+        for(var openIndex = 0; openIndex < openList.length; openIndex++){
+          if (openList[openIndex]&&openList[openIndex].selectedIndex == index && opts[index].open && openList[openIndex].selectedValue) {
+            label = label + "(" + openList[openIndex].selectedValue + ")";
+          } 
+        }
         let value = index.toString();
         Vue.set(opts[index], "label", label);
         Vue.set(opts[index], "value", value);
@@ -435,8 +435,22 @@ export default {
       let selectedOpt = null;
       if (_type == "object") {
         // 多选
-
         let isUnselect = this.lastCheckList.length > index.length;
+
+        if (isUnselect){
+          // 取消选中,先判断取消选中的是哪个index,然后在判断开放选项中是否有取消选择的,将其删除掉
+          let unselectItem;
+          for(var item in this.lastCheckList){
+            if (index.indexOf(this.lastCheckList[item])<0) {
+                unselectItem = this.lastCheckList[item]
+              }
+          }
+          for(var oIndex =0; oIndex<this.selectedOpenList.length; oIndex++){
+              if(this.selectedOpenList[oIndex].selectedIndex == unselectItem){
+                this.selectedOpenList.splice(oIndex,1);
+              }
+          }
+        }
         if (!isUnselect) {
           let lastIndex = index[index.length - 1];
           selectedOpt = this.questionOptions[lastIndex];
@@ -451,10 +465,10 @@ export default {
         }
       } else if (_type == "string") {
         selectedOpt = this.questionOptions[index];
+        this.selectedOpenList.splice(0,1);
       }
+
       // 处理开放选项
-      this.selectedOpenIndex = "";
-      this.selectedOpenValue = "";
       if (selectedOpt.open) {
         this.$prompt(selectedOpt.openOptionTips, "请填写", {
           confirmButtonText: "确定",
@@ -462,8 +476,10 @@ export default {
           customClass: "select-open-dialog",
         }).then(({ value }) => {
           console.log(`Dialog value: ${value}, `);
-          this.selectedOpenValue = value;
-          this.selectedOpenIndex = index;
+          this.$set(this.selectedOpenList, this.selectedOpenList.length ,{
+            selectedIndex : selectedOpt.value,
+            selectedValue : value
+          })
           this.answerNote[selectedOpt.id] = value;
         });
       }
@@ -547,10 +563,13 @@ export default {
           alert(`最少需选中${this.currentQuestion.minOptions}项`);
           return;
         }
-        if(this.selectedOpenIndex && !this.selectedOpenValue){
-          alert("请填写开放性选项答案")
-          return;
+        for(let index in this.selectedOpenList){
+          if(this.selectedOpenList[index].selectedIndex && !this.selectedOpenList[index].selectedValue ){
+            alert("请填写开放性选项答案")
+            return;
+          }
         }
+        this.selectedOpenList.length = 0;
 
         // 确定答案
         // this.answers[questionId] = selectionIds.join(',');