在VUE选择列表的开始和数组中显示主要选项?

发布于 2025-02-09 06:58:45 字数 1866 浏览 2 评论 0原文

例如,在国家 /地区选择列表下拉列表中,我想先显示最常见的选项选项,但也要在整个阵列沿通常按名称的位置显示它们。我还计划在其他选择产品和类别的选择上执行此操作。

我正在使用v-select来创建选择列表,并能够将“主选项”数组与concat()方法合并:

this.arrNew = this.arrmain.concat(this.arrcountry);

data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"Brasil","code":1},
        {"label":"Estados Unidos","code":136}
      ],
      arrNew: []
    }
  },

但是,正如预期的那样,由于重复的键,我会得到VUE警告,并且在Select的搜索结果中显示了两次国家。

如何在不实际合并的情况下将多个阵列组合为选择选项?

我还想将分隔线和文字放置以命名每个数组。

我选择国家 /地区的组件代码是这样的:

<template>
<div>
  <v-select v-model="$attrs.countryName" id="country" name="country" autocomplete="off" v-validate="'required'" required class="style-select-filter" @option:selected="$emit('changed', $event)" :options="arrNew" placeholder="Selecione um país"></v-select>
  <small v-if="errors.has('country')" class="field-text is-danger">{{ errors.first('country') }}</small>
</div>
</template>
<script>

let config = {
  headers: {
  }
}

export default {
  name: "Selectcountry",
  inject: ['$validator'],
  data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"Brasil","code":1},
        {"label":"Estados Unidos","code":136}
      ],
      arrNew: []
    }
  },  
  methods: {
    clearTest() {
      alert("Teste");
    },
    searchcountries() {
        
      this.$http.get('country/list', config).then(response => {
            
        this.arrCountry = response.data.pt;
        this.arrNew = this.arrMain.concat(this.arrCountry);

      }).catch(error => {
              console.log(error)
              this.errored = true
      }).finally(() => this.loading = false);

    }
  },
  created() {

  },
  mounted() {

      this.searchcountries();

  },
}
</script>

In the country select list dropdown for example, I want to show the most common selected options first, but also show them in their usual ordered-by-name position along the full array. I also plan to do this on other select for products and categories.

I'm using v-select to create the select list and was able to merge the "main options" array with concat() method:

this.arrNew = this.arrMain.concat(this.arrCountry);

data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"Brasil","code":1},
        {"label":"Estados Unidos","code":136}
      ],
      arrNew: []
    }
  },

but as expected I get a Vue warn due to duplicate keys and also the countries are displayed twice at the search results of the select.

How do I combine more than one array for the select option without actually merging them?

I would also like to place dividers and text to name each array.

My component code for country select is like this:

<template>
<div>
  <v-select v-model="$attrs.countryName" id="country" name="country" autocomplete="off" v-validate="'required'" required class="style-select-filter" @option:selected="$emit('changed', $event)" :options="arrNew" placeholder="Selecione um país"></v-select>
  <small v-if="errors.has('country')" class="field-text is-danger">{{ errors.first('country') }}</small>
</div>
</template>
<script>

let config = {
  headers: {
  }
}

export default {
  name: "Selectcountry",
  inject: ['$validator'],
  data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"Brasil","code":1},
        {"label":"Estados Unidos","code":136}
      ],
      arrNew: []
    }
  },  
  methods: {
    clearTest() {
      alert("Teste");
    },
    searchcountries() {
        
      this.$http.get('country/list', config).then(response => {
            
        this.arrCountry = response.data.pt;
        this.arrNew = this.arrMain.concat(this.arrCountry);

      }).catch(error => {
              console.log(error)
              this.errored = true
      }).finally(() => this.loading = false);

    }
  },
  created() {

  },
  mounted() {

      this.searchcountries();

  },
}
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-02-16 06:58:45

我发现的解决方案是将ARRMAIN上的ID键从“代码”更改为“ ID”,因此我不会在控制台上发出重复的键。另外,我添加了一个空的标签和一个“文本”键来命名并将数组分开。

data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"","text":"Most used"},
        {"label":"Brasil","id":1},
        {"label":"Estados Unidos","id":136},
        {"label":"","text":"All"}
      ],
      arrNew: []
    }
  }, 

然后,一个带有自定义选项道具的模板以显示我想要的方式:

<template #option="{ label, text }">
      <p style="font-weight: bold; margin: 0; pointer-events: none" class="pe-none user-select-none">{{ text }}</p>
      <span>{{ label }}</span>
</template>

我唯一找不到的方法是在下拉列表中的“文本”键上阻止选择。指针盛会:无;不起作用。

The solution I found was changing the id keys on arrMain from "code" to "id" so I don't get a duplicate keys warn on console. Also I added an empty label and a "text" key to name and separate the arrays.

data() {
    return {
      arrCountry: [],
      arrMain: [
        {"label":"","text":"Most used"},
        {"label":"Brasil","id":1},
        {"label":"Estados Unidos","id":136},
        {"label":"","text":"All"}
      ],
      arrNew: []
    }
  }, 

And then a template with a custom option prop to display the way I want:

<template #option="{ label, text }">
      <p style="font-weight: bold; margin: 0; pointer-events: none" class="pe-none user-select-none">{{ text }}</p>
      <span>{{ label }}</span>
</template>

The only thing I could not find a way to do is to block selection on the "text" keys in the dropdown. pointer-events: none; does not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文