Search results

Data Source

The DropDownList loads the data either from local data sources or remote data services using the DataSource property. It supports the data type of array or DataManager.

The DropDownList also supports different kinds of data services such as OData, OData V4, and Web API, and data formats such as XML, JSON, and JSONP with the help of DataManager adaptors.

Fields Type Description
Text string Specifies the display text of each list item.
Value int or string Specifies the hidden data value mapped to each list item that should contain a unique value.
GroupBy string Specifies the category under which the list item has to be grouped.
IconCss string Specifies the icon class of each list item.

When binding complex data to the DropDownList, fields should be mapped correctly. Otherwise, the selected item remains undefined.

Binding local data

Local data can be represented in two ways as described below.

Array of JSON data

The DropDownList can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the Fields property.

In the following example, Name column from complex data have been mapped to the Value field.

@using Syncfusion.EJ2.Blazor.DropDowns

<EjsDropDownList TValue="string" Placeholder="e.g. Australia" DataSource="@Country">
    <DropDownListFieldSettings Text="Name" Value="Code"></DropDownListFieldSettings>
</EjsDropDownList>

@code {

    public class Countries
    {
        public string Name { get; set; }

        public string Code { get; set; }
    }

    List<Countries>Country = new List<Countries>
    {
        new Countries() { Name = "Australia", Code = "AU" },
        new Countries() { Name = "Bermuda", Code = "BM" },
        new Countries() { Name = "Canada", Code = "CA" },
        new Countries() { Name = "Cameroon", Code = "CM" },
        new Countries() { Name = "Denmark", Code = "DK" },
        new Countries() { Name = "France", Code = "FR" },
        new Countries() { Name = "Finland", Code = "FI" },
        new Countries() { Name = "Germany", Code = "DE" },
        new Countries() { Name = "Greenland", Code = "GL" },
        new Countries() { Name = "Hong Kong", Code = "HK" },
        new Countries() { Name = "India", Code = "IN" },
        new Countries() { Name = "Italy", Code = "IT" },
        new Countries() { Name = "Japan", Code = "JP" },
        new Countries() { Name = "Mexico", Code = "MX" },
        new Countries() { Name = "Norway", Code = "NO" },
        new Countries() { Name = "Poland", Code = "PL" },
        new Countries() { Name = "Switzerland", Code = "CH" },
        new Countries() { Name = "United Kingdom", Code = "GB" },
        new Countries() { Name = "United States", Code = "US" },
    };
}

The output will be as follows.

DropdownList

Array of complex data

The DropDownList can generate its list items through an array of complex data. For this, the appropriate columns should be mapped to the Fields property.

In the following example, Code.ID column and Country.CountryID column from complex data have been mapped to the Value field and Text field, respectively.

@using Syncfusion.EJ2.Blazor.DropDowns

<EjsDropDownList TValue="string" Placeholder="e.g. Select a country" DataSource="@LocalData">
    <DropDownListFieldSettings Text="Country.CountryID" Value="Code.ID"></DropDownListFieldSettings>
</EjsDropDownList>

@code {

public object LocalData { get; set; } = new Complex().GetData();

    public class Code
    {
        public string ID { get; set; }
    }

    public class Country
    {
        public string CountryID { get; set; }
    }

    public class Complex
    {
        public Country Country { get; set; }
        public Code Code { get; set; }
        public List<Complex>GetData() {
             List<Complex>Data = new List<Complex>();
            Data.Add(new Complex() { Country = new Country() { CountryID = "Australia" }, Code = new Code() { ID = "AU" } });
            Data.Add(new Complex() { Country = new Country() { CountryID = "Bermuda" }, Code = new Code() { ID = "BM" } });
            Data.Add(new Complex() { Country = new Country() { CountryID = "Canada" }, Code = new Code() { ID = "CA" } });
            Data.Add(new Complex() { Country = new Country() { CountryID = "Cameroon" }, Code = new Code() { ID = "CM" } });
            Data.Add(new Complex() { Country = new Country() { CountryID = "Denmark" }, Code = new Code() { ID = "DK" } });
            Data.Add(new Complex() { Country = new Country() { CountryID = "France" }, Code = new Code() { ID = "FR" } });
            return Data;
         }
     }
}

The output will be as follows.

DropdownList

Binding remote data

The DropDownList supports retrieval of data from remote data services with the help of DataManager control. The Query property is used to fetch data from the database and bind it to the DropDownList.

The following sample displays the first 6 contacts from Customers table of the Northwind Data Service.

@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.DropDowns

<EjsDropDownList TValue="string" Placeholder="Select a customer" Query="@Query">
    <EjsDataManager Url="https://services.odata.org/V4/Northwind/Northwind.svc/" Adaptor="Adaptors.ODataV4Adaptor" CrossDomain=true></EjsDataManager>
    <DropDownListFieldSettings Text="ContactName" Value="CustomerID"></DropDownListFieldSettings>
