Templates in ASP.NET CORE AutoComplete
27 Aug 20267 minutes to read
The AutoComplete provides several options to customize each list item, 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 the itemTemplate property.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)" placeholder="Select a customer" popupHeight="200px"
itemTemplate="@Html.Raw("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>")">
<e-autocomplete-fields value="FirstName"></e-autocomplete-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-autocomplete>
</div>
</div>
<style>
.city {
right: 15px;
position: absolute;
}
</style>Group template
The group header title under which appropriate sub-items are categorized can also be customized with the help of the groupTemplate property. This template is common for both inline and floating group header template.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6).where(new ej.data.Predicate('City', 'equal','london').or('City','equal','seattle'))" placeholder="Select a customer" popupHeight="200px"
groupTemplate="@Html.Raw("<strong>${City}</strong>")">
<e-autocomplete-fields groupBy="City" value="FirstName"></e-autocomplete-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-autocomplete>
</div>
</div>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 the 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.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="customers" query="new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)" placeholder="Find an Employee" popupHeight="200px"
headerTemplate="@Html.Raw("<span class='head'><span class='name'>Name</span><span class='city'>City</span></span>")"
itemTemplate="@Html.Raw("<span class='item' ><span class='name'>${FirstName}</span><span class='city'>${City}</span></span>")">
<e-autocomplete-fields value="FirstName"></e-autocomplete-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svc/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-autocomplete>
</div>
</div>
<style>
.head, .item {
display: table;
width: 100%;
margin: auto;
}
.head {
height: 40px;
font-size: 15px;
font-weight: 600;
}
.name, .city {
display: table-cell;
vertical-align: middle;
width: 50%;
}
.head .name {
text-indent: 16px;
}
.head .city {
text-indent: 10px;
}
</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 the footerTemplate property.
In the following sample, footer element displays the total number of list items present in the AutoComplete.
var data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="employees" dataSource="@data" placeholder="Select an game" popupHeight="270px"
footerTemplate="@Html.Raw( "<span class='foot'> Total list items: " + 8 + "</span>")">
<e-autocomplete-fields value="Name"></e-autocomplete-fields>
</ejs-autocomplete>
</div>
</div>
<style>
.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 customize the suggestion list content when no data is found and no matches are found on search with the help of the noRecordsTemplate property.
@{
var data = new string[] { };
}
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="employees" dataSource="@data" placeholder="Select an employee" popupHeight="270px"
noRecordsTemplate="@Html.Raw("<span class='norecord'> NO DATA AVAILABLE</span>")">
<e-autocomplete-fields value="Name"></e-autocomplete-fields>
</ejs-autocomplete>
</div>
</div>Action failure template
There is also an option to customize the suggestion list content when the data fetch request fails at the remote server. This can be achieved using the actionFailureTemplate property.
<div class='control-wrapper'>
<div style='padding-top:75px;'>
<ejs-autocomplete id="customers" query="new ej.data.Query().from('Employees').select(['FirstName']).take(6)" placeholder="Select a customer" popupHeight="200px"
actionFailureTemplate="@Html.Raw("<span class='action-failure'> Data fetch get fails</span>")">
<e-autocomplete-fields value="FirstName"></e-autocomplete-fields>
<e-data-manager url="https://services.odata.org/V4/Northwind/Northwind.svcs/" adaptor="ODataV4Adaptor" crossDomain="true"></e-data-manager>
</ejs-autocomplete>
</div>
</div>