Templates of Drop Down List Component
8 Dec 202410 minutes to read
The DropDownList has been provided with several options to customize each list item, group title, selected value, 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 DropDownList can be customized with the help of itemTemplate property.
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(obj => obj.Url("https://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 Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
{
Value = "FirstName"
}).Query("new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)").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 DropDownListController : Controller
{
public ActionResult Itemtemplate()
{
return View();
}
}
}
Value template
The currently selected value that is displayed by default on the DropDownList input element can be customized using the valueTemplate property.
In the following sample, the selected value is displayed as a combined text of both FirstName
and City
in the DropDownList input, which is separated by a hyphen.
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
dataManger.Url("https://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ItemTemplate("<span><span class='name'>${FirstName}</span><span class ='city'>${City}</span></span>").ValueTemplate("<span>${FirstName} - ${City}</span>").Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
{
Value = "FirstName",
}).Query("new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)").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 DropDownListController : Controller
{
public ActionResult valuetemplate()
{
return View();
}
}
}
Group template
The group header title under which appropriate sub-items are categorized can also be customized with the help of groupTemplate property. This template is common for both inline and floating group header template.
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
dataManger.Url("https://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).GroupTemplate("<strong>${City}</strong>").Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
{
Value = "FirstName",
GroupBy = "City"
}).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'))").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 DropDownListController : Controller
{
public ActionResult grouptemplate()
{
return View();
}
}
}
Header template
The header element is shown statically at the top of the popup list items within the DropDownList, 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.
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
dataManger.Url("https://services.odata.org/V4/Northwind/Northwind.svc/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ItemTemplate("<span class='item' ><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 Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
{
Value = "FirstName",
}).Query("new ej.data.Query().from('Employees').select(['FirstName', 'City', 'EmployeeID']).take(6)").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 DropDownListController : Controller
{
public ActionResult headertemplate()
{
return View();
}
}
}
Footer template
The DropDownList has options to show a footer element at the bottom of the list items in the popup 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 DropDownList.
@Html.EJS().DropDownList("games").DataSource((IEnumerable<object>)ViewBag.data).Placeholder("Select a game").PopupHeight("270px").FooterTemplate("<span class='foot'> Total list items: " + 5 + "</span>").Render()
<style>
.foot {
text-indent: 1.2em;
display: block;
font-size: 15px;
line-height: 40px;
border-top: 1px solid #e0e0e0;
}
</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 DropDownListController : Controller
{
public ActionResult footertemplate()
{
ViewBag.data = new string[] { "Badminton", "Basketball", "Cricket", "Football", "Golf", "Gymnastics", "Hockey", "Tennis" };
return View();
}
}
}
No records template
The DropDownList is provided with support to custom design the popup list content when no data is found and no matches are found on search with the help of noRecordsTemplate property.
In the following sample, popup list content displays the notification of no data available.
@Html.EJS().DropDownList("games").Placeholder("Select a game").PopupHeight("200px").DataSource((IEnumerable<object>)ViewBag.data).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 DropDownListController : Controller
{
public ActionResult norecords()
{
ViewBag.data = new string[] { };
return View();
}
}
}
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 DropDownList displays the notification.
@Html.EJS().DropDownList("customers").Placeholder("Select a customer").PopupHeight("200px").DataSource(dataManger =>
dataManger.Url("https://services.odata.org/V4/Northwind/Northwind.svcs/").Adaptor("ODataV4Adaptor").CrossDomain(true)).ActionFailureTemplate("<span class='action-failure'> Data fetch get fails</span>").Fields(new Syncfusion.EJ2.DropDowns.DropDownListFieldSettings
{
Value = "FirstName"
}).Query("new ej.data.Query().from('Employees').select(['FirstName']).take(6)").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 DropDownListController : Controller
{
public ActionResult actionfailure()
{
return View();
}
}
}