</EjsDropDownList>

@code {
    public string Query = "new ej.data.Query().from('Customers').select(['ContactName', 'CustomerID']).take(6).requiresCount()";
}

The output will be as follows.

DropdownList

Web API Adaptor

Use the WebApiAdaptor to bind DropDownList with Web API created using OData.

@using Syncfusion.EJ2.Blazor.Data
@using Syncfusion.EJ2.Blazor.DropDowns

<EjsDropDownList TValue="string" Placeholder="Select a Employee" Query="@Query">
    <EjsDataManager Url="https://ej2services.syncfusion.com/production/web-services/api/Employees" Adaptor="Adaptors.WebApiAdaptor" CrossDomain=true></EjsDataManager>
    <DropDownListFieldSettings Text="FirstName" Value="EmployeeID"></DropDownListFieldSettings>
</EjsDropDownList>

@code {
    public string Query = "new ej.data.Query().select(['FirstName', 'EmployeeID']).take(6).requiresCount()";
}

The output will be as follows.

DropdownList

Custom Adaptor

The EjsDataManager has custom adaptor support which allows you to perform manual operations on the data. This can be utilized for implementing custom data binding and editing operations in the DropDownList component.

For implementing custom data binding in DropDownList, the DataAdaptor class is used. This abstract class acts as a base class for the custom adaptor.

The DataAdaptor abstract class has both synchronous and asynchronous method signatures which can be overridden in the custom adaptor. Following are the method signatures present in this class,

public abstract class DataAdaptor
{
    /// <summary>
    /// Performs data Read operation synchronously.
    /// </summary>
    public virtual object Read(DataManagerRequest dataManagerRequest, string key = null)

    /// <summary>
    /// Performs data Read operation asynchronously.
    /// </summary>
    public virtual Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string key = null)
}

The custom data binding can be performed in the DropDownList component by providing the custom adaptor class and overriding the Read or ReadAsync method of the DataAdaptor abstract class.

The following sample code demonstrates implementing custom data binding using custom adaptor,

<EjsDropDownList TValue="string">
    <EjsDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></EjsDataManager>
    <DropDownListFieldSettings Value="CustomerID"></DropDownListFieldSettings>
</EjsDropDownList>

@code{

    public class Orders
    {
        public Orders() { }
        public Orders(int OrderID, string CustomerID)
        {
            this.OrderID = OrderID;
            this.CustomerID = CustomerID;
        }
        public int OrderID { get; set; }
        public string CustomerID { get; set; }
    }
    public class CustomAdaptor : DataAdaptor
    {
        static readonly HttpClient client = new HttpClient();
        public static List<OrdersDetails> order = OrdersDetails.GetAllRecords();
        public override object Read(DataManagerRequest dm, string key = null)
        {
            IEnumerable<OrdersDetails> DataSource = order;
            if (dm.Search != null && dm.Search.Count > 0)
            {
                DataSource = DataOperations.PerformSearching(DataSource, dm.Search);  //Search
            }
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting
            {
                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);
            }
            if (dm.Where != null && dm.Where.Count > 0) //Filtering
            {
                DataSource = DataOperations.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator);
            }
            int count = DataSource.Cast<OrdersDetails>().Count();
            if (dm.Skip != 0)
            {
                DataSource = DataOperations.PerformSkip(DataSource, dm.Skip);         //Paging
            }
            if (dm.Take != 0)
            {
                DataSource = DataOperations.PerformTake(DataSource, dm.Take);
            }
            return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;
       }
    }
}

Offline mode

To avoid post back for every action, set the DropDownList to load all data on initialization and make the actions process in client-side. To enable this behaviour, use the Offline property of DataManager.

The following example for remote data binding and enabled offline mode.

<EjsDropDownList TValue="string" Placeholder="Select a Employee" Query="@Query">
    <EjsDataManager Url="https://ej2services.syncfusion.com/production/web-services/api/Employees" Offline=true Adaptor="Adaptors.WebApiAdaptor" CrossDomain=true></EjsDataManager>
    <DropDownListFieldSettings Text="FirstName" Value="EmployeeID"></DropDownListFieldSettings>
</EjsDropDownList>

@code {
    public string Query = "new ej.data.Query().select(['FirstName', 'EmployeeID']).take(6).requiresCount()";
}

The output will be as follows.

DropdownList

Enum data binding

You can bind enum data to DropDownList component. The following code helps you get a string value from the enumeration data.

@using Syncfusion.EJ2.Blazor.DropDowns;

<EjsDropDownList Placeholder="e.g. Australia" DataSource="@EnumValues" @bind-Value="@ddlVal">
</EjsDropDownList>

@code{

    public string[] EnumValues = Enum.GetNames(typeof(Values));
    public Values ddlVal { get; set; } = Values.Canada;

    public enum Values
    {
        Australia,
        Bermuda,
        Canada,
        Denmark,
        India,
        US

    }
}

The output will shown as follows,

Enum data