Updated autocomplete demo
parent
95e0cfe13f
commit
a8e1ab6677
|
@ -11,27 +11,21 @@
|
||||||
<div class="content-section implementation">
|
<div class="content-section implementation">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<h5>Basic</h5>
|
<h5>Basic</h5>
|
||||||
<AutoComplete v-model="selectedCountry" :suggestions="filteredCountriesBasic" @complete="searchCountryBasic($event)" field="name" />
|
<AutoComplete v-model="selectedCountry1" :suggestions="filteredCountries" @complete="searchCountry($event)" field="name" />
|
||||||
<span style="marginLeft: .5em">Country: {{selectedCountry || 'none'}}</span>
|
|
||||||
|
|
||||||
<h5>Dropdown and Templating</h5>
|
<h5>Dropdown and Templating</h5>
|
||||||
<AutoComplete v-model="brand" :suggestions="filteredBrands" @complete="searchBrand($event)" placeholder="Search car brands" :dropdown="true">
|
<AutoComplete v-model="selectedCountry2" :suggestions="filteredCountries" @complete="searchCountry($event)" :dropdown="true" field="name">
|
||||||
<template #item="slotProps">
|
<template #item="slotProps">
|
||||||
<div class="p-autocomplete-brand-item">
|
<div class="country-item">
|
||||||
<img :alt="slotProps.item" :src="'demo/images/car/' + slotProps.item + '.png'" />
|
<img src="../../assets/images/flag_placeholder.png" :class="'flag flag-' + slotProps.item.code.toLowerCase()" class="p-mr-2" />
|
||||||
<div>{{slotProps.item}}</div>
|
<div>{{slotProps.item.name}}</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</AutoComplete>
|
</AutoComplete>
|
||||||
<span style="marginLeft: .5em">Brand: {{brand || 'none'}}</span>
|
|
||||||
|
|
||||||
<h5>Multiple</h5>
|
<h5>Multiple</h5>
|
||||||
<span class="p-fluid">
|
<span class="p-fluid">
|
||||||
<AutoComplete :multiple="true" v-model="selectedCountries" :suggestions="filteredCountriesMultiple" @complete="searchCountryMultiple($event)" field="name" />
|
<AutoComplete :multiple="true" v-model="selectedCountries" :suggestions="filteredCountries" @complete="searchCountry($event)" field="name" />
|
||||||
</span>
|
</span>
|
||||||
<ul>
|
|
||||||
<li v-for="(c,i) of selectedCountries" :key="i">{{c}}</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -47,13 +41,10 @@ export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
countries: null,
|
countries: null,
|
||||||
selectedCountry: null,
|
selectedCountry1: null,
|
||||||
filteredCountriesBasic: null,
|
selectedCountry2: null,
|
||||||
selectedCountries: [],
|
filteredCountries: null,
|
||||||
filteredCountriesMultiple: null,
|
selectedCountries: []
|
||||||
brands: null,
|
|
||||||
brand: null,
|
|
||||||
filteredBrands: null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
countryService: null,
|
countryService: null,
|
||||||
|
@ -62,38 +53,18 @@ export default {
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.countryService.getCountries().then(data => this.countries = data);
|
this.countryService.getCountries().then(data => this.countries = data);
|
||||||
this.brands = ['Audi', 'BMW', 'Fiat', 'Ford', 'Honda', 'Jaguar', 'Mercedes', 'Renault', 'Volvo'];
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
searchCountry(query) {
|
searchCountry(event) {
|
||||||
return this.countries.filter((country) => {
|
|
||||||
return country.name.toLowerCase().startsWith(query.toLowerCase());
|
|
||||||
});
|
|
||||||
},
|
|
||||||
searchCountryBasic(event) {
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.filteredCountriesBasic = this.searchCountry(event.query);
|
if (!event.query.trim().length) {
|
||||||
}, 250);
|
this.filteredCountries = [...this.countries];
|
||||||
},
|
|
||||||
searchCountryMultiple(event) {
|
|
||||||
setTimeout(() => {
|
|
||||||
this.filteredCountriesMultiple = this.searchCountry(event.query);
|
|
||||||
}, 250);
|
|
||||||
},
|
|
||||||
searchBrand(event) {
|
|
||||||
setTimeout(() => {
|
|
||||||
let results;
|
|
||||||
|
|
||||||
if (event.query.length === 0) {
|
|
||||||
results = [...this.brands];
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
results = this.brands.filter((brand) => {
|
this.filteredCountries = this.countries.filter((country) => {
|
||||||
return brand.toLowerCase().startsWith(event.query.toLowerCase());
|
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.filteredBrands = results;
|
|
||||||
}, 250);
|
}, 250);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -104,10 +75,9 @@ export default {
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
.p-autocomplete-brand-item {
|
.country-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
|
||||||
|
|
||||||
img {
|
img {
|
||||||
width: 28px;
|
width: 28px;
|
||||||
|
|
|
@ -254,28 +254,22 @@ export default {
|
||||||
</a>
|
</a>
|
||||||
<CodeHighlight>
|
<CodeHighlight>
|
||||||
<template v-pre>
|
<template v-pre>
|
||||||
<h3>Basic</h3>
|
<h5>Basic</h5>
|
||||||
<AutoComplete v-model="selectedCountry" :suggestions="filteredCountriesBasic" @complete="searchCountryBasic($event)" field="name" />
|
<AutoComplete v-model="selectedCountry1" :suggestions="filteredCountries" @complete="searchCountry($event)" field="name" />
|
||||||
<span style="marginLeft: .5em">Country: {{selectedCountry || 'none'}}</span>
|
<h5>Dropdown and Templating</h5>
|
||||||
|
<AutoComplete v-model="selectedCountry2" :suggestions="filteredCountries" @complete="searchCountry($event)" :dropdown="true" field="name">
|
||||||
<h3>Dropdown and Templating</h3>
|
|
||||||
<AutoComplete v-model="brand" :suggestions="filteredBrands" @complete="searchBrand($event)" placeholder="Hint: type 'v' or 'f'" :dropdown="true">
|
|
||||||
<template #item="slotProps">
|
<template #item="slotProps">
|
||||||
<div class="p-autocomplete-brand-item">
|
<div class="country-item">
|
||||||
<img :alt="slotProps.item" :src="'demo/images/car/' + slotProps.item + '.png'" />
|
<img src="../../assets/images/flag_placeholder.png" :class="'flag flag-' + slotProps.item.code.toLowerCase()" class="p-mr-2" />
|
||||||
<div>{{slotProps.item}}</div>
|
<div>{{slotProps.item.name}}</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</AutoComplete>
|
</AutoComplete>
|
||||||
<span style="marginLeft: .5em">Brand: {{brand || 'none'}}</span>
|
|
||||||
|
|
||||||
<h3>Multiple</h3>
|
<h5>Multiple</h5>
|
||||||
<span class="p-fluid">
|
<span class="p-fluid">
|
||||||
<AutoComplete :multiple="true" v-model="selectedCountries" :suggestions="filteredCountriesMultiple" @complete="searchCountryMultiple($event)" field="name" />
|
<AutoComplete :multiple="true" v-model="selectedCountries" :suggestions="filteredCountries" @complete="searchCountry($event)" field="name" />
|
||||||
</span>
|
</span>
|
||||||
<ul>
|
|
||||||
<li v-for="(c,i) of selectedCountries" :key="i">{{c}}</li>
|
|
||||||
</ul>
|
|
||||||
</template>
|
</template>
|
||||||
</CodeHighlight>
|
</CodeHighlight>
|
||||||
|
|
||||||
|
@ -286,13 +280,10 @@ export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
countries: null,
|
countries: null,
|
||||||
selectedCountry: null,
|
selectedCountry1: null,
|
||||||
filteredCountriesBasic: null,
|
selectedCountry2: null,
|
||||||
selectedCountries: [],
|
filteredCountries: null,
|
||||||
filteredCountriesMultiple: null,
|
selectedCountries: []
|
||||||
brands: null,
|
|
||||||
brand: null,
|
|
||||||
filteredBrands: null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
countryService: null,
|
countryService: null,
|
||||||
|
@ -301,38 +292,18 @@ export default {
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
this.countryService.getCountries().then(data => this.countries = data);
|
this.countryService.getCountries().then(data => this.countries = data);
|
||||||
this.brands = ['Audi', 'BMW', 'Fiat', 'Ford', 'Honda', 'Jaguar', 'Mercedes', 'Renault', 'Volvo'];
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
searchCountry(query) {
|
searchCountry(event) {
|
||||||
return this.countries.filter((country) => {
|
|
||||||
return country.name.toLowerCase().startsWith(query.toLowerCase());
|
|
||||||
});
|
|
||||||
},
|
|
||||||
searchCountryBasic(event) {
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.filteredCountriesBasic = this.searchCountry(event.query);
|
if (!event.query.trim().length) {
|
||||||
}, 250);
|
this.filteredCountries = [...this.countries];
|
||||||
},
|
|
||||||
searchCountryMultiple(event) {
|
|
||||||
setTimeout(() => {
|
|
||||||
this.filteredCountriesMultiple = this.searchCountry(event.query);
|
|
||||||
}, 250);
|
|
||||||
},
|
|
||||||
searchBrand(event) {
|
|
||||||
setTimeout(() => {
|
|
||||||
let results;
|
|
||||||
|
|
||||||
if (event.query.length === 0) {
|
|
||||||
results = [...this.brands];
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
results = this.brands.filter((brand) => {
|
this.filteredCountries = this.countries.filter((country) => {
|
||||||
return brand.toLowerCase().startsWith(event.query.toLowerCase());
|
return country.name.toLowerCase().startsWith(event.query.toLowerCase());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.filteredBrands = results;
|
|
||||||
}, 250);
|
}, 250);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -340,10 +311,9 @@ export default {
|
||||||
</CodeHighlight>
|
</CodeHighlight>
|
||||||
|
|
||||||
<CodeHighlight lang="css">
|
<CodeHighlight lang="css">
|
||||||
.p-autocomplete-brand-item {
|
.country-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
|
||||||
|
|
||||||
img {
|
img {
|
||||||
width: 28px;
|
width: 28px;
|
||||||
|
|
Loading…
Reference in New Issue