Browse Source

修改选择模板的选中

yangg 4 months ago
parent
commit
d62fcf0a75

File diff suppressed because it is too large
+ 0 - 0
dist/index.html


File diff suppressed because it is too large
+ 0 - 0
dist/static/css/chunk-618ae9f3.c3ce1842.css


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/app.637f2107.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-618ae9f3.3d4052d4.js


File diff suppressed because it is too large
+ 0 - 0
dist/static/js/chunk-6debddaa.634e254f.js


+ 150 - 116
src/views/project/components/dataList.vue

@@ -193,13 +193,21 @@
         v-loading="templateLoading"
         v-loading="templateLoading"
         :data="templateList"
         :data="templateList"
         :max-height="600"
         :max-height="600"
+        :row-key="getRowKey"
         element-loading-text="加载中..."
         element-loading-text="加载中..."
         element-loading-spinner="el-icon-loading"
         element-loading-spinner="el-icon-loading"
         element-loading-background="rgba(255, 255, 255, 0.8)"
         element-loading-background="rgba(255, 255, 255, 0.8)"
         @selection-change="handleSelectionChange"
         @selection-change="handleSelectionChange"
         @select="handleSelect"
         @select="handleSelect"
+        @select-all="handleSelectAll"
+        @current-change="handleCurrentRowChange"
       >
       >
-        <el-table-column type="selection" width="55" />
+        <el-table-column 
+          type="selection" 
+          width="55"
+          :selectable="isSelectable"
+          :reserve-selection="true"
+        />
         <el-table-column prop="id" label="ID" width="80" align="center" />
         <el-table-column prop="id" label="ID" width="80" align="center" />
         <el-table-column prop="title" label="模版名称" align="left" />
         <el-table-column prop="title" label="模版名称" align="left" />
         <el-table-column prop="create_time" label="创建时间" align="center" />
         <el-table-column prop="create_time" label="创建时间" align="center" />
