CategoryDialog.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <ele-modal
  3. :title="title"
  4. :visible.sync="treeVisible"
  5. :before-close="handleClose"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. append-to-body
  9. width="40%"
  10. >
  11. <div class="ele-border-lighter sys-organization-list">
  12. <el-tree
  13. ref="tree"
  14. :data="treeData"
  15. highlight-current
  16. node-key="id"
  17. :props="{ label: 'name' }"
  18. :expand-on-click-node="false"
  19. :default-expand-all="true"
  20. @node-click="onNodeClick"
  21. />
  22. </div>
  23. <template v-slot:footer>
  24. <el-button @click="handleClose">关闭</el-button>
  25. <el-button type="primary" @click="selected">选择</el-button>
  26. </template>
  27. </ele-modal>
  28. </template>
  29. <script>
  30. import { getTreeByPid } from '@/api/material/manage.js'
  31. export default {
  32. data () {
  33. return {
  34. treeVisible: false,
  35. treeData: [],
  36. current:null,
  37. title:'选择分类',
  38. pathList:[]
  39. }
  40. },
  41. watch: {
  42. },
  43. methods: {
  44. open(pid,title){
  45. this.treeVisible = true
  46. this.title = title
  47. // if(title=='选择产品分类'){
  48. // if(this.remarkform.productCategoryLevelId) this.$refs.tree.setCurrentKey(this.remarkform.productCategoryLevelId)
  49. // }else{
  50. // if(this.remarkform.categoryLevelId) this.$refs.tree.setCurrentKey(this.remarkform.categoryLevelId)
  51. // }
  52. this.getTree(pid)
  53. },
  54. handleClose () {
  55. this.treeVisible = false
  56. this.current = null
  57. },
  58. getTree(pid){
  59. getTreeByPid(pid).then(res=>{
  60. this.treeData = res
  61. })
  62. },
  63. onNodeClick(node){
  64. this.current = node
  65. },
  66. selected(){
  67. if(!this.current){
  68. return this.$message.warning(`请${this.title}`)
  69. }
  70. this.pathList = this.findParent([],this.current,this.treeData)
  71. this.pathList.unshift(this.current)
  72. const PathInfo = {
  73. categoryLevelPath:'',
  74. categoryLevelPathId:[]
  75. }
  76. const pathName = []
  77. this.pathList.map(item=>{
  78. pathName.unshift(item.name)
  79. PathInfo.categoryLevelPathId.unshift(item.id)
  80. })
  81. PathInfo.categoryLevelPath = pathName.join('-')
  82. PathInfo.categoryLevelPathId = PathInfo.categoryLevelPathId.join(',')
  83. this.$emit('chooseCategory',this.current,this.title,PathInfo)
  84. this.handleClose()
  85. },
  86. // parents:用于返回的数组,childNode:要查询的节点,treeData:json树形数据
  87. findParent (parents, childNode, treeData) {
  88. for (let i = 0; i < treeData.length; i++) {
  89. // 父节点查询条件
  90. if (treeData[i].id === childNode.parentId) {
  91. // 如果找到结果,保存当前节点
  92. parents.push(treeData[i])
  93. // 用当前节点再去原数据查找当前节点的父节点
  94. this.findParent(parents, treeData[i],this.treeData)
  95. break
  96. } else {
  97. if (treeData[i].children instanceof Array) {
  98. // 没找到,遍历该节点的子节点
  99. this.findParent(parents, childNode, treeData[i].children)
  100. }
  101. }
  102. }
  103. return parents
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .sys-organization-list {
  110. height: calc(100vh - 264px);
  111. box-sizing: border-box;
  112. border-width: 1px;
  113. border-style: solid;
  114. overflow: auto;
  115. }
  116. .sys-organization-list :deep(.el-tree-node__content) {
  117. height: 40px;
  118. & > .el-tree-node__expand-icon {
  119. margin-left: 10px;
  120. }
  121. }
  122. </style>