classifyManage.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <script setup>
  2. import { reactive, ref, watch, computed, nextTick } from 'vue'
  3. import { Search } from '@element-plus/icons-vue'
  4. import { ElMessage, ElMessageBox } from 'element-plus'
  5. import {ClassifyInterface} from '@/api/modules/trainingApi'
  6. import _ from 'lodash'
  7. let searchText = ref('')
  8. let tableData = ref([])
  9. let tableLoading = ref(false)
  10. let defaultList = ref([{ClassifyId:-1,ClassifyName:'无'}])
  11. const rules = reactive({
  12. ParentId:[{required:true,message:'请选择上级分类'}],
  13. ClassifyName:[{required:true,message:'请输入分类名称'}]
  14. })
  15. const optionList = computed(()=>{
  16. const list = tableData.value.map(i=>{
  17. return {
  18. ClassifyId:i.ClassifyId,
  19. ClassifyName:i.ClassifyName
  20. }
  21. })
  22. return list
  23. })
  24. let isModifyDialogShow = ref(false)
  25. let formRef = ref(null)
  26. watch(isModifyDialogShow,(newVal)=>{
  27. if(newVal){
  28. nextTick(()=>{
  29. formRef.value?.clearValidate()
  30. })
  31. }
  32. })
  33. let currentClassify = ref({})
  34. function handleModifyClassify(data){
  35. currentClassify.value = _.cloneDeep(data)
  36. if(data.ParentId===0){
  37. currentClassify.value.ParentId = -1
  38. }
  39. isModifyDialogShow.value = true
  40. }
  41. //添加编辑分类
  42. async function modifyClassify(){
  43. try{
  44. await formRef.value?.validate()
  45. }catch(e){
  46. console.log(e)
  47. return
  48. }
  49. let res = null
  50. //params对ParentId为-1的数据做处理,转为0
  51. if(currentClassify.value.ClassifyId){
  52. res = await ClassifyInterface.editClassify({
  53. ClassifyId:currentClassify.value.ClassifyId,
  54. ParentId:currentClassify.value.ParentId===-1?0:currentClassify.value.ParentId,
  55. ClassifyName:currentClassify.value.ClassifyName
  56. })
  57. }else{
  58. res = await ClassifyInterface.addClassify({
  59. ParentId:currentClassify.value.ParentId===-1?0:currentClassify.value.ParentId,
  60. ClassifyName:currentClassify.value.ClassifyName
  61. })
  62. }
  63. if(res.Ret!==200) return
  64. ElMessage.success(`${currentClassify.value.ClassifyId?'编辑':'添加'}成功`)
  65. getTableData()
  66. isModifyDialogShow.value = false
  67. }
  68. function getTableData(){
  69. ClassifyInterface.getClassifyList({
  70. Keyword:searchText.value
  71. }).then(res=>{
  72. if(res.Ret!==200) return
  73. tableData.value = res.Data&&res.Data.List||[]
  74. })
  75. }
  76. getTableData()
  77. function deleteClassify(data){
  78. if(data.Children&&data.Children.length){
  79. ElMessageBox.confirm(
  80. '该分类下已关联内容,不可删除!',
  81. '提示',
  82. {
  83. confirmButtonText: '知道了',
  84. showCancelButton:false,
  85. type: 'error',
  86. }
  87. ).then(()=>{})
  88. }else{
  89. ElMessageBox.confirm(
  90. '删除后不可恢复,是否确认删除?',
  91. '提示',
  92. {
  93. confirmButtonText: '确定',
  94. cancelButtonText: '取消',
  95. type: 'warning',
  96. }
  97. ).then(()=>{
  98. ClassifyInterface.deleteClassify({
  99. ClassifyId:data.ClassifyId
  100. }).then(res=>{
  101. if(res.Ret!==200) return
  102. ElMessage.success('删除成功')
  103. getTableData()
  104. })
  105. })
  106. }
  107. }
  108. </script>
  109. <template>
  110. <div class="classify-manage-wrap traing-manage">
  111. <div class="top-wrap">
  112. <el-button type="primary" @click="handleModifyClassify({})">添加分类</el-button>
  113. <el-input v-model="searchText" clearable :prefix-icon="Search" placeholder="请输入分类名称"
  114. @input="getTableData" style="width:240px;"></el-input>
  115. </div>
  116. <div class="table-wrap">
  117. <el-table :data="tableData" v-loading="tableLoading" row-key="ClassifyId"
  118. :tree-props="{children:'Children',hasChildren:'hasChildren'}">
  119. <el-table-column prop="ClassifyName" label="一级分类" align="center">
  120. <template #default="{row}">
  121. <span>{{row.ParentId?'':row.ClassifyName}}</span>
  122. </template>
  123. </el-table-column>
  124. <el-table-column prop="ClassifyName" label="二级分类" align="center">
  125. <template #default="{row}">
  126. <span>{{row.ParentId?row.ClassifyName:''}}</span>
  127. </template>
  128. </el-table-column>
  129. <el-table-column label="操作" align="center" width="400px">
  130. <template #default="{row}">
  131. <el-link :underline="false" type="primary" style="margin-right: 20px;" @click="handleModifyClassify(row)">编辑</el-link>
  132. <el-link :underline="false" type="danger" @click="deleteClassify(row)">删除</el-link>
  133. </template>
  134. </el-table-column>
  135. </el-table>
  136. </div>
  137. <!-- 添加分类弹窗 -->
  138. <el-dialog :title="currentClassify.ClassifyId?'编辑分类':'添加分类'" v-model="isModifyDialogShow"
  139. :close-on-click-modal="false" :modal-append-to-body="false" @close="isModifyDialogShow=false" width="589px" center>
  140. <div class="dialog-container">
  141. <el-form :model="currentClassify" :rules="rules" ref="formRef" label-position="top">
  142. <el-form-item label="上级分类" prop="ParentId">
  143. <el-select v-model="currentClassify.ParentId" placeholder="请选择上级分类" style="width:100%;">
  144. <el-option v-for="item in currentClassify.ClassifyId
  145. ?(currentClassify.ParentId<=0?defaultList:optionList)
  146. :[...defaultList,...optionList]"
  147. :key="item.ClassifyId"
  148. :label="item.ClassifyName"
  149. :value="item.ClassifyId"/>
  150. </el-select>
  151. </el-form-item>
  152. <el-form-item label="分类名称" prop="ClassifyName">
  153. <el-input v-model="currentClassify.ClassifyName" placeholder="请输入分类名称" style="width:100%;"></el-input>
  154. </el-form-item>
  155. </el-form>
  156. </div>
  157. <div class="foot-container">
  158. <el-button @click="isModifyDialogShow=false">取 消</el-button>
  159. <el-button type="primary" @click="modifyClassify">确认</el-button>
  160. </div>
  161. </el-dialog>
  162. </div>
  163. </template>
  164. <style scoped lang="scss">
  165. @import "./css/manage.scss";
  166. </style>