tabs.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="btn-group">
  3. <div
  4. :class="['btn-item', { active: activeTabs === index }]"
  5. @click="handleActive(item,index)"
  6. v-for="(item, index) in baseList"
  7. :key="index + item"
  8. >
  9. {{ item.indicatorName }}
  10. </div>
  11. </div>
  12. </template>
  13. <script setup>
  14. import {onMounted, onUnmounted, ref, watch} from 'vue'
  15. import eventBus from "@/api/eventBus";
  16. const props = defineProps({
  17. list: {
  18. type: Array,
  19. defalut: ()=>[],
  20. },
  21. keyStr: {
  22. type: String,
  23. defalut: '',
  24. },
  25. active: {
  26. type: String,
  27. defalut: '',
  28. },
  29. });
  30. onMounted(()=>{
  31. //监听来自 navigation 的 tab 高亮更新事件
  32. eventBus.on('updateTabActive', handleUpdateTabActive)
  33. })
  34. onUnmounted(()=>{
  35. eventBus.off('updateTabActive', handleUpdateTabActive)
  36. })
  37. const emit = defineEmits(["handleActive"])
  38. const baseList = ref([])
  39. const activeTabs = ref(0);
  40. const handleActive = (item,index) => {
  41. activeTabs.value = index;
  42. emit('handleActive',{name:props.keyStr,id:item.id})
  43. eventBus.emit('handleActive',{id:item.id,key:props.keyStr,monitorItems:props.list})
  44. };
  45. // 处理来自 navigation 的 tab 高亮更新事件
  46. function handleUpdateTabActive({indicatorId, indicatorName}) {
  47. // 在当前列表中查找匹配的指标
  48. const targetIndex = baseList.value.findIndex(item =>
  49. item.id === indicatorId || item.indicatorName === indicatorName
  50. );
  51. if (targetIndex !== -1) {
  52. activeTabs.value = targetIndex;
  53. // 触发父组件事件,确保数据也能正常更新
  54. emit('handleActive',{name:indicatorName,id:indicatorId})
  55. }
  56. }
  57. watch(()=>props.list,(newValue,oldValue)=>{
  58. if(newValue){
  59. baseList.value = newValue
  60. }
  61. })
  62. watch(()=>props.active,(newValue,oldValue)=>{
  63. if(newValue){
  64. const index = props.list.findIndex(item =>item.indicatorName===newValue)
  65. if(index!==-1){
  66. activeTabs.value = index
  67. handleActive(newValue,index)
  68. }
  69. }
  70. })
  71. </script>
  72. <style lang="scss" scoped>
  73. .btn-group {
  74. font-size: 11px;
  75. display: flex;
  76. align-items: center;
  77. .btn-item {
  78. border-radius: 2px;
  79. padding: 3px 8px;
  80. margin-left: 8px;
  81. cursor: pointer;
  82. flex: none;
  83. &.active {
  84. background: #4f4f4f;
  85. }
  86. }
  87. }
  88. </style>