在VUE选择列表的开始和数组中显示主要选项?
例如,在国家 /地区选择列表下拉列表中,我想先显示最常见的选项选项,但也要在整个阵列沿通常按名称的位置显示它们。我还计划在其他选择产品和类别的选择上执行此操作。
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的解决方案是将ARRMAIN上的ID键从“代码”更改为“ ID”,因此我不会在控制台上发出重复的键。另外,我添加了一个空的标签和一个“文本”键来命名并将数组分开。
然后,一个带有自定义选项道具的模板以显示我想要的方式:
我唯一找不到的方法是在下拉列表中的“文本”键上阻止选择。指针盛会:无;不起作用。
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.
And then a template with a custom option prop to display the way I want:
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.