HcTreeNode.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <ElTree class="hc-tree-node-box" ref="ElTreeRef" :props="ElTreeProps" :data="ElTreeData" :node-key="nodeKey" highlight-current accordion show-checkbox :check-strictly="isStrictly" :expand-on-click-node="false" @check="ElTreeCheckChange">
  3. <template #default="{ node, data }">
  4. <div class="custom-tree-node">
  5. <div class="label" @dblclick="ElTreeDblClick(data)">
  6. <el-input v-model="data.title" size="small" @keyup="keyUpEvent($event,data)" v-if="data.isInputName" @blur="ElTreeBtnClick(data)">
  7. <template #append>
  8. <el-button type="primary" plain size="small" @click="ElTreeBtnClick(data)">
  9. <HcIcon name="done"/>
  10. </el-button>
  11. </template>
  12. </el-input>
  13. <span v-else>{{data.title}}</span>
  14. </div>
  15. </div>
  16. </template>
  17. </ElTree>
  18. </template>
  19. <script setup>
  20. import {nextTick, onMounted, ref, watch} from "vue";
  21. import {getArrValue} from "vue-utils-plus"
  22. import wbsApi from '~api/data-fill/wbs';
  23. //参数
  24. const props = defineProps({
  25. projectId: {
  26. type: [String,Number],
  27. default: ''
  28. },
  29. nodeKey: {
  30. type: String,
  31. default: 'primaryKeyId'
  32. },
  33. nodeId: {
  34. type: [String,Number],
  35. default: ''
  36. },
  37. oldId: {
  38. type: [String,Number],
  39. default: ''
  40. },
  41. strictly: {
  42. type: Boolean,
  43. default: false
  44. },
  45. })
  46. //变量
  47. const ElTreeRef = ref(null)
  48. const projectId = ref(props.projectId)
  49. const isStrictly = ref(props.strictly)
  50. const ElTreeProps = ref({label: 'title', children: 'children', isLeaf: 'notExsitChild'})
  51. //监听
  52. watch(() => [
  53. props.projectId,
  54. props.strictly,
  55. ], ([pid,strictly]) => {
  56. projectId.value = pid
  57. isStrictly.value = strictly
  58. })
  59. //渲染完成
  60. onMounted(() => {
  61. ElTreeLoadNode()
  62. })
  63. //事件
  64. const emit = defineEmits(['check-change'])
  65. //树形结构异步加载数据
  66. const ElTreeData = ref([])
  67. const ElTreeLoadNode = async () => {
  68. let nodeId = props.oldId || props.nodeId || ''
  69. const {error, code, data} = await wbsApi.queryWbsTreePrivateByProjectIdAndId({
  70. id: nodeId,
  71. projectId: projectId.value
  72. })
  73. if (!error && code === 200) {
  74. ElTreeData.value = getArrValue(data)
  75. await nextTick(() => {
  76. ElTreeCheckedKeys()
  77. })
  78. }
  79. }
  80. //被选择的
  81. const ElTreeCheckChange = (_,nodes) => {
  82. emit('check-change', nodes)
  83. }
  84. //处理节点
  85. const ElTreeCheckedKeys = () => {
  86. const Nodes = ElTreeRef.value.getCheckedNodes() || []
  87. const HalfNodes = ElTreeRef.value.getHalfCheckedNodes() || []
  88. emit('check-change', {
  89. checkedNodes: Nodes,
  90. halfCheckedNodes: HalfNodes
  91. })
  92. }
  93. //更改节点名称
  94. const ElTreeDblClick = (item) => {
  95. item.isInputName = true;
  96. }
  97. //回车
  98. const keyUpEvent = (e,item) => {
  99. if (e.key === "Enter") {
  100. ElTreeBtnClick(item)
  101. }
  102. }
  103. //更改节点名称完成
  104. const ElTreeBtnClick = (item) => {
  105. if (!item?.title) {
  106. window?.$message?.warning('节点名称不能为空')
  107. } else {
  108. item.isInputName = false;
  109. ElTreeCheckedKeys()
  110. }
  111. }
  112. </script>
  113. <style lang="scss" scoped>
  114. .custom-tree-node {
  115. position: relative;
  116. width: 100%;
  117. }
  118. </style>
  119. <style lang="scss">
  120. .hc-tree-node-box .el-tree-node__content {
  121. height: 32px !important;
  122. }
  123. </style>