Templates in EJ 1 ASP.NET MVC ComboBox

18 Nov 2018 / 23 minutes to read

The ComboBox has been provided with several options to customize each list item, group title, selected value, header, and footer elements.

Item template

The content of each list item within the ComboBox can be customized with the help of ItemTemplate property.

In the following sample, each list item is split into two columns to display the relevant data.

  • html
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("selectCountry")
                        .Width("100%")
                        .Datasource((IEnumerable<empList>)ViewBag.datasource)
                        .ComboBoxFields(h=>h.Text("text"))                    
                        .HeaderTemplate("<div class=\"head\">  Photo  <span style=\"padding-left:42px\"> Contact Info </span></div>")
                        .ItemTemplate("<div><img class=\"eimg\" src=\"../Images/combobox/${eimg}.png\" alt=\"employee\"/><div class=\"ename\"> ${text} </div><div class=\"temp\"> ${country} </div></div>")
                         .FooterTemplate("<div class=\"Foot\"> Total Items Count: 5 </div>")
                        .Placeholder("Select a employee")
                        .Render();
                }
            </div>
        </div>
  • css
  • <style>
            
            .ename {
                font-weight: bold;
                display: block !important;
                opacity: .87;
            }
            
            .tempName {
                padding: 5px 42px;
                opacity: .87;
            }
            
            .temp {
                margin-top: -15px;
                opacity: .54;
            }
            
            .eimg {
                border-radius: 50%;
                padding: 10px 16px;
                width: 40px;
                height: 40px;
                float: left;
            }
            
            .tempImg {
                padding-bottom: 3px;
                border-radius: 50%;
                float: left;
            }
            
            .e-dropdownbase .e-list-item * {
                display: block;
            }
        </style>

    Output for item template ComboBox control is as follows.

    item_template

    Group template

    The group header title with appropriate sub-items are categorized that is customized 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.

  • html
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("selectCountry")
                        .Width("100%")
                        .Datasource((IEnumerable<empList>)ViewBag.datasource)
                        .ComboBoxFields(h=>h.Text("text").GroupBy("country"))                    
                        .ItemTemplate("<div><img class=\"eimg\" src=\"../Images/combobox/${eimg}.png\" alt=\"employee\"/><div class=\"ename\"> ${text} </div><div class=\"temp\"> ${country} </div></div>")
                        .GroupTemplate("<strong>${country}</strong>")
                        .Placeholder("Select a country")
                        .Render();
                }
            </div>
        </div>
  • css
  • <style>
            
            .ename {
                font-weight: bold;
                display: block !important;
                opacity: .87;
            }
            
            .tempName {
                padding: 5px 42px;
                opacity: .87;
            }
            
            .temp {
                margin-top: -15px;
                opacity: .54;
            }
            
            .eimg {
                border-radius: 50%;
                padding: 10px 16px;
                width: 40px;
                height: 40px;
                float: left;
            }
            
            .tempImg {
                padding-bottom: 3px;
                border-radius: 50%;
                float: left;
            }
            
            .e-dropdownbase .e-list-item * {
                display: block;
            }
        </style>

    Output for group template ComboBox control is as follows.

    group_template

    Header template

    The header element is shown statically at the top of the popup list items within the ComboBox, 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
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("selectCountry")
                        .Width("100%")
                        .Datasource((IEnumerable<empList>)ViewBag.datasource)
                        .ComboBoxFields(h=>h.Text("text"))                    
                        .ItemTemplate("<div><img class=\"eimg\" src=\"../Images/combobox/${eimg}.png\" alt=\"employee\"/><div class=\"ename\"> ${text} </div><div class=\"temp\"> ${country} </div></div>")
                        .HeaderTemplate("<div class=\"head\">  Photo  <span style=\"padding-left:42px\"> Contact Info </span></div>")
                        .FooterTemplate("<div class=\"Foot\"> Total Items Count: 5 </div>")
                        .Placeholder("Select a country")
                        .Render();
                }
            </div>
        </div>
  • css
  • <style>
             .head {
                background-color: #a9a9a9;
                height: 30px;
                font-weight: bold;
                padding: 14px 0 0 20px;
            }
            
            
            .ename {
                font-weight: bold;
                display: block !important;
                opacity: .87;
            }
            
            .tempName {
                padding: 5px 42px;
                opacity: .87;
            }
            
            .temp {
                margin-top: -15px;
                opacity: .54;
            }
            
            .eimg {
                border-radius: 50%;
                padding: 10px 16px;
                width: 40px;
                height: 40px;
                float: left;
            }
            
            .tempImg {
                padding-bottom: 3px;
                border-radius: 50%;
                float: left;
            }
            
            .e-dropdownbase .e-list-item * {
                display: block;
            }
        </style>

    Output for item template ComboBox control is as follows.

    header_template

    The ComboBox has an option 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 ComboBox.

  • html
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("selectCountry")
                        .Width("100%")
                        .Datasource((IEnumerable<empList>)ViewBag.datasource)
                        .ComboBoxFields(h=>h.Text("text"))                    
                        .ItemTemplate("<div><img class=\"eimg\" src=\"../Images/combobox/${eimg}.png\" alt=\"employee\"/><div class=\"ename\"> ${text} </div><div class=\"temp\"> ${country} </div></div>")
                         .FooterTemplate("<div class=\"Foot\"> Total Items Count: 5 </div>")                    
                        .Placeholder("Select a employee")
                        .Render();
                }
            </div>
        </div>
  • css
  • <style>
            
            .Foot {
                background-color: #dadada;
                vertical-align: middle;
                padding: 16px;
                font-weight: bold;
            }
    
            .ename {
                font-weight: bold;
                display: block !important;
                opacity: .87;
            }
            
            .tempName {
                padding: 5px 42px;
                opacity: .87;
            }
            
            .temp {
                margin-top: -15px;
                opacity: .54;
            }
            
            .eimg {
                border-radius: 50%;
                padding: 10px 16px;
                width: 40px;
                height: 40px;
                float: left;
            }
            
            .tempImg {
                padding-bottom: 3px;
                border-radius: 50%;
                float: left;
            }
            
            .e-dropdownbase .e-list-item * {
                display: block;
            }
        </style>

    Output for footer template ComboBox control is as follows.

    footer_template

    No records template

    The ComboBox is provided with the 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.

  • html
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("searchCustomer")
                        .Datasource(d => d.URL("//js.syncfusion.com/ej/ejServices/wcf/NorthWind.svc/").Offline(false).CrossDomain(true))
                        .Query("ej.Query().from('Suppliers').select('SupplierID', 'ContactName').take(0)")
                        .ComboBoxFields(f => f.Text("ContactName").Value("SupplierID"))
                        .Width("100%")
                        .NoRecordsTemplate("<span class='norecord'> NO DATA AVAILABLE</span>")
                        .Placeholder("Search a customer")
                        .Render();
                }
            </div>
        </div>
  • c#
  • public ActionResult Databinding()
            {
                return View();
            }

    Output for no records template ComboBox control is as follows.

    no_records_template

    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 by using the ActionFailureTemplate property.

    In the following sample, when the data fetch request fails, the ComboBox displays the notification.

  • html
  • <div class="frame">
            <div class="control">
                @{
                    Html.EJ()
                        .ComboBox("searchCustomer")
                        .Datasource(d => d.URL("//js.syncfusion.com/ej/ejServices/wcf/NorthWind.svc/").Offline(false).CrossDomain(true))
                        .Query("ej.Query().from('Suppliers').select('SupplierID', 'ContactName')")
                        .ComboBoxFields(f => f.Text("ContactName").Value("SupplierID"))
                        .Width("100%")
                        .ActionFailureTemplate("<span class='action-failure'>Data fetch get fails</span>")
                        .Placeholder("Search a customer")
                        .Render();
                }
            </div>
        </div>
  • c#
  • public ActionResult Databinding()
            {
                return View();
            }