@@ -441,7 +449,7 @@ export default {
       customList: [], // 客户列表
       customList: [], // 客户列表
       productList: [], // 产品列表
       productList: [], // 产品列表
       currentPhaseList: [], // 阶段列表
       currentPhaseList: [], // 阶段列表
-      selectedTemplateIds: new Set(), // 存储所有选中的模板ID
+      selectedTemplateIds: new Set(), // 存储所有选中的ID
       selectedPageIds: new Map(), // 存储每页的选择状态
       selectedPageIds: new Map(), // 存储每页的选择状态
       isAllSelected: false, // 是否全选
       isAllSelected: false, // 是否全选
       selectionCache: new Map(), // 添加选择缓存
       selectionCache: new Map(), // 添加选择缓存
@@ -455,7 +463,8 @@ export default {
       documentSelectVisible: false, // 文档选择弹窗显示状态
       documentSelectVisible: false, // 文档选择弹窗显示状态
       documentVersions: [], // 文档版本列表
       documentVersions: [], // 文档版本列表
       documentVersionsLoading: false, // 加载状态
       documentVersionsLoading: false, // 加载状态
-      currentTemplate: null // 当前选中的模板
+      currentTemplate: null, // 当前选中的模板
+      confirmLoading: false, // 添加确认加载状态
     }
     }
   },
   },
   computed: {
   computed: {
@@ -2489,54 +2498,48 @@ export default {
       // 加载模板列表
       // 加载模板列表
       this.loadTemplateList()
       this.loadTemplateList()
     },
     },
+    /* 全选 */
+    handleSelectAll(selection) {
+      // selection 为当前选中的所有行数据
+    const currentPage = this.templateQueryForm.page
+      if (selection.length > 0) {
+      // 全选操作
+      this.selectedPageIds.set(currentPage, new Set(selection.map(row => row.id)))
+      // 更新所有选中项的ID集合
+      selection.forEach(row => {
+        this.selectedTemplateIds.add(row.id)
+      })
+    } else {
+      // 取消全选操作
+      const previousSelection = this.selectedPageIds.get(currentPage) || new Set()
+      previousSelection.forEach(id => {
+        this.selectedTemplateIds.delete(id)
+      })
+      this.selectedPageIds.delete(currentPage)
+    }
+    },
     // 处理模版分页变化
     // 处理模版分页变化
     async handleTemplatePageChange(page) {
     async handleTemplatePageChange(page) {
-      try {
-        // 保存当前页的选择状态
-        const currentPage = this.templateQueryForm.page
-        const currentSelection = this.$refs.templateTable?.selection || []
-
-        // 更新选择缓存
-        this.selectionCache.set(
-          currentPage,
-          new Set(currentSelection.map((row) => row.id))
-        )
-
-        // 更新全局选择状态
-        currentSelection.forEach((row) => {
-          this.selectedTemplateIds.add(row.id)
-        })
-
-        // 更新页码
-        this.templateQueryForm.page = page
-
-        // 加载新页面数据
-        await this.loadTemplateList()
-      } catch (error) {
-        console.error('页面切换失败:', error)
-        this.$message.error('页面切换失败')
-      }
+      this.templateQueryForm.page = page
+      this.loadTemplateList()
     },
     },
     // 加载模版表
     // 加载模版表
     async loadTemplateList() {
     async loadTemplateList() {
       this.templateLoading = true
       this.templateLoading = true
       try {
       try {
-        const res = await searchList({
-          ...this.templateQueryForm,
-          document_type: this.templateQueryForm.document_type || undefined // 只在有值时传递
-        })
-
+        const res = await searchTemplateList(this.templateQueryForm)
         if (res.status === 200) {
         if (res.status === 200) {
-          this.templateList = res.data.dataList
-          this.templateTotal = res.data.totalRecord
-          this.templateDialogVisible = true
-
-          await this.$nextTick()
-
-          if (this.$refs.templateTable) {
-            this.$refs.templateTable.clearSelection()
-            this.restoreSelectionState()
-          }
+          this.templateList = res.data.list
+          this.templateTotal = res.data.total
+          
+          // 在数据加载完成后恢复选择状态
+          this.$nextTick(() => {
+            this.templateList.forEach(row => {
+              if (this.selectedTemplateIds.has(row.id)) {
+                this.$refs.templateTable.toggleRowSelection(row, true)
+              }
+            })
+          })
         }
         }
       } catch (error) {
       } catch (error) {
         console.error('加载模板列表失败:', error)
         console.error('加载模板列表失败:', error)
@@ -2586,66 +2589,27 @@ export default {
 
 
     // 修改 handleSelectionChange 方法
     // 修改 handleSelectionChange 方法
     handleSelectionChange(selection) {
     handleSelectionChange(selection) {
-      if (!Array.isArray(selection)) {
-        console.warn('Invalid selection:', selection)
-        return
-      }
-
-      const currentPage = this.templateQueryForm.page
-
-      // 更新选择缓存
-      this.selectionCache.set(
-        currentPage,
-        new Set(selection.map((row) => row.id))
-      )
-
-      // 更新全局选择状态
-      const currentPageIds = new Set(this.templateList.map((row) => row.id))
-
-      // 移除当前页未选中的项
-      currentPageIds.forEach((id) => {
-        if (!selection.some((row) => row.id === id)) {
-          this.selectedTemplateIds.delete(id)
-        }
+      console.log(selection)
+      // 更新选中项ID集合
+      selection.forEach(item => {
+        this.selectedTemplateIds.add(item.id)
       })
       })
-
-      // 添加当前页选中的项
-      selection.forEach((row) => {
-        this.selectedTemplateIds.add(row.id)
-      })
-
-      // 更新全选状态
-      this.isAllSelected =
-        this.templateList.length > 0 &&
-        this.templateList.every((row) => this.selectedTemplateIds.has(row.id))
+      
+      // 更新已选择的模板数组
+      this.selectedTemplates = selection
     },
     },
 
 
     handleSelect(selection, row) {
     handleSelect(selection, row) {
-      if (!row || typeof row.id === 'undefined') {
-        console.warn('Invalid row data:', row)
-        return
-      }
-
-      const currentPage = this.templateQueryForm.page
-      const isSelected = selection.some((item) => item.id === row.id)
-
-      if (isSelected) {
+      if (selection.includes(row)) {
+        // 选中时添加ID
         this.selectedTemplateIds.add(row.id)
         this.selectedTemplateIds.add(row.id)
-        // 更新当前页选择记录
-        const currentPageSelected =
-          this.selectedPageIds.get(currentPage) || new Set()
-        currentPageSelected.add(row.id)
-        this.selectedPageIds.set(currentPage, currentPageSelected)
       } else {
       } else {
+        // 取消选中时移除ID
         this.selectedTemplateIds.delete(row.id)
         this.selectedTemplateIds.delete(row.id)
-        // 更新当前页选择记录
-        const currentPageSelected = this.selectedPageIds.get(currentPage)
-        if (currentPageSelected) {
-          currentPageSelected.delete(row.id)
-        }
-        // 取消全选状态
-        this.isAllSelected = false
       }
       }
+      
+      // 更新选中状态
+      this.selectedTemplates = selection
     },
     },
 
 
     // 修改加载方法
     // 修改加载方法
@@ -2770,38 +2734,73 @@ export default {
     },
     },
 
 
     // 确认选择
     // 确认选择
-    confirmTemplateSelection() {
-      if (this.selectedTemplateIds.size === 0) {
-        this.$message.warning('请至少选择一个模版')
-        return
-      }
+    async confirmTemplateSelection() {
+      try {
+        // 检查是否有选中的模板
+        if (this.selectedTemplateIds.size === 0) {
+          this.$message.warning('请至少选择一个模版')
+          return
+        }
 
 
-      // 转换Set为数组
-      const selectedIds = Array.from(this.selectedTemplateIds)
+        // 显示加载状态
+        this.confirmLoading = true
+
+        // 转换Set为数组
+        const selectedIds = Array.from(this.selectedTemplateIds)
+
+        // 调用关联接口
+        const res = await relate({
+          project_id: this.currentData.id,
+          doc_ids: JSON.stringify(selectedIds)
+        })
 
 
-      relate({
-        project_id: this.currentData.id,
-        doc_ids: JSON.stringify(selectedIds)
-      }).then((res) => {
         if (res.status === 200) {
         if (res.status === 200) {
+          // 更新成功
           this.$message.success('模版关联成功')
           this.$message.success('模版关联成功')
-          this.closeTemplateDialog()
+          
           // 更新当前数据的documentIds
           // 更新当前数据的documentIds
-          this.currentData.documentIds = selectedIds
-          this.search()
+          this.$set(this.currentData, 'documentIds', selectedIds)
+          
+          // 关闭对话框并清理选择状态
+          this.closeTemplateDialog()
+          
+          // 刷新列表
+          await this.search()
+          
+          // 触发自定义事件通知父组件
+          this.$emit('template-updated', {
+            projectId: this.currentData.id,
+            documentIds: selectedIds
+          })
+        } else {
+          this.$message.error(res.message || '模版关联失败')
         }
         }
-      })
+      } catch (error) {
+        console.error('模版关联失败:', error)
+        this.$message.error(error.response?.data?.message || '模版关联失败')
+      } finally {
+        this.confirmLoading = false
+      }
     },
     },
 
 
-    // 关对话框
+    // 修改对话框方法
     closeTemplateDialog() {
     closeTemplateDialog() {
-      this.templateDialogVisible = false
-      // 清除所有缓存
+      // 清理选择状态
       this.selectedTemplateIds.clear()
       this.selectedTemplateIds.clear()
-      this.selectedPageIds.clear()
-      this.selectionCache.clear()
+      this.selectedTemplates = []
+      this.currentPageSelected = false
       this.isAllSelected = false
       this.isAllSelected = false
-
+      
+      // 重置加载状态
+      this.confirmLoading = false
+      
+      // 关闭对话框
+      this.templateDialogVisible = false
+      
+      // 重置分页
+      this.templateQueryForm.page = 1
+      
+      // 清理表格选择状态
       if (this.$refs.templateTable) {
       if (this.$refs.templateTable) {
         this.$refs.templateTable.clearSelection()
         this.$refs.templateTable.clearSelection()
       }
       }
@@ -3625,6 +3624,41 @@ export default {
         this.$message.error(error.message || '导出失败')
         this.$message.error(error.message || '导出失败')
       }
       }
       this.documentSelectVisible = false
       this.documentSelectVisible = false
+    },
+
+    // 新增方法:处理选择变化
+    handleCurrentRowChange(selection) {
+      // 更新当前页的选择状态
+      const currentPage = this.templateQueryForm.page
+      this.selectedPageIds.set(currentPage, selection.map(item => item.id))
+      
+      // 更新所有选中项的集合
+      this.selectedTemplates = selection
+      
+      // 更新全局选中状态集合
+      this.selectedTemplateIds = new Set([
+        ...Array.from(this.selectedPageIds.values()).flat()
+      ])
+    },
+
+    // 新增方法:处理单行选择
+    isSelectable(row) {
+      // 这里可以添加行选择的条件判断
+      return true
+    },
+
+    // 新增方法:获取行的唯一标识
+    getRowKey(row) {
+      return row.id
+    },
+
+    // 新增:清除选择缓存方法
+    clearSelection() {
+      this.selectedTemplateIds.clear()
+      this.selectedTemplates = []
+      if (this.$refs.templateTable) {
+        this.$refs.templateTable.clearSelection()
+      }
     }
     }
   }
   }
 }
 }

Some files were not shown because too many files changed in this diff