MenuTree.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="menu-list">
  3. <el-tree highlight-current :data="nodeList" :props="defaultProps" @node-click="handleNodeClick">
  4. <div slot-scope="{ node }" class="custom-tree-node">
  5. <div
  6. :class="[
  7. 'tree-box-item',
  8. curSelectId == node.data.id ? 'tree-box-item-active' : '',
  9. node.data.children ? 'tree-box-item-father' : '',
  10. ]"
  11. >
  12. <span style="margin-right: 10px">{{ node.label }}</span>
  13. </div>
  14. </div>
  15. </el-tree>
  16. </div>
  17. </template>
  18. <script>
  19. import { isTrue } from '@/utils/validate';
  20. export default {
  21. name: 'MenuTree',
  22. props: {
  23. nodeList: {
  24. type: Array,
  25. required: true,
  26. },
  27. id: {
  28. type: String,
  29. default: '',
  30. },
  31. bookName: {
  32. type: String,
  33. default: '',
  34. },
  35. },
  36. data() {
  37. return {
  38. isTrue,
  39. curSelectId: this.id || '',
  40. defaultProps: {
  41. children: 'children',
  42. label: 'label',
  43. nodeKey: 'id',
  44. isLeaf: (data, node) => {
  45. if (data.is_leaf === 'true') {
  46. // 实体门店 叶子结点 不展示icon
  47. return true;
  48. }
  49. },
  50. },
  51. };
  52. },
  53. watch: {
  54. nodeList: {
  55. handler(val) {},
  56. immediate: true,
  57. },
  58. },
  59. created() {},
  60. methods: {
  61. /**
  62. * 选择节点
  63. * @param {string} nodeId - 节点ID
  64. * @param {boolean} isLeaf - 是否是叶子节点
  65. */
  66. selectNode(nodeId, isLeaf) {
  67. // if (!isLeaf) return;
  68. if (this.curSelectId === nodeId) return;
  69. this.curSelectId = nodeId;
  70. this.$emit('selectNode', nodeId);
  71. },
  72. /**
  73. * 计算章节名称样式
  74. * @param {number} deep - 节点深度
  75. * @param {boolean} isLeaf - 是否是叶子节点
  76. * @returns {Object} - 样式对象
  77. */
  78. computedNameStyle(deep, isLeaf) {
  79. return {
  80. 'padding-left': `${(deep - 1) * 16}px`,
  81. cursor: isLeaf ? 'pointer' : 'auto',
  82. };
  83. },
  84. handleNodeClick(data) {
  85. // if (data.isLeaf) {
  86. // this.nodeName = data.name;
  87. this.selectNode(data.id);
  88. this.curSelectId = data.id;
  89. // }
  90. },
  91. },
  92. };
  93. </script>
  94. <style lang="scss" scoped>
  95. .menu-list {
  96. :deep .el-tree {
  97. background: transparent;
  98. .el-tree-node__expand-icon {
  99. font-size: 16px;
  100. }
  101. }
  102. .tree-box-item-active {
  103. color: #f90;
  104. }
  105. .custom-tree-node {
  106. display: block !important;
  107. width: 100%;
  108. height: 100%;
  109. color: #2c2c2c;
  110. }
  111. :deep .el-tree-node__content,
  112. :deep .custom-tree-node {
  113. height: auto;
  114. }
  115. .el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  116. background: none;
  117. }
  118. .tree-box-item {
  119. align-items: center;
  120. overflow: hidden;
  121. font-weight: 400;
  122. line-height: 38px;
  123. text-overflow: ellipsis;
  124. white-space: nowrap;
  125. -webkit-box-align: center;
  126. -ms-flex-align: center;
  127. &-father {
  128. font-weight: bold;
  129. }
  130. }
  131. }
  132. </style>
  133. <style lang="scss"></style>