primevue-mirror/apps/showcase/doc/autocomplete/ObjectsDoc.vue

118 lines
3.6 KiB
Vue
Raw Permalink Normal View History

2023-02-28 08:29:30 +00:00
<template>
<DocSectionText v-bind="$attrs">
<p>
AutoComplete can work with objects using the <i>optionLabel</i> property that defines the label to display as a suggestion. The value passed to the model would still be the object instance of a suggestion. Here is an example with a
Country object that has name and code fields such as <i>&#123;name: "United States", code:"USA"&#125;</i>.
</p>
</DocSectionText>
2024-05-20 12:14:38 +00:00
<div class="card flex justify-center">
2023-02-28 08:29:30 +00:00
<AutoComplete v-model="selectedCountry" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
</div>
<DocSectionCode :code="code" :service="['CountryService']" />
</template>
<script>
import { CountryService } from '@/service/CountryService';
export default {
data() {
return {
countries: null,
selectedCountry: null,
filteredCountries: null,
code: {
2023-09-22 12:54:14 +00:00
basic: `
2023-10-15 09:38:39 +00:00
<AutoComplete v-model="selectedCountry" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
`,
2023-09-22 12:54:14 +00:00
options: `
<template>
2024-05-20 12:14:38 +00:00
<div class="card flex justify-center">
2023-02-28 08:29:30 +00:00
<AutoComplete v-model="selectedCountry" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
</div>
</template>
<script>
import { CountryService } from '@/service/CountryService';
export default {
data() {
return {
countries: null,
selectedCountry: null,
filteredCountries: null
};
},
mounted() {
CountryService.getCountries().then((data) => (this.countries = data));
},
methods: {
search(event) {
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-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">
2023-02-28 08:29:30 +00:00
<AutoComplete v-model="selectedCountry" optionLabel="name" :suggestions="filteredCountries" @complete="search" />
</div>
</template>
<script setup>
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();
const search = (event) => {
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-10-15 09:38:39 +00:00
<\/script>
`
2023-02-28 08:29:30 +00:00
}
};
},
mounted() {
CountryService.getCountries().then((data) => (this.countries = data));
},
methods: {
search(event) {
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);
}
}
};
</script>