Переглянути джерело

fix:优化题目列表选择框和卡券选择框公共组件及相关引用页面的逻辑

mnisting 4 місяців тому
батько
коміт
7c912f7c38

+ 84 - 32
src/components/QuestionSelect/index.vue

@@ -73,20 +73,33 @@
       ></right-toolbar>
     </el-row>
     <!-- 表格 -->
+    <!-- @selection-change="handleSelectionChange" -->
     <el-table
-      ref="multipleTable"
+      ref="tableRef"
+      :class="[selectMode]"
       v-loading="loading"
       :data="tableList"
-      row-key="id"
-      @selection-change="handleSelectionChange"
+      :row-key="(row) => row.id"
+      @select="handleSelect"
+      @select-all="handleSelectAll"
     >
-      <el-table-column type="selection" width="55" align="center" />
+      <el-table-column
+        type="selection"
+        width="55"
+        align="center"
+        reserve-selection
+      />
       <el-table-column label="问题类型" align="center" prop="type">
         <template slot-scope="scope">
           {{ scope.row.type === "1" ? "单选" : "多选" }}
         </template>
       </el-table-column>
-      <el-table-column label="问题描述" align="center" prop="descripiton" />
+      <el-table-column
+        label="问题描述"
+        align="center"
+        prop="descripiton"
+        show-overflow-tooltip
+      />
       <el-table-column label="创建时间" align="center" prop="createTime" />
       <el-table-column
         label="操作"
