Templates in Vue Auto complete component
11 Jun 202424 minutes to read
The AutoComplete has been provided with several options to customize each list items, group title, header and footer elements. It uses the Essential JS 2 Template engine to compile and render the elements properly.
Item template
The content of each list item within the AutoComplete can be customized with the help of itemTemplate
property.
In the following sample, each list item is split into two columns to display relevant data’s.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:sortOrder='sortOrder' :itemTemplate='iTemplate' popupHeight="450px"></ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const itemVue = createApp().component("itemTemplate", {
template: `<span><span class='name'></span><span class ='city'></span></span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
const fields = { value: 'FirstName' };
const waterMark = 'Find an employee';
const sortOrder = 'Ascending';
const data = remoteData;
const iTemplate = function (e) {
return {
template: itemVue
};
};
const query = new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.city {
right: 15px;
position: absolute;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:sortOrder='sortOrder' :itemTemplate='iTemplate' popupHeight="450px"></ejs-autocomplete>
</div>
</div>
</template>
<script>
import { createApp } from "vue";
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const itemVue = createApp().component("itemTemplate", {
template: `<span><span class='name'></span><span class ='city'></span></span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
data() {
return {
fields: { value: 'FirstName' },
waterMark: 'Find an employee',
sortOrder: 'Ascending',
data: remoteData,
iTemplate: function (e) {
return {
template: itemVue
};
},
query: new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6),
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.city {
right: 15px;
position: absolute;
}
</style>
Group template
The group header title under which appropriate sub-items are categorized can also be customize with the help of groupTemplate
property. This template is common for both inline and floating group header template.
In the following sample, employees are grouped according to their city.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:sortOrder='sortOrder' :groupTemplate='gTemplate' popupHeight="450px">
</ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
import { Query, Predicate, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const groupTemplate = createApp().component("groupTemplate", {
template: `<strong></strong>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
const fields = { value: 'FirstName', groupBy: 'City' };
const waterMark = 'Find an employee';
const sortOrder = 'Ascending';
const data = remoteData;
const gTemplate = function (e) {
return {
template: groupTemplate
};
};
const query = new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6).where(new Predicate('City', 'equal', 'london').or('City', 'equal', 'seattle'));
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:sortOrder='sortOrder' :groupTemplate='gTemplate' popupHeight="450px"></ejs-autocomplete>
</div>
</div>
</template>
<script>
import { createApp } from "vue";
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
import { Query, Predicate, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const groupTemplate = createApp().component("groupTemplate", {
template: `<strong></strong>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
data() {
return {
fields: { value: 'FirstName', groupBy: 'City' },
waterMark: 'Find an employee',
sortOrder: 'Ascending',
data: remoteData,
gTemplate: function (e) {
return {
template: groupTemplate
};
},
query: new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6).where(new Predicate('City', 'equal', 'london').or('City', 'equal', 'seattle')),
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>
Header template
The header element is shown statically at the top of the suggestion list items within the AutoComplete, and any custom element can be placed as a header element using headerTemplate
property.
In the following sample, the list items and its headers are designed and displayed as two columns similar to multiple columns of the grid.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:headerTemplate='hTemplate' :sortOrder='sortOrder' :itemTemplate='iTemplate'
popupHeight="450px"></ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const headerVue = createApp().component("headerTemplate", {
template: `<span class='head'><span class='name'>Name</span><span class='city'>City</span></span>`,
data() {
return {
data: {}
};
}
});
const itemVue = createApp().component("itemTemplate", {
template: `<span class='item'><span class='name'> </span><span class ='city'></span></span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
const fields = { value: 'FirstName' };
const waterMark = 'Find an employee';
const sortOrder = 'Ascending';
const data = remoteData;
const iTemplate = function (e) {
return {
template: itemVue
};
};
const hTemplate = function (e) {
return {
template: headerVue
};
};
const query = new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6);
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.city {
right: 15px;
position: absolute;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete id='employees' :query='query' :dataSource='data' :fields='fields' :placeholder='waterMark'
:headerTemplate='hTemplate' :sortOrder='sortOrder' :itemTemplate='iTemplate' popupHeight="450px">
</ejs-autocomplete>
</div>
</div>
</template>
<script>
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const headerVue = createApp().component("headerTemplate", {
template: `<span class='head'><span class='name'>Name</span><span class='city'>City</span></span>`,
data() {
return {
data: {}
};
}
});
const itemVue = createApp().component("itemTemplate", {
template: `<span class='item'><span class='name'> </span><span class ='city'></span></span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svc',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
data() {
return {
fields: { value: 'FirstName' },
waterMark: 'Find an employee',
sortOrder: 'Ascending',
data: remoteData,
iTemplate: function (e) {
return {
template: itemVue
};
},
hTemplate: function (e) {
return {
template: headerVue
};
},
query: new Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6),
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.city {
right: 15px;
position: absolute;
}
</style>
Footer template
The AutoComplete has options to show a footer element at the bottom of the list items in the suggestion list. Here, you can place any custom element as a footer element using footerTemplate
property.
In the following sample, footer element displays the total number of list items present in the AutoComplete.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' :footerTemplate='fTemplate' id='employees' :dataSource='data'
:placeholder='waterMark' :open='onopen'></ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp, ref, onMounted } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
const instance = ref(null);
const footVue = createApp().component("footerTemplate", {
template: `<span class='foot'></span>`,
data() {
return {
data: {}
};
}
});
onMounted(() => {
document.getElementsByClassName('e-autocomplete')[0].addEventListener('keyup', (e) => {
onopen();
})
});
const waterMark = 'Find a game';
const sortOrder = 'Ascending';
const data = ['Badminton', 'Basketball', 'Cricket',
'Football', 'Golf', 'Gymnastics',
'Hockey', 'Rugby', 'Snooker', 'Tennis'
];
const fTemplate = function (e) {
return {
template: footVue
};
};
const onopen = function () {
var count = instance.value.getItems().length;
var ele = document.getElementsByClassName('foot')[0];
ele.innerHTML = "Total list item: " + count;
};
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.foot {
text-indent: 1.2em;
display: block;
font-size: 15px;
line-height: 40px;
border-top: 1px solid #e0e0e0;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' :footerTemplate='fTemplate' id='employees' :dataSource='data'
:placeholder='waterMark' :open='onopen'></ejs-autocomplete>
</div>
</div>
</template>
<script>
import { createApp } from "vue";
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
const footVue = createApp().component("footerTemplate", {
template: `<span class='foot'></span>`,
data() {
return {
data: {}
};
}
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
mounted() {
document.getElementsByClassName('e-autocomplete')[0].addEventListener('keyup', (e) => {
this.onopen();
})
},
data() {
return {
waterMark: 'Find a game',
sortOrder: 'Ascending',
data: ['Badminton', 'Basketball', 'Cricket',
'Football', 'Golf', 'Gymnastics',
'Hockey', 'Rugby', 'Snooker', 'Tennis'
],
fTemplate: function (e) {
return {
template: footVue
};
},
}
},
methods: {
onopen: function () {
var count = this.$refs.instance.getItems().length;
var ele = document.getElementsByClassName('foot')[0];
ele.innerHTML = "Total list item: " + count;
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
.foot {
text-indent: 1.2em;
display: block;
font-size: 15px;
line-height: 40px;
border-top: 1px solid #e0e0e0;
}
</style>
No records template
The AutoComplete is provided with support to custom design the popup list content when no data is found and no matches found on search with the help of noRecordsTemplate
property.
In the following sample, popup list content displays the notification of no data available.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' id='employees' :dataSource='data' :placeholder='waterMark'
:noRecordsTemplate='nTemplate'></ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
const nVue = createApp().component("noRecordsTemplate", {
template: `<span class="norecord"> NO DATA AVAILABLE</span>`,
data() {
return {
data: {}
};
}
});
const waterMark = 'Find an item';
const data = [];
const nTemplate = function (e) {
return {
template: nVue
};
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
;
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' id='employees' :dataSource='data' :placeholder='waterMark'
:noRecordsTemplate='nTemplate'>
</ejs-autocomplete>
</div>
</div>
</template>
<script>
import { createApp } from "vue";
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
const nVue = createApp().component("noRecordsTemplate", {
template: `<span class="norecord"> NO DATA AVAILABLE</span>`,
data() {
return {
data: {}
};
}
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
data() {
return {
waterMark: 'Find an item',
data: [],
nTemplate: function (e) {
return {
template: nVue
};
},
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>
Action failure template
There is also an option to custom design the popup list content when the data fetch request fails at the remote server. This can be achieved using the actionFailureTemplate property.
In the following sample, when the data fetch request fails, the AutoComplete displays the notification.
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' :fields='fields' :query='query' id='employees' :dataSource='data'
:placeholder='waterMark' :actionFailureTemplate='actionTemplate'></ejs-autocomplete>
</div>
</div>
</template>
<script setup>
import { createApp } from "vue";
import { AutoCompleteComponent as EjsAutocomplete } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const actionVue = createApp().component("actionFailureTemplate", {
template: `<span class="action-failure"> Data fetch got failed</span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svcs',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
const waterMark = 'Find an employee';
const data = remoteData;
const actionTemplate = function (e) {
return {
template: actionVue
};
};
const query = new Query().from('Employees').select(['FirstName']).take(6);
const fields = { value: 'FirstName' };
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>
<template>
<div id="app">
<div class='autocomplete'>
<ejs-autocomplete ref='instance' :fields='fields' :query='query' id='employees' :dataSource='data'
:placeholder='waterMark' :actionFailureTemplate='actionTemplate'></ejs-autocomplete>
</div>
</div>
</template>
<script>
import { createApp } from "vue";
import { AutoCompleteComponent } from '@syncfusion/ej2-vue-dropdowns';
import { Query, DataManager, ODataV4Adaptor } from '@syncfusion/ej2-data';
const actionVue = createApp().component("actionFailureTemplate", {
template: `<span class="action-failure"> Data fetch got failed</span>`,
data() {
return {
data: {}
};
}
});
const remoteData = new DataManager({
url: 'https://services.odata.org/V4/Northwind/Northwind.svcs',
adaptor: new ODataV4Adaptor,
crossDomain: true
});
export default {
name: "App",
components: {
"ejs-autocomplete": AutoCompleteComponent
},
data() {
return {
waterMark: 'Find an employee',
data: remoteData,
actionTemplate: function (e) {
return {
template: actionVue
};
},
query: new Query().from('Employees').select(['FirstName']).take(6),
fields: { value: 'FirstName' }
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-vue-dropdowns/styles/material.css";
#app {
color: #008cff;
height: 40px;
position: absolute;
width: 90%;
top: 10%;
}
.autocomplete {
width: 30%;
margin: 0 auto;
}
</style>