DirTree.vue 5.0 KB

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