@@ -140,9 +153,15 @@ import AddAndEdit from "@/views/exerciseManage/question/components/addAndEdit.vu
 export default {
   components: { AddAndEdit },
   props: {
+    // 传过来的勾选的数据
     selectedIds: {
-      type: Array,
-      default: () => [],
+      type: String,
+      default: "",
+    },
+    // 勾选模式  single 单选  multiple 多选,默认多选
+    selectMode: {
+      type: String,
+      default: "multiple",
     },
   },
   data() {
@@ -166,7 +185,7 @@ export default {
       // 表格相关
       loading: false,
       tableList: [],
-      selectedRow: [],
+      curSelection: [], // 存储当前勾选的数据
       total: 0,
       //  弹窗相关
       title: "",
@@ -176,38 +195,41 @@ export default {
     };
   },
   created() {
-    this.getTableList();
+    this.resetQuery();
   },
   watch: {
+    // 传过来的勾选的数据
     selectedIds: {
       handler(newVal) {
-        // console.log(newVal);
-        if (newVal && newVal.length > 0) {
-          this.$nextTick(() => {
-            this.tableList.forEach((row) => {
-              if (newVal.includes(String(row.id))) {
-                // console.log(111);
-                this.$refs.multipleTable.toggleRowSelection(row, true);
-              }
-            });
-          });
+        // console.log("selectedIds", newVal);
+        if (newVal) {
+          let arr = newVal.split(",");
+          this.curSelection = arr.map((item) => ({ id: Number(item) }));
+        } else {
+          this.curSelection = [];
         }
       },
-      deep: true,
       immediate: true,
     },
   },
   methods: {
+    showSelect() {
+      this.$refs.tableRef.clearSelection();
+      this.tableList.forEach((row) => {
+        const isSelected = this.curSelection.some((item) => item.id === row.id);
+        this.$refs.tableRef.toggleRowSelection(row, isSelected);
+      });
+    },
     // 确认选择
     handleConfirm() {
-      this.$emit("confirm", this.selectedRow);
+      this.$emit("confirm", this.curSelection);
       this.handleCancel();
     },
     // 关闭弹窗
     handleCancel() {
       this.$emit("close");
       // 清空选择
-      this.$refs.multipleTable.clearSelection();
+      this.$refs.tableRef.clearSelection();
     },
     // 搜索
     handleQuery() {
@@ -226,8 +248,39 @@ export default {
       this.getTableList();
     },
     // 表格选中
-    handleSelectionChange(selection) {
-      this.selectedRow = selection;
+    handleSelect(selection, row) {
+      // console.log("选择数据", selection, row);
+      // 目前this.curSelection是所有传进来的选中项
+      // 判断this.curSelection中有没有该操作项row
+      const index = this.curSelection.findIndex((item) => item.id === row.id);
+      // 如果有的话说明是取消选中,没有则是新选中的
+      if (index === -1) {
+        if (this.selectMode === "single") {
+          this.curSelection = [row];
+          this.showSelect();
+        } else {
+          this.curSelection.push(row);
+        }
+      } else {
+        this.curSelection.splice(index, 1);
+      }
+    },
+    handleSelectAll(selection) {
+      // console.log("全选数据", selection);
+      if (selection.length) {
+        selection.forEach((row) => {
+          if (!this.curSelection.some((item) => item.id === row.id)) {
+            this.curSelection.push(row);
+          }
+        });
+      } else {
+        // 当前页面取消全选,获取当前页面全部数据的id
+        const currentPageIds = this.tableList.map((item) => item.id);
+        // console.log("currentPageIds", currentPageIds);
+        this.curSelection = this.curSelection.filter(
+          (item) => !currentPageIds.includes(item.id)
+        );
+      }
     },
     // 查看
     handleDetail(row) {
@@ -251,14 +304,7 @@ export default {
           if (res.code === 200) {
             this.tableList = res.rows;
             this.total = res.total;
-            this.$nextTick(() => {
-              this.tableList.forEach((row) => {
-                if (this.selectedIds.includes(String(row.id))) {
-                  // console.log(222);
-                  this.$refs.multipleTable.toggleRowSelection(row, true);
-                }
-              });
-            });
+            this.$nextTick(() => this.showSelect());
           }
         })
         .finally(() => (this.loading = false));
@@ -268,6 +314,12 @@ export default {
 </script>
 
 <style lang="scss" scoped>
+// 单选情况下不展示head的勾选框
+::v-deep .single.el-table {
+  thead .el-checkbox {
+    display: none;
+  }
+}
 .footer {
   text-align: right;
   margin-top: 40px;

+ 73 - 34
src/components/SelectCoupon/index.vue

@@ -74,14 +74,20 @@
     <!-- 表格 -->
     <!-- @selection-change="handleSelectionChange" -->
     <el-table
-      class="singleTable"
-      ref="singleTable"
+      :class="[selectMode]"
+      ref="tableRef"
       v-loading="loading"
       :data="tableList"
-      row-key="couponNumber"
+      :row-key="(row) => row.couponNumber"
       @select="handleSelect"
+      @select-all="handleSelectAll"
     >
-      <el-table-column type="selection" width="55" align="center" />
+      <el-table-column
+        type="selection"
+        width="55"
+        align="center"
+        reserve-selection
+      />
       <el-table-column label="卡券名称" align="center" prop="title" />
       <el-table-column label="类型" align="center" prop="type">
         <template slot-scope="scope">
@@ -159,6 +165,11 @@ export default {
       type: String,
       default: "",
     },
+    // 勾选模式  single 单选  multiple 多选,默认多选
+    selectMode: {
+      type: String,
+      default: "multiple",
+    },
   },
   data() {
     return {
@@ -169,7 +180,7 @@ export default {
         pageNum: 1,
         pageSize: 10,
         type: "",
-        descripiton: "",
+        title: "",
       },
       // 时间
       dateRange: [],
@@ -181,7 +192,7 @@ export default {
       // 表格相关
       loading: false,
       tableList: [],
-      selectedRow: [],
+      curSelection: [], // 存储当前勾选的数据
       total: 0,
       //  弹窗相关
       title: "",
@@ -190,39 +201,79 @@ export default {
       dlgLoading: false,
     };
   },
-  // created() {
-  //   this.getTableList();
-  // },
+  created() {
+    this.resetQuery();
+  },
   watch: {
+    // 传过来的勾选的数据
     selectedIds: {
       handler(newVal) {
-        console.log(newVal);
-        this.getTableList();
+        // console.log("selectedIds", newVal);
+        if (newVal) {
+          let arr = JSON.parse(newVal);
+          this.curSelection = [...arr];
+        } else {
+          this.curSelection = [];
+        }
       },
       immediate: true,
     },
   },
   methods: {
+    showSelect() {
+      this.$refs.tableRef.clearSelection();
+      this.tableList.forEach((row) => {
+        const isSelected = this.curSelection.some(
+          (item) => item.couponNumber === row.couponNumber
+        );
+        this.$refs.tableRef.toggleRowSelection(row, isSelected);
+      });
+    },
     // 用户勾选
     handleSelect(selection, row) {
-      console.log(111, selection, row);
-      if (selection.length > 1) {
-        // selection超过一个时说明之前已选中了一个,需要先清空选择再选中现在点击的
-        this.$refs.singleTable.clearSelection();
-        this.$refs.singleTable.toggleRowSelection(row, true);
+      // console.log("选择数据", selection, row);
+      // 目前this.curSelection是所有传进来的选中项
+      // 判断this.curSelection中有没有该操作项row
+      const index = this.curSelection.findIndex((item) => item.id === row.id);
+      // 如果有的话说明是取消选中,没有则是新选中的
+      if (index === -1) {
+        if (this.selectMode === "single") {
+          this.curSelection = [row];
+          this.showSelect();
+        } else {
+          this.curSelection.push(row);
+        }
+      } else {
+        this.curSelection.splice(index, 1);
+      }
+    },
+    handleSelectAll(selection) {
+      // console.log("全选数据", selection);
+      if (selection.length) {
+        selection.forEach((row) => {
+          if (!this.curSelection.some((item) => item.id === row.id)) {
+            this.curSelection.push(row);
+          }
+        });
+      } else {
+        // 当前页面取消全选,获取当前页面全部数据的id
+        const currentPageIds = this.tableList.map((item) => item.id);
+        // console.log("currentPageIds", currentPageIds);
+        this.curSelection = this.curSelection.filter(
+          (item) => !currentPageIds.includes(item.id)
+        );
       }
-      this.selectedRow = [row];
     },
     // 确认选择
     handleConfirm() {
-      this.$emit("confirm", this.selectedRow);
+      this.$emit("confirm", this.curSelection);
       this.handleCancel();
     },
     // 关闭弹窗
     handleCancel() {
       this.$emit("close");
       // 清空选择
-      // this.$refs.singleTable.clearSelection();
+      this.$refs.tableRef.clearSelection();
     },
     // 搜索
     handleQuery() {
@@ -236,15 +287,10 @@ export default {
         pageNum: 1,
         pageSize: 10,
         type: "",
-        descripiton: "",
+        title: "",
       };
       this.getTableList();
     },
-    // 表格选中
-    // handleSelectionChange(selection) {
-    //   console.log("表格选中值", selection);
-    //   this.selectedRow = selection;
-    // },
     // 查看
     handleDetail(row) {
       this.title = "查看";
@@ -267,14 +313,7 @@ export default {
           if (res.code === 200) {
             this.tableList = res.rows;
             this.total = res.total;
-            this.$nextTick(() => {
-              this.tableList.forEach((row) => {
-                if (this.selectedIds === row.couponNumber) {
-                  // console.log(222);
-                  this.$refs.singleTable.toggleRowSelection(row, true);
-                }
-              });
-            });
+            this.$nextTick(() => this.showSelect());
           }
         })
         .finally(() => (this.loading = false));
@@ -284,7 +323,7 @@ export default {
 </script>
 
 <style lang="scss" scoped>
-::v-deep .singleTable {
+::v-deep .single.el-table {
   thead .el-checkbox {
     display: none;
   }

+ 23 - 6
src/views/coupon/cardPsdManage/components/addAndEdit.vue

@@ -57,6 +57,8 @@
     <!-- 选择卡券弹窗 -->
     <el-dialog title="选择卡券" :visible.sync="dlgVisible" append-to-body>
       <SelectCoupon
+        ref="couponRef"
+        selectMode="single"
         :selectedIds="selectedIds"
         @close="handleClose"
         @confirm="handleSelect"
@@ -105,6 +107,9 @@ export default {
         expirationDate: [
           { required: true, trigger: "blur", message: "请选择到期时间" },
         ],
+        couponId: [
+          { required: true, trigger: "blur", message: "请选择所属卡券" },
+        ],
       },
       pickerOptions: {
         disabledDate(time) {
@@ -167,20 +172,32 @@ export default {
     // 确认选择卡券
     handleSelect(val) {
       console.log(val);
-      this.form.coupon = `${val[0].title}(${val[0].couponNumber})`;
-      this.form.couponId = val[0].id;
-      this.form.couponNumber = val[0].couponNumber;
-      this.form.couponTitle = val[0].title;
+      if (val[0]) {
+        this.form.coupon = `${val[0].title}(${val[0].couponNumber})`;
+        this.form.couponId = val[0].id;
+        this.form.couponNumber = val[0].couponNumber;
+        this.form.couponTitle = val[0].title;
+      } else {
+        this.form.coupon = "";
+        this.form.couponId = "";
+        this.form.couponNumber = "";
+        this.form.couponTitle = "";
+      }
     },
     // 选择卡券
     selectCoupon() {
       this.dlgVisible = true;
-      this.selectedIds = this.form.couponNumber;
+      this.$refs.couponRef && this.$refs.couponRef.resetQuery();
+      let obj = {
+        title: this.form.couponTitle,
+        id: this.form.couponId,
+        couponNumber: this.form.couponNumber,
+      };
+      this.selectedIds = JSON.stringify([obj]);
     },
     // 关闭卡券弹窗
     handleClose() {
       this.dlgVisible = false;
-      this.selectedIds = "";
     },
     // 生成卡号
     createCardNumber() {

+ 1 - 1
src/views/exerciseManage/question/index.vue

@@ -115,7 +115,7 @@
           {{ scope.row.type === "1" ? "单选" : "多选" }}
         </template>
       </el-table-column>
-      <el-table-column label="问题描述" align="center" prop="descripiton" />
+      <el-table-column label="问题描述" align="center" prop="descripiton" show-overflow-tooltip />
       <el-table-column label="创建时间" align="center" prop="createTime" />
       <el-table-column
         label="操作"

+ 4 - 6
src/views/exerciseManage/specialManage/components/addAndEdit.vue

@@ -48,7 +48,8 @@
     <!-- 题目列表(可选) -->
     <el-dialog title="选择题目" :visible.sync="dlgVisible" append-to-body>
       <QuestionSelect
-        :selectedIds="selectedIdArr"
+        ref="questionRef"
+        :selectedIds="selectedIds"
         @close="handleClose"
         @confirm="confirmQuestion"
       />
@@ -93,7 +94,6 @@ export default {
       // 题目列表弹窗相关参数
       dlgVisible: false,
       selectedVisible: false,
-      selectedIdArr: [],
       selectedIds: "",
       // 已选题目的条数
       selectNumber: 0,
@@ -143,13 +143,11 @@ export default {
     selectQuestion() {
       if (this.dlgType !== 3) {
         this.dlgVisible = true;
-        this.selectedIdArr = this.form.questionIds
-          ? this.form.questionIds.split(",")
-          : [];
+        this.$refs.questionRef && this.$refs.questionRef.resetQuery();
       } else {
         this.selectedVisible = true;
-        this.selectedIds = this.form.questionIds;
       }
+      this.selectedIds = this.form.questionIds;
     },
     // 确认添加题目
     confirmQuestion(val) {