ArtCatTree.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <route lang="yaml">
  2. meta:
  3. enabled: false
  4. </route>
  5. <script lang="ts" setup>
  6. import { ElMessage, ElMessageBox } from 'element-plus'
  7. import ArtCatComponent from './ArtCat.vue'
  8. import { deleteArtCatById, getArtCatByList } from '@/api/system/artCatApi'
  9. const emit = defineEmits(['onItemSelect'])
  10. interface TreeNode {
  11. label: string
  12. isLeaf: boolean
  13. artCatId: number
  14. children?: TreeNode[]
  15. }
  16. interface Object {
  17. artCatId: number
  18. artCatTitle: string
  19. artCatDesc: string
  20. }
  21. const state = reactive({
  22. loading: false,
  23. keywords: '',
  24. treeData: [] as TreeNode[],
  25. curArtCatId: 0,
  26. dataList: [] as Object[],
  27. dialogState: 1,
  28. search: {
  29. keywords: '',
  30. },
  31. })
  32. const leftTreeRef = ref()
  33. const getArtCatData = async () => {
  34. const { data } = await getArtCatByList(state.search)
  35. const children: TreeNode[] = []
  36. data.forEach((item: any) => {
  37. return children.push({
  38. label: item.artCatTitle,
  39. artCatId: item.artCatId,
  40. isLeaf: true,
  41. })
  42. })
  43. state.treeData = children
  44. if (data && data.length > 0) {
  45. state.curArtCatId = data[0].artCatId
  46. await nextTick(() => {
  47. leftTreeRef.value.setCurrentKey(state.curArtCatId)
  48. emit('onItemSelect', state.curArtCatId)
  49. })
  50. }
  51. }
  52. onMounted(() => {
  53. getArtCatData()
  54. })
  55. // 模型选择
  56. const onTreeNodeClick = (node: TreeNode) => {
  57. if (node.isLeaf) {
  58. state.curArtCatId = node.artCatId
  59. emit('onItemSelect', state.curArtCatId)
  60. }
  61. }
  62. const artCatRef = ref()
  63. // 新增
  64. const onArtCatAdd = () => {
  65. artCatRef.value.showAddModel(0)
  66. }
  67. // 添加子类
  68. const onSubArtCatAdd = (row: any) => {
  69. const artCatId = row.artCatId
  70. artCatRef.value.showAddModel(artCatId)
  71. }
  72. // 编辑
  73. const onArtCatEdit = (row: any) => {
  74. const artCatId = row.artCatId
  75. if (artCatId > 0) {
  76. artCatRef.value.showEditModel(artCatId)
  77. }
  78. }
  79. // 删除
  80. const onArtCatDelete = (row: any) => {
  81. const { artCatId, artCatTitle } = row
  82. ElMessageBox.confirm(`确认删除「${artCatTitle}」吗?`, '确认信息').then(async () => {
  83. const { msg } = await deleteArtCatById(artCatId)
  84. ElMessage.success(msg)
  85. await getArtCatData()
  86. })
  87. }
  88. </script>
  89. <template>
  90. <el-scrollbar class="tree">
  91. <el-button-group class="btns">
  92. <el-button type="primary" class="add" @click="onArtCatAdd()">
  93. <template #icon>
  94. <el-icon>
  95. <svg-icon name="i-ep:plus" />
  96. </el-icon>
  97. </template>
  98. 内容分类
  99. </el-button>
  100. <el-button @click="getArtCatData">
  101. <template #icon>
  102. <el-icon>
  103. <svg-icon name="i-ep:refresh" />
  104. </el-icon>
  105. </template>
  106. </el-button>
  107. </el-button-group>
  108. <ElTree ref="leftTreeRef" :data="state.treeData" default-expand-all node-key="wfDefId" @node-click="onTreeNodeClick">
  109. <template #default="{ node, data }">
  110. <div class="custom-tree-node">
  111. <div class="label" :title="node.label">
  112. <el-icon v-if="data.isLeaf" size="16px" style="margin-right: 5px;">
  113. <svg-icon name="ep:document-copy" />
  114. </el-icon>
  115. <div class="text">
  116. {{ data.label }}
  117. </div>
  118. </div>
  119. <div class="actions">
  120. <el-button-group>
  121. <el-button type="primary" plain size="small" @click.stop="onSubArtCatAdd(data)">
  122. <template #icon>
  123. <el-icon>
  124. <svg-icon name="i-ep:plus" />
  125. </el-icon>
  126. </template>
  127. </el-button>
  128. <el-button type="info" plain size="small" @click.stop="onArtCatEdit(data)">
  129. <template #icon>
  130. <el-icon>
  131. <svg-icon name="i-ep:edit" />
  132. </el-icon>
  133. </template>
  134. </el-button>
  135. <el-button type="danger" plain size="small" @click.stop="onArtCatDelete(data)">
  136. <template #icon>
  137. <el-icon>
  138. <svg-icon name="i-ep:delete" />
  139. </el-icon>
  140. </template>
  141. </el-button>
  142. </el-button-group>
  143. </div>
  144. </div>
  145. </template>
  146. </ElTree>
  147. <ArtCatComponent ref="artCatRef" @success="getArtCatData" />
  148. </el-scrollbar>
  149. </template>
  150. <style lang="scss" scoped>
  151. .btns {
  152. display: inline-flex;
  153. width: 100%;
  154. margin-bottom: var(--container-padding);
  155. .add {
  156. width: 100%;
  157. }
  158. }
  159. .tree {
  160. flex: 1;
  161. overflow-y: auto;
  162. :deep(.el-tree) {
  163. .el-tree-node__content {
  164. height: 40px;
  165. }
  166. .is-current > .el-tree-node__content {
  167. background-color: var(--el-color-primary-light-9);
  168. }
  169. .custom-tree-node {
  170. flex: 1;
  171. position: relative;
  172. width: 0;
  173. height: 100%;
  174. display: flex;
  175. flex-direction: column;
  176. justify-content: center;
  177. .label {
  178. display: inline-flex;
  179. align-items: center;
  180. width: calc(100% - 10px);
  181. color: var(--el-text-color-primary);
  182. .text {
  183. @include text-overflow;
  184. }
  185. }
  186. &:hover {
  187. .actions {
  188. display: block;
  189. }
  190. }
  191. .actions {
  192. display: none;
  193. position: absolute;
  194. right: 10px;
  195. top: 50%;
  196. transform: translateY(-50%);
  197. .el-button {
  198. padding: 5px 8px;
  199. }
  200. }
  201. }
  202. }
  203. }
  204. </style>