primevue-mirror/doc/autocomplete/ForceSelectionDoc.vue

118 lines
3.6 KiB
Vue
Raw Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
ForceSelection mode validates the manual input to check whether it also exists in the suggestions list, if not the input value is cleared to make sure the value passed to the model is always one of the suggestions. Simply enable
<i>forceSelection</i> to enforce that input is always from the suggestion list.
</p>
</DocSectionText>
2024-05-20 12:14:38 +00:00
<div class="card flex justify-center">
2024-04-24 10:36:41 +00:00
<AutoComplete v-model="selectedCountry" forceSelection optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2023-02-28 08:29:30 +00:00
</div>
2024-04-24 10:36:41 +00:00
<DocSectionCode :code="code" :service="['CountryService']" />
2023-02-28 08:29:30 +00:00
</template>
<script>
2024-04-24 10:36:41 +00:00
import { CountryService } from '@/service/CountryService';
2023-02-28 08:29:30 +00:00
export default {
data() {
return {
2024-04-24 10:36:41 +00:00
countries: null,
selectedCountry: null,
filteredCountries: null,
2023-02-28 08:29:30 +00:00
code: {
2023-09-22 12:54:14 +00:00
basic: `
2024-04-24 10:36:41 +00:00
<AutoComplete v-model="selectedCountry" forceSelection optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2023-10-15 09:38:39 +00:00
`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2024-05-20 12:14:38 +00:00
<div class="card flex justify-center">
2024-04-24 10:36:41 +00:00
<AutoComplete v-model="selectedCountry" forceSelection optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2023-02-28 08:29:30 +00:00
</div>
</template>
<script>
2024-04-24 10:36:41 +00:00
import { CountryService } from '@/service/CountryService';
2023-02-28 08:29:30 +00:00
export default {
data() {
return {
2024-04-24 10:36:41 +00:00
countries: null,
selectedCountry: null,
filteredCountries: null
2023-02-28 08:29:30 +00:00
};
},
2024-04-24 10:36:41 +00:00
mounted() {
CountryService.getCountries().then((data) => (this.countries = data));
},
2023-02-28 08:29:30 +00:00
methods: {
search(event) {
2024-04-24 10:36:41 +00:00
setTimeout(() => {
if (!event.query.trim().length) {
this.filteredCountries = [...this.countries];
} else {
this.filteredCountries = this.countries.filter((country) => {
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
});
}
}, 250);
2023-02-28 08:29:30 +00:00
}
}
};
2023-10-15 09:38:39 +00:00
<\/script>
`,
2023-09-22 12:54:14 +00:00
composition: `
<template>
2024-05-20 12:14:38 +00:00
<div class="card flex justify-center">
2024-04-24 10:36:41 +00:00
<AutoComplete v-model="selectedCountry" forceSelection optionLabel="name" :suggestions="filteredCountries" @complete="search" />
2023-02-28 08:29:30 +00:00
</div>
</template>
<script setup>
2024-04-24 10:36:41 +00:00
import { ref, onMounted } from "vue";
import { CountryService } from "@/service/CountryService";
onMounted(() => {
CountryService.getCountries().then((data) => (countries.value = data));
});
const countries = ref();
const selectedCountry = ref();
const filteredCountries = ref();
2023-02-28 08:29:30 +00:00
const search = (event) => {
2024-04-24 10:36:41 +00:00
setTimeout(() => {
if (!event.query.trim().length) {
filteredCountries.value = [...countries.value];
} else {
filteredCountries.value = countries.value.filter((country) => {
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
});
}
}, 250);
2023-02-28 08:29:30 +00:00
}
2023-10-15 09:38:39 +00:00
<\/script>
`
2023-02-28 08:29:30 +00:00
}
};
},
2024-04-24 10:36:41 +00:00
mounted() {
CountryService.getCountries().then((data) => (this.countries = data));
},
2023-02-28 08:29:30 +00:00
methods: {
search(event) {
2024-04-24 10:36:41 +00:00
setTimeout(() => {
if (!event.query.trim().length) {
this.filteredCountries = [...this.countries];
} else {
this.filteredCountries = this.countries.filter((country) => {
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
});
}
}, 250);
2023-02-28 08:29:30 +00:00
}
}
};
</script>