recursive.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <div>
  3. <div :class="{'embedded': outweigh}">
  4. <template v-if="outweigh">
  5. <span :class="{'act': actIndex === -1}">不限</span>
  6. <!-- <span class="text666 mr-2"></span> -->
  7. </template>
  8. <span v-for="(item, index) in props.items" :key="item.id">
  9. <span v-if="index < num" class="mx-3" :class="{'act': actIndex === index}" style="line-height: 32px;" @click="handleClick(item, index)">{{ item.name }}</span>
  10. </span>
  11. <template v-if="underAndEqual">
  12. <!-- <span class="text666 mr-2"></span> -->
  13. <span v-if="props.items?.length >= num" @click="handleClick(props.items, 'other')">其他</span>
  14. </template>
  15. </div>
  16. <template v-if="children?.length">
  17. <v-divider v-if="outweigh" class="mx-2"></v-divider>
  18. <recursive ref="childRef" :defaultOpen="props.defaultOpen" :items="children" :parentId="itemId"></recursive>
  19. </template>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { computed, reactive, ref, watch } from 'vue'
  24. import recursive from './recursive'
  25. defineOptions({ name:'common-components-areaTree-recursive'})
  26. const num = 14
  27. const childRef = ref(null)
  28. const props = defineProps({
  29. items: Object,
  30. defaultOpen: {
  31. type: Number,
  32. default: 3
  33. },
  34. parentId: {
  35. type: [Number, String],
  36. default: -1
  37. }
  38. })
  39. // const currentRowType = computed(() => {
  40. // if (props.items?.length) return props.items[0].type - 0
  41. // else return 0
  42. // })
  43. let itemId = ref(0)
  44. let currentRowType = ref(0)
  45. let actIndex = ref(0)
  46. let currentRow = reactive({ id: -1})
  47. let children = ref([])
  48. const setRow = (item) => {
  49. if (item) {
  50. console.log('currentRow1', { ...item, children: null})
  51. currentRow = item
  52. children.value = currentRow.children
  53. itemId.value = currentRow.id || 0
  54. currentRowType.value = currentRow.type || 0
  55. } else {
  56. currentRow = { id: -1 }
  57. itemId.value = -1
  58. currentRowType.value = 0
  59. }
  60. }
  61. const init = () => {
  62. itemId.value = -1
  63. if (currentRowType.value < props.defaultOpen && props.items?.length) {
  64. setRow(props.items[0])
  65. actIndex.value = 0
  66. } else {
  67. setRow(null)
  68. }
  69. if (currentRowType.value > props.defaultOpen) {
  70. actIndex.value = -1
  71. }
  72. }
  73. // 监听
  74. watch(
  75. () => props.parentId,
  76. () => {
  77. // 函数
  78. init()
  79. },
  80. { immediate: true },
  81. { deep: true }
  82. )
  83. const handleClick = (item, index) => {
  84. console.log('init', item.name)
  85. if (index === 'other') return
  86. else {
  87. actIndex.value = index
  88. currentRow.value = item
  89. children.value = item.children
  90. children.value = item.children
  91. itemId.value = item.id || 0
  92. }
  93. }
  94. console.log('currentRowType', currentRowType)
  95. const outweigh = computed(() => (currentRowType.value > props.defaultOpen))
  96. const underAndEqual = computed(() => (currentRowType.value <= props.defaultOpen))
  97. </script>
  98. <style lang="scss" scoped>
  99. .text666 {
  100. // color: #666666;
  101. border-right: 1px solid #666666;
  102. }
  103. .text333 {
  104. color: #333333;
  105. }
  106. .act {
  107. color: var(--v-primary-base);
  108. }
  109. span {
  110. cursor: pointer;
  111. &:hover {
  112. color: var(--v-primary-lighten1);
  113. }
  114. }
  115. .embedded {
  116. padding: 4px 12px;
  117. background-color: #f8f8f8;
  118. }
  119. </style>