Data Binding

To populate data in the DropDownList widget, define dataSource property with associated fields. In DropDownList, can bind either local array or OData, WebApi and other RESTful services.

Fields

The below listed fields are the data collection fields which maps fields for the data items of the DropDownList.

Properties
Description
dataSource
The data source contains the list of data for generating the popup list items.
query
It specifies the query to retrieve the data from the online server.
fields
It specifies the mapping fields for the data items of the DropDownList widget.
id
It specifies the ID of the tag.
text
It specifies the text content of the tag.
value
It specifies the value of the tag.
groupBy
It is used to categorize the items based on a specific field.
imageUrl
It defines the image location.
imageAttributes
It defines the image attributes such as height, width, styles, etc.
spriteCssClass
It defines the sprite CSS for the image tag.
htmlAttributes
It defines the HTML attributes such as class and styles for an item.
selected
This field defines the tag value to be selected initially. Corresponding field mapped has Boolean values to select the list items on control creation. The data with value true in this field is selected automatically when the control is initialized with checkbox.
tableName
It defines the table name for the tag value or displays text while rendering remote data.

Local Data

Define a JSON array and initialize the widget with dataSource property. Specify the column names in the fields property.

NOTE

The columns are bounded automatically when the fields are specified with the default names like id, text, etc…

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="empList" [fields]="fieldsvalues">
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
       	empList: Array<Object>;
        fieldsvalues: Object;
        constructor() {
            this.empList = [{
                    text: "Erik Linden",
                    role: "Representative",
                    country: "England"
                }, {
                    text: "John Linden",
                    role: "Representative",
                    country: "Norway"
                }, {
                    text: "Louis",
                    role: "Representative",
                    country: "Australia"
                }, {
                    text: "Lawrence",
                    role: "Representative",
                    country: "India"
            }];
            this.fieldsvalues = { text: "text", value: "country", groupBy:"role" };
        }
    }

    The JSON array to the dataSource property can also be provided as an instance of the ej.DataManager. When the JSON array is passed as an instance of ej.DataManager, the ej.JsonAdaptor will be used to manipulate the DropDownList data source. The following code explains this behavior,

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data">
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
       	data: any;
        constructor() {
            var items = [{
                    text: "ListItem 1",
                    value: "item1"
                }, {
                    text: "ListItem 2",
                    value: "item2"
                }, {
                    text: "ListItem 3",
                    value: "item3"
                }, {
                    text: "ListItem 4",
                    value: "item4"
                }, {
                    text: "ListItem 5",
                    value: "item5"
            }];
           this.data = ej.DataManager(items);
        }
    }

    Binding Remote Data Service

    To bind remote data to the DropDownList, assign a service data as an instance of ejDataManager to the dataSource property.

    OData

    OData is a standardized protocol for creating and consuming data. Provide the OData service URL directly to the “ej.DataManager” class and then you can assign it to DropDownList “dataSource”.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" />
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        constructor() {
            this.data = ej.DataManager("http://mvc.syncfusion.com/Services/Northwnd.svc/Orders");
            this.fieldsvalues = { text: "ShipName", value: "ShipCountry" };
        }
    }

    OData Version 4

    The OData v4 is an improved version of OData protocols and the Data Manager can also retrieve and consume data from ODatav4 services.

    By using URL property of “ej.DataManager” bind OData Version 4 Service link and specify adaptor as ej.ODataV4Adaptor.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" />
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        constructor() {
            var dataManager = ej.DataManager({
                url: "http://services.odata.org/V4/Northwind/Northwind.svc/Regions/",
                adaptor: new ej.ODataV4Adaptor()
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "RegionDescription", value: "RegionID" };
        }
    }

    NOTE

    Events associated with remote data bind is listed here.

    WebAPI Binding

    Using ej.WebApiAdaptor, bind WebApi service’s data to DropDownList. The data from WebApi service must be returned as an object that has property “Items” with its value as data source and another property “Count” with its value as dataSource’s total records count.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" />
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        constructor() {
            var dataManager =  ej.DataManager({
            url: "/api/Orders",
            adaptor: new ej.WebApiAdaptor()
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "CustomerID", value: "CustomerID" };
        }
    }
  • c#
  • public class OrdersController : ApiController
        {
            // GET api/orders
            NorthwindDataContext db = new NorthwindDataContext();
            public PageResult<Order> Get(ODataQueryOptions opts)
            {
                List<Order> ord = db.Orders.ToList();
    
                return new PageResult<Order>(ord as IEnumerable<Order>, null, ord.Count);
            }
    
        }

    ASP.NET Web Method Binding

    The data can retrieve data from ASP.NET Web methods by making use of the WebMethod Adaptor of ejDataManager.

    The WebMethod Adaptor is used to bind data source from remote services and code behind methods.
    By using “WebMethodAdaptor” we can bind data from WebService to the DropDownList control and also we need to include “ScriptService” Attribute to WebService in order to enable request from client-side.

  • html
  • <span> Please select... </span>
    <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="dataList" [query]="queries" [fields]="fieldsvalues" />
  • c#
  • [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
            [WebMethod]
            public object Get()
            {
                List<Employee> EmpData = new List<Employee>();
                EmpData.Add(new Employee
                {
                    Name = "Erik Linden",
                    Role = "Executive"
                    
                });
                EmpData.Add(new Employee
                {
                    Name = "John Linden",
                    Role = "Representative"
                    
                });
                EmpData.Add(new Employee
                {
                    Name = "Louis",
                    Role = "Representative"
                    
                });
                EmpData.Add(new Employee
                {
                    Name = "Lawrence",
                    Role = "Executive"
                    
                });
                dynamic count = EmpData.Count;
                return new
                {
                    result = EmpData,
                    count = count
                };
    
            }
            public class Employee
            {
                public string Name { get; set; }
                public string Role { get; set; }
                
            }
        }
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        dataList: any;
        fieldsvalues: Object;
        queries: any;    
        constructor() {
             var data = ej.DataManager({
                url: "WebService1.asmx/Get",
                adaptor: new ej.WebMethodAdaptor()
            });
            var query = ej.Query().requiresCount();
            this.dataList = data;
            this.queries = query;
            this.fieldsvalues = { text: "Name", value: "Country" };
        }
    }

    Use the above code example to use WebMethod adaptor and bind the data to the DropDownList.

    MVC controller Action Binding

    The data can retrieve from MVC controller. This can be achieved by using the UrlAdaptor of ej.DataManager.

    Defines the List of Employee Data and converted into JSON object.

  • c#
  • using System.Web.Script.Serialization;
        public partial class DropdownlistController: Controller
        {
            public ActionResult DropdownlistFeatures()
            {
                return View();
             } 
            public class Employee
            {
                public string Name { get; set; }
                public string Role { get; set; }
            }
            public JsonResult getData()
            {
                List<Employee> EmpData = new List<Employee>();
                EmpData.Add(new Employee
                {
                    Name = "Erik Linden",
                    Role = "Executive"
                });
                EmpData.Add(new Employee
                {
                    Name = "John Linden",
                    Role = "Representative"
                });
                EmpData.Add(new Employee
                {
                    Name = "Louis",
                    Role = "Representative"
                });
                EmpData.Add(new Employee
                {
                    Name = "Lawrence",
                    Role = "Executive"
                });
                var jsonSerialiser = new JavaScriptSerializer();
                return Json(jsonSerialiser.Serialize(EmpData));
            }
        }

    In client side, specify the URL of Data to url property and specify the type of Adaptor in adaptor property of DataManager and initialize the DropDownList with dataSource property. Specify the column names in the fields property.

  • html
  • <div class="ctrllabel"> Select an Employee</div>
    <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" />
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        constructor() {
            var dataManager = ej.DataManager({
            url: "/Dropdownlist/getData",
            adaptor: new ej.UrlAdaptor()
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "Name", value: "Role" };
        }
    }

    Other Restful web services

    The Custom Adaptor concept of “ej.DataManager” allows to customize or generate your own adaptor which is used to process “query” and “result” data.
    When using remote data binding, the adaptor of “ej.DataManager” plays vital role in processing queries to make them suitable to sends along with data request and also process the response data from the server.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" />
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        constructor() {
            var customAdaptor = new ej.Adaptor().extend({
            insert: function (dm, data) {
            return dm.dataSource.json.push(data);
            },
            processQuery: ej.JsonAdaptor.prototype.processQuery
            // reused process query from json adaptor
            });
            
            window.dropdownData = [{
                FirstName: "John",
                LastName: "Abraham"
            }, {
                FirstName: "Ben",
                LastName: "Nick"
            }, {
                FirstName: "Andrew",
                LastName: "Clarke"
            }];
    
            var dataManager = new ej.DataManager(window.dropdownData);
    
            // assigning custom adaptor to datamanager
            dataManager.adaptor = new customAdaptor();
    
            // insert from custom adaptor usage
            dataManager.insert({
                FirstName: "Joel",
                LastName: "Nick"
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "FirstName", value: "LastName" };
        }
    }

    Virtual Scrolling

    To improve the performance when displaying large data set, you can use “allowVirtualScrolling” and virtualScrollMode property. This retrieves only a fixed amount of list items and loads remaining data on scrolling. The items will be fetched via AJAX request.

    This supports two modes of virtualization. They are,

    • Normal Mode
    • Continuous Mode

    IMPORTANT

    1. Sorting and Grouping is not supported with Virtual Scrolling
    2. “virtualScrollMode” property accepts both the string and ej.VirtualScrollMode enum value.

    Normal Mode

    It loads the data on scrolling the list of items. This can be achieved by setting normal value to the “virtualScrollMode” property.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" [itemsCount]="count" [allowVirtualScrolling]="true" [virtualScrollMode]="mode"/>
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        mode: any;
        count: number;
        constructor() {
            var dataManager = ej.DataManager({
                url: "http://mvc.syncfusion.com/services/Northwnd.svc/Orders"
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "ShipName", value: "ShipCountry" };
            this.mode = ej.VirtualScrollMode.Normal;
            this.count =7;
        }
    }

    Continuous Mode

    It loads the set of items when the scroller reaches at the end. This behaves like infinity scrolling. So when scroll reaches the end, it will fetch the remaining set of items and bind with your DropDownList. This can be achieved by setting continuous value to the “virtualScrollMode” property.

    NOTE

    In both modes, set of items will be fetched based on the count specified in the itemsCount property and next set of items will be loaded on scrolling.

  • html
  • <input type="text" id="dropdown1" ej-dropdownlist [dataSource]="data" [fields]="fieldsvalues" [itemsCount]="count" [allowVirtualScrolling]="true" [virtualScrollMode]="mode"/>
  • html
  • import {Component} from '@angular/core';
    @Component({
    selector: 'sd-home',
    templateUrl: 'app/components/dropdown/dropdown.component.html'
    })
    export class DropDownListComponent {
        data: any;
        fieldsvalues: Object;
        mode: any;
        count: number;
        constructor() {
            var dataManager = ej.DataManager({
                url: "http://mvc.syncfusion.com/services/Northwnd.svc/Orders"
            });
            this.data = dataManager;
            this.fieldsvalues = { text: "ShipName", value: "ShipCountry" };
            this.mode = ej.VirtualScrollMode.Continuous;
            this.count =7;
        }
    }