CategoryList.vue 1.85 KB
<template>
  <v-container>
    <div height="100" class="pa-2">
      <v-row>
        <div class="tw-pr-0 tw-font-bold tw-w-[95px] tw-h-[36px] tw-leading-[36px]">一级类别:</div>
        <v-col class="pa-0">
          <v-btn
            v-for="(item, index) in categoryStore.list"
            :key="index"
            variant="text"
            @click="handleCategoryClick(item)"
            :color="
              categoryStore.selectedCategory === item.categoryDisplayName ? 'blue-accent-2' : ''
            "
          >
            {{ item.categoryDisplayName }}
          </v-btn>
        </v-col>
      </v-row>
      <v-row>
        <div class="tw-pr-0 tw-font-bold tw-w-[95px] tw-h-[36px] tw-leading-[36px]">二级类别:</div>
        <v-col class="pa-0">
          <v-btn
            v-for="(item, index) in subCategoryList"
            :key="index"
            variant="text"
            @click="handleSubCategoryClick(item.id)"
            :color="categoryStore.selectedSubCategory === item.id ? 'blue-accent-2' : ''"
          >
            {{ item.name }}
          </v-btn>
        </v-col>
      </v-row>
    </div>
  </v-container>
</template>

<script setup lang="ts">
import { useCategoryStore } from '@/stores/category'
import type { CategoryRootType } from '@/type'
import { computed } from 'vue'

const categoryStore = useCategoryStore()

const handleCategoryClick = (item: CategoryRootType) => {
  categoryStore.updateCategory(item.categoryDisplayName)
  categoryStore.updateSubCategory(item.list[0].id)
}

const handleSubCategoryClick = (value: string) => {
  categoryStore.updateSubCategory(value)
}

const subCategoryList = computed(() => {
  if (categoryStore.selectedCategory) {
    const tmp = categoryStore.list.filter(
      (item) => item.categoryDisplayName === categoryStore.selectedCategory
    )
    return tmp?.[0]?.list || []
  }
  return []
})
</script>