Browse Source

perf: 优化 vue2 代码生成模板,增加批量删除功能

puhui999 5 months ago
parent
commit
b76c39fa8d

+ 19 - 0
yudao-module-infra/src/main/resources/codegen/vue/api/api.js.vm

@@ -27,6 +27,16 @@ export function delete${simpleClassName}(id) {
   })
 }
 
+#if ( $table.templateType != 2 && $deleteBatchEnable)
+/** 批量删除${table.classComment} */
+export function delete${simpleClassName}List(ids) {
+  return request({
+    url: `${baseURL}/delete-list?ids=${ids.join(',')}`,
+    method: 'delete'
+  })
+}
+#end
+
 // 获得${table.classComment}
 export function get${simpleClassName}(id) {
   return request({
@@ -130,6 +140,15 @@ export function export${simpleClassName}Excel(params) {
       method: 'delete'
     })
   }
+  #if ($deleteBatchEnable)
+  /** 批量删除${subTable.classComment} */
+  export function delete${subSimpleClassName}List(ids) {
+    return request({
+      url: `${baseURL}/${subSimpleClassName_strikeCase}/delete-list?ids=${ids.join(',')}`,
+      method: 'delete'
+    })
+  }
+  #end
   // 获得${subTable.classComment}
   export function get${subSimpleClassName}(id) {
     return request({

+ 50 - 6
yudao-module-infra/src/main/resources/codegen/vue/views/components/list_sub_erp.vue.vm

@@ -13,10 +13,36 @@
         <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                    v-hasPermi="['${permissionPrefix}:create']">新增</el-button>
       </el-col>
+    #if ($deleteBatchEnable)
+      <el-col :span="1.5">
+        <el-button
+            type="danger"
+            plain
+            icon="el-icon-delete"
+            size="mini"
+            :disabled="isEmpty(checkedIds)"
+            @click="handleDeleteBatch"
+            v-hasPermi="['${permissionPrefix}:delete']"
+        >
+          批量删除
+        </el-button>
+      </el-col>
+    #end
     </el-row>
 #end
       ## 列表
-      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
+      <el-table
+          v-loading="loading"
+          :data="list"
+          :stripe="true"
+          :show-overflow-tooltip="true"
+          #if ($table.templateType == 11 && $deleteBatchEnable)
+          @selection-change="handleRowCheckboxChange"
+          #end
+      >
+          #if ($table.templateType == 11 && $deleteBatchEnable)
+            <el-table-column type="selection" width="55" />
+          #end
           #foreach($column in $subColumns)
               #if ($column.listOperationResult)
                   #set ($dictType=$column.dictType)
@@ -82,6 +108,9 @@
         // 列表的数据
         list: [],
 #if ($table.templateType == 11)
+        #if ($deleteBatchEnable)
+        checkedIds: [],
+        #end
         // 列表的总页数
         total: 0,
         // 查询参数
@@ -135,12 +164,27 @@
           this.loading = false;
         }
       },
-      /** 搜索按钮操作 */
-      handleQuery() {
-        this.queryParams.pageNo = 1;
-        this.getList();
-      },
+      #if ($table.templateType == 11 && $deleteBatchEnable)
+        /** 批量删除${table.classComment} */
+        async handleDeleteBatch() {
+          await this.#[[$modal]]#.confirm('是否确认删除?')
+          try {
+            await ${simpleClassName}Api.delete${subSimpleClassName}List(this.checkedIds);
+            await this.getList();
+            this.#[[$modal]]#.msgSuccess("删除成功");
+          } catch {}
+        },
+        handleRowCheckboxChange(records) {
+          this.checkedIds = records.map((item) => item.id);
+        },
+        #end
+
 #if ($table.templateType == 11)
+        /** 搜索按钮操作 */
+        handleQuery() {
+          this.queryParams.pageNo = 1;
+          this.getList();
+        },
       /** 添加/修改操作 */
       openForm(id) {
         if (!this.${subJoinColumn.javaField}) {

+ 54 - 9
yudao-module-infra/src/main/resources/codegen/vue/views/index.vue.vm

@@ -53,26 +53,45 @@
         <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                    v-hasPermi="['${permissionPrefix}:export']">导出</el-button>
       </el-col>
-        ## 特殊:树表专属逻辑
-        #if ( $table.templateType == 2 )
-          <el-col :span="1.5">
-            <el-button type="danger" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">
-              展开/折叠
-            </el-button>
-          </el-col>
-        #end
+    ## 特殊:树表专属逻辑
+    #if ( $table.templateType == 2 )
+      <el-col :span="1.5">
+        <el-button type="danger" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">
+          展开/折叠
+        </el-button>
+      </el-col>
+    #end
+    #if ($table.templateType != 2 && $deleteBatchEnable)
+      <el-col :span="1.5">
+        <el-button
+            type="danger"
+            plain
+            icon="el-icon-delete"
+            size="mini"
+            :disabled="isEmpty(checkedIds)"
+            @click="handleDeleteBatch"
+            v-hasPermi="['${permissionPrefix}:delete']"
+        >
+          批量删除
+        </el-button>
+      </el-col>
+    #end
       <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
     </el-row>
 
       ## 特殊:主子表专属逻辑
       #if ( $table.templateType == 11 && $subTables && $subTables.size() > 0 )
       <el-table
+        row-key="id"
         v-loading="loading"
         :data="list"
         :stripe="true"
         :highlight-current-row="true"
         :show-overflow-tooltip="true"
         @current-change="handleCurrentChange"
+        #if ($deleteBatchEnable)
+        @selection-change="handleRowCheckboxChange"
+        #end
       >
           ## 特殊:树表专属逻辑
       #elseif ( $table.templateType == 2 )
@@ -87,7 +106,18 @@
         :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
       >
       #else
-      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
+      <el-table
+          v-loading="loading"
+          :data="list"
+          :stripe="true"
+          :show-overflow-tooltip="true"
+          #if ($deleteBatchEnable)
+          @selection-change="handleRowCheckboxChange"
+          #end
+      >
+      #end
+      #if ($table.templateType != 2 && $deleteBatchEnable)
+        <el-table-column type="selection" width="55" />
       #end
       ## 特殊:主子表专属逻辑
       #if ( $table.templateType == 12 && $subTables && $subTables.size() > 0 )
@@ -229,6 +259,7 @@ export default {
       refreshTable: true,
       // 选中行
       currentRow: {},
+      checkedIds: [],
       // 查询参数
       queryParams: {
         ## 特殊:树表专属逻辑(树不需要分页接口)
@@ -301,6 +332,20 @@ export default {
        this.#[[$modal]]#.msgSuccess("删除成功");
       } catch {}
     },
+    #if ($table.templateType != 2 && $deleteBatchEnable)
+    /** 批量删除${table.classComment} */
+    async handleDeleteBatch() {
+      await this.#[[$modal]]#.confirm('是否确认删除?')
+      try {
+        await ${simpleClassName}Api.delete${simpleClassName}List(this.checkedIds);
+        await this.getList();
+        this.#[[$modal]]#.msgSuccess("删除成功");
+      } catch {}
+    },
+    handleRowCheckboxChange(records) {
+      this.checkedIds = records.map((item) => item.id);
+    },
+    #end
     /** 导出按钮操作 */
     async handleExport() {
       await this.#[[$modal]]#.confirm('是否确认导出所有${table.classComment}数据项?');