我正在创建一个用于过滤返回数据的单选项组 Select 器,但我被困在最后一个区域.

它吸引了所有适当的"焦点区域",但是它对数据库中的每一项都这样做.我想做的是让它在每个"焦点区域"只显示一次,而不是每个条目.你可以在下面的图片中看到它在做什么.

enter image description here

我相信这个问题存在于下面的代码中,但我不确定它是什么.我添加了脚本以了解更多上下文.

如有任何帮助,将不胜感激:

     <v-radio-group class="ml-2" v-model="filters.areaOfFocus" column>
        <v-radio
          v-for="areaOfFocus in areaOfFocuses"
          :key="areaOfFocus"
          :label="areaOfFocus"
          :value="areaOfFocus"
          color="#C5D5EA"
        ></v-radio>
      </v-radio-group>

Script

export default {
  name: "CapacityBuilding",
  props: ["tools"],
  data() {
    return {
      menu1: false,
      menu2: false,
      menu3: false,
      FullProfileDialog: false,
      error: null,
      filters: {
        keyword: null,
        areaOfFocus: null,
        resourceType: null,
        resourceFormat: null,
      },
      viewingEntity: null,
      areaofFocuses: [],
      resourceTypes: [],
      resourceFormats: [],
      filteredTools: [],
      websiteLink: [],
      videoLink: [],
      file: [],
      toolHeaders: [
        { text: "Name", value: "attributes.name", width: "30%" },
        {
          text: "Category",
          value: "attributes.contentResourceTopic",
          width: "20%",
        },
        {
          text: "Format",
          value: "attributes.contentResourceFormat",
          width: "15%",
        },
        { text: "Type", value: "attributes.contentResourceType", width: "20%" },
        {
          text: "Actions",
          value: "actions",
          align: "center",
          sortable: false,
          width: "20%",
          class: "actions-text",
        },
      ],
    };
  },
  created() {
    this.areaOfFocuses = this.tools
      .map((t) => t.attributes.contentAreaOfFocus)
      .sort();
    this.resourceTypes = this.tools
      .map((t) => t.attributes.contentResourceType)
      .sort();
    this.resourceFormats = this.tools
      .map((t) => t.attributes.contentResourceFormat)
      .sort();
    this.filteredTools = this.tools;
  },
  methods: {
    async applyFilters() {
      var tempFilteredTools = JSON.parse(JSON.stringify(this.tools));

      if (this.filters.keyword) {
        var searchBy = this.filters.keyword.toLowerCase();

        tempFilteredTools = tempFilteredTools.filter(
          (q) =>
            q.attributes.name.toLowerCase().indexOf(searchBy) !== -1 ||
            (q.attributes.contentResourceFormat &&
              q.attributes.contentResourceFormat
                .toLowerCase()
                .indexOf(searchBy) !== -1) ||
            (q.attributes.contentResourceTopic &&
              q.attributes.contentResourceTopic
                .toLowerCase()
                .indexOf(searchBy) !== -1) ||
            (q.attributes.contentResourceType &&
              q.attributes.contentResourceType
                .toLowerCase()
                .indexOf(searchBy) !== -1)
        );
      }

      if (this.filters.areaOfFocus) {
        tempFilteredTools = tempFilteredTools.filter(
          (q) => q.attributes.contentAreaOfFocus === this.filters.areaOfFocus
        );
      }

      if (this.filters.resourceType) {
        tempFilteredTools = tempFilteredTools.filter(
          (q) => q.attributes.contentResourceType === this.filters.resourceType
        );
      }

      if (this.filters.resourceFormat) {
        tempFilteredTools = tempFilteredTools.filter(
          (q) =>
            q.attributes.contentResourceFormat === this.filters.resourceFormat
        );
      }

      this.filteredTools = tempFilteredTools;
    },
    async resetSearch() {
      this.filters = {
        areaOfFocus: null,
        resourceFormat: null,
        resourceType: null,
        keyword: null,
      };
      await this.applyFilters();
    },
  },
};

推荐答案

您可以使用Set删除重复项:

const tools = [{attributes: {contentAreaOfFocus: 'c'}}, {attributes: {contentAreaOfFocus: 'b'}}, {attributes: {contentAreaOfFocus: 'c'}}, {attributes: {contentAreaOfFocus: 'a'}}, {attributes: {contentAreaOfFocus: 'a'}}]
const areaOfFocuses = [... new Set(tools.map(a => a.attributes.contentAreaOfFocus).sort())]
console.log(areaOfFocuses)

Vue.js相关问答推荐

元素不会对react 性props 的更改做出react

如何向 添加方法和 v-model?

处理多张卡片中的按钮

有没有办法初始化一个保留初始 HTML 的 Vue 对象

VueJS:在组件之间使用全局对象的最佳实践?

VueJS:为什么在input事件处理程序中触发Input输入事件?

如何从特定索引呈现 v-for

使用 vue-cli 遇到无法推断解析器错误

Express.js + lint 出错

如何从 .vue 文件中导出多个对象

Vue 和 TypeScript 所需的props

验证子组件Vue vee-validate

请求正在进行时显示加载器

如何让 Vue (vue cli 3) 正确处理 GraphQL 文件?

在加载数据之前触发mounted方法 - VueJS

在找不到图像源时设置替代图像

如何在 Vue.js 插槽范围内传递方法

emit更新数组不起作用

Vue 2.0 设置路由 - 不要使用new来产生副作用

根据nuxt中的url参数动态加载组件