TreeList.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <template>
  2. <!-- -->
  3. <!-- element-loading-text="拼命加载中……" -->
  4. <el-tree
  5. v-loading="props.loading"
  6. element-loading-background="#ffffff"
  7. element-loading-custom-class="loading_icon"
  8. :element-loading-spinner="elIconLoading"
  9. style="max-width: 600px"
  10. :data="filterTreeData"
  11. :props="defaultProps"
  12. :node-key="props.nodeKey"
  13. :expand-on-click-node="false"
  14. @node-click="handleNodeClick"
  15. :show-checkbox="props.showCheckBox"
  16. @check="HandleChecked"
  17. :check-on-click-leaf="false"
  18. :default-expand-all="defaultExpandAll"
  19. :default-checked-keys="props.defaultCheckedKeys"
  20. ref="treeRef"
  21. >
  22. <template #default="{ node, data }">
  23. <div class="custom-tree-node">
  24. <template v-if="editItemKey == data.id">
  25. <el-input @click="$event.stopPropagation()" class="edit_input" v-model="editTaskName" />
  26. </template>
  27. <span v-else>{{ node.label }}</span>
  28. <div class="operation">
  29. <!-- 展示操作栏 -->
  30. <template v-if="props.operation == 'all' &&
  31. (!props.operationFlagKey || props.operationFlagKey && data?.[props.operationFlagKey])">
  32. <!-- 展示修改确认操作 -->
  33. <template v-if="editItemKey == data.id">
  34. <el-icon style="margin-left: 5px;" @click="HandleEditConfirm($event,node,data)"><Check /></el-icon>
  35. </template>
  36. <!-- 展示 ... 和 新增 -->
  37. <template v-else>
  38. <el-icon v-if="(node?.level < props.maxLevel) && props.showAddOperation"
  39. @click="HandleAddSubItem($event,node,data)"
  40. style="color:#666666"><Plus />
  41. </el-icon>
  42. <el-popover
  43. :visible="popVisible && popOperationKey==data.id"
  44. placement="bottom"
  45. popper-class="pop_operation"
  46. style="min-width: auto !important;width: auto !important;"
  47. >
  48. <span @click="HandleRenameSubItm(node,data)">重命名</span>
  49. <span class="delete" @click="HandleDeleteSubItm(node,data)">删除</span>
  50. <template #reference>
  51. <el-icon style="transform: rotate(90deg);color:#666666;"
  52. @click="OpenMorePop($event,node, data)"><MoreFilled /></el-icon>
  53. </template>
  54. </el-popover>
  55. </template>
  56. </template>
  57. <!-- 展示关闭操作 -->
  58. <el-icon v-else-if="props.operation == 'close' &&
  59. (!props.operationFlagKey || props.operationFlagKey && data?.[props.operationFlagKey])"
  60. style="color:#666666"
  61. @click="HandleDelete(node, data)"><CircleClose /></el-icon>
  62. </div>
  63. </div>
  64. </template>
  65. </el-tree>
  66. </template>
  67. <script setup lang="ts">
  68. import { computed, ref} from 'vue'
  69. import {
  70. extractArrayObjectNode,deepSort,addNodeKeyValOfArrayObject
  71. } from '@/utils/commonFunctions.ts'
  72. const props = defineProps({
  73. defaultCheckedKeys:{
  74. type:Array,
  75. },
  76. loading:{
  77. type:Boolean,
  78. default:false
  79. },
  80. defaultTreeData:{
  81. type:Array<{
  82. label:String,
  83. id?:[String,null,number],
  84. children?:[] | null
  85. }>,
  86. },
  87. defaultExpandAll:{
  88. type:Boolean,
  89. default:false
  90. },
  91. showCheckBox:{
  92. type:Boolean,
  93. default:false
  94. },
  95. operation:{
  96. type:String, // 'all'|'close'|'none'
  97. default:'all'
  98. },
  99. showAddOperation:{
  100. type:Boolean,
  101. default:true,
  102. },
  103. operationFlagKey:{ //配合 operation 使用
  104. type:String, // 控制 操作显隐的键 的 变量名
  105. // 键的值 0 --无操作, 1 有操作
  106. },
  107. maxLevel:{
  108. type:Number,
  109. default:2
  110. },
  111. nodeKey:{
  112. type:String,
  113. default:'id'
  114. },
  115. sortMsg:{
  116. type:{
  117. sortKey:String,
  118. childrenKey:String
  119. },
  120. default:()=>{
  121. return {
  122. sortKey:'code',
  123. childrenKey:'children'
  124. }
  125. }
  126. },
  127. nodeDisabled:{
  128. type:Boolean,
  129. default:false
  130. }
  131. })
  132. const emit = defineEmits([
  133. 'openAddSubItmForm','updateTreeName','deleteTreeNode',
  134. 'changeCurrentSubTreeKey','updateSelectedTreeNode'
  135. ])
  136. const filterTreeData = computed(()=>{
  137. if(props.nodeDisabled){
  138. // disabled:true
  139. // 给节点添加 属性
  140. let list = JSON.parse(JSON.stringify(props.defaultTreeData))
  141. addNodeKeyValOfArrayObject(list,'disabled',props.nodeDisabled)
  142. return list
  143. }else{
  144. return props.defaultTreeData
  145. }
  146. })
  147. const elIconLoading = ref(`
  148. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024" ><path fill="currentColor" d="M512 64a32 32 0 0 1 32 32v192a32 32 0 0 1-64 0V96a32 32 0 0 1 32-32m0 640a32 32 0 0 1 32 32v192a32 32 0 1 1-64 0V736a32 32 0 0 1 32-32m448-192a32 32 0 0 1-32 32H736a32 32 0 1 1 0-64h192a32 32 0 0 1 32 32m-640 0a32 32 0 0 1-32 32H96a32 32 0 0 1 0-64h192a32 32 0 0 1 32 32M195.2 195.2a32 32 0 0 1 45.248 0L376.32 331.008a32 32 0 0 1-45.248 45.248L195.2 240.448a32 32 0 0 1 0-45.248m452.544 452.544a32 32 0 0 1 45.248 0L828.8 783.552a32 32 0 0 1-45.248 45.248L647.744 692.992a32 32 0 0 1 0-45.248M828.8 195.264a32 32 0 0 1 0 45.184L692.992 376.32a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0m-452.544 452.48a32 32 0 0 1 0 45.248L240.448 828.8a32 32 0 0 1-45.248-45.248l135.808-135.808a32 32 0 0 1 45.248 0"></path></svg>`)
  149. const defaultProps = ref({
  150. children: 'children',
  151. label: 'label',
  152. class:'custom_tree'
  153. })
  154. const editItemKey = defineModel('editItemKey') // 修改操作 选中的树节点key
  155. const editTaskName = defineModel('editTaskName')
  156. const popVisible = ref(false)
  157. const popOperationKey = ref() //更多操作 选中的树节点key
  158. const treeRef = defineModel('treeRef')
  159. const currentSelectedSubTarget = ref<any[]>([]) //已选指标
  160. const HandleEditConfirm = (e,node,data)=>{
  161. e.stopPropagation()
  162. // 调用接口传递修改的值
  163. editItemKey.value = ''
  164. emit('updateTreeName',{label:editTaskName.value,id:data.id})
  165. }
  166. const HandleChecked = (aimNode,selectedStatus)=>{
  167. console.log('点击节点复选框后触发的值',selectedStatus)
  168. currentSelectedSubTarget.value = extractArrayObjectNode(props.defaultTreeData,props.nodeKey,selectedStatus.checkedKeys)
  169. console.log('执行结果',currentSelectedSubTarget.value)
  170. // 递归排序
  171. currentSelectedSubTarget.value = deepSort(currentSelectedSubTarget.value,props.sortMsg.sortKey,props.sortMsg.childrenKey)
  172. // 这个数据在传递之前要进行过滤排序
  173. emit('updateSelectedTreeNode',currentSelectedSubTarget.value)
  174. }
  175. const handleNodeClick = (data,node) => {
  176. console.log('节点点击了',data)
  177. console.log('节点点击了node',node)
  178. emit('changeCurrentSubTreeKey',{data,node})
  179. }
  180. const HandleAddSubItem = ( e, node, data)=>{
  181. console.log('当前节点',node)
  182. console.log('当前数据',data)
  183. e.stopPropagation()
  184. emit('openAddSubItmForm', {data,node})
  185. }
  186. const HandleRenameSubItm = (node,data)=>{
  187. console.log('修改的节点')
  188. popVisible.value = false
  189. popOperationKey.value = ''
  190. editItemKey.value = data.id
  191. editTaskName.value = node.label
  192. }
  193. const HandleDeleteSubItm = (node,data)=>{
  194. console.log('删除子元素',node,data)
  195. emit('deleteTreeNode',{node,id:data.id})
  196. popVisible.value = false
  197. }
  198. const OpenMorePop = (e,node,data)=>{
  199. console.log('打开更多pop',node,data)
  200. e.stopPropagation()
  201. popVisible.value = !popVisible.value
  202. popOperationKey.value = data.id
  203. }
  204. const HandleDelete = (node,data)=>{
  205. console.log('删除对应类型下的指标',node,data)
  206. emit('deleteTreeNode',{node,id:data.id})
  207. }
  208. </script>
  209. <style lang="scss" scoped>
  210. .custom-tree-node {
  211. // border:solid red 1px;
  212. flex: 1;
  213. display: flex;
  214. align-items: center;
  215. justify-content: space-between;
  216. font-size: 14px;
  217. padding-right: 12px;
  218. line-height: 1;
  219. .edit_input{
  220. flex:1 0;
  221. :deep(.el-input__wrapper){
  222. height: 28px;
  223. }
  224. }
  225. .operation{
  226. display: flex;
  227. gap:10px;
  228. }
  229. }
  230. :deep(.loading_icon){
  231. .el-loading-spinner{
  232. color:#2e64fa;
  233. .el-loading-text{
  234. color:#2e64fa;
  235. }
  236. }
  237. }
  238. </style>
  239. <style lang="scss">
  240. .custom_tree > .el-tree-node__content{
  241. // border:solid red 1px;
  242. height: 28px !important;
  243. }
  244. .custom_tree.is-current > .el-tree-node__content{
  245. background-color: #E3EEFF !important;
  246. color:#2E64FA;
  247. }
  248. .pop_operation.el-popper.is-light{
  249. width:auto !important;
  250. min-width: auto !important;
  251. padding:6px;
  252. font-size: 12px ;
  253. display: flex;
  254. flex-direction: column;
  255. gap: 10px;
  256. .delete{
  257. color:#F56C6C;
  258. }
  259. >span{
  260. &:hover{
  261. cursor: pointer;
  262. }
  263. }
  264. }
  265. </style>