Search results

Templates

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.

razor
itemtemplate.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ItemTemplate("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>").Fields(new AutoCompleteFieldSettings
            {
                Value = "FirstName",}).Query((string)ViewBag.query).Render()
<style>
    .city{
    right: 15px;
    position: absolute;
}
</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult Itemtemplate()
        {
            return View();
        }       
    }
}

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.

razor
grouptemplate.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).GroupTemplate("<strong>${City}</strong>").Fields(new AutoCompleteFieldSettings
            {
                Value = "FirstName",
                GroupBy = "City"
            }).Query((string)ViewBag.query).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult grouptemplate()
        {
            return View();
        }       
    }
}

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.

razor
headertemplate.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ItemTemplate("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>").HeaderTemplate("<span class='head'><span class='name'>Name</span><span class='city'>City</span></span>").Fields(new AutoCompleteFieldSettings
            {
                Value = "FirstName",
            }).Query((string)ViewBag.query).Render()
<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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult headertemplate()
        {
            return View();
        }       
    }
}

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.

razor
footertemplate.cs
@Html.EJS().AutoComplete("games").DataSource((IEnumerable<object>)ViewBag.localdata).Placeholder("Select an games").PopupHeight("270px").FooterTemplate("<span class='foot'> Total list items: " + 5 + "</span>").Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult footertemplate()
        {
            ViewBag.data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
            return View();
        }
    }
}

No records template

The AutoComplete is provided with support to custom design the suggestion list content when no data is found and no matches found on search with the help of noRecordsTemplate property.

In the following sample, suggestion list content displays the notification of no data available.

razor
norecordstemplate.cs
@Html.EJS().AutoComplete("games").Placeholder("Select a game").PopupHeight("200px").DataSource((IEnumerable<object>)ViewBag.localdata).NoRecordsTemplate("<span class='norecord'> NO DATA AVAILABLE</span>").Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult norecords()
        {
            ViewBag.data = new string[] { };
            return View();
        }
    }
}

Action failure template

There is also an option to custom design the suggestion 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.

razor
actionfailuretemplate.cs
@Html.EJS().AutoComplete("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
            dataManger.Url("http://services.odata.org/V4/Northwind/Northwind.svcs/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ActionFailureTemplate("<span class='action-failure'> Data fetch get fails</span>").Fields(new AutoCompleteFieldSettings
            {
                Text = "FirstName",
                Value = "EmployeeID"
            }).Query((string)ViewBag.query).Render()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class AutoCompleteController : Controller
    {
        public IActionResult actionfailure()
        {
            return View();
        }
    }
}