Adding a Drop-Down List to Your Angular Application | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (219)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (918)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (149)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (632)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (596)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Adding a Drop-Down List

Adding a Drop-Down List to Your Angular Application

In this blog post, we will look at the Angular drop-down list component in Essential JS 2. You will learn how to get started with a simple drop-down list and move on to enabling several of its features by extending the sample with simple code snippets.

The drop-down list is a text box component that allows a single, non-editable value to be selected from a list of predefined values.

The Essential JS 2 Angular drop-down list component is typically used in an application when you have more than 5 to 10 options and you want to allow users to pick one of them. It has been implemented with features your end users expect, such as searching (including support for diacritical marks), item grouping, cascading selection, accessibility, templating.

Get started with the drop-down list component

The following steps will guide you through the necessary configuration for the drop-down list component in an Angular application.

    1.  Clone the basic Angular sample using following commands.
git clone https://github.com/angular/quickstart.git quickstart
cd quickstart
npm install
    1. Install the drop-down list dependencies which are required to render the component in the Angular environment.
npm install @syncfusion/ej2-ng-dropdowns --save
    1. Map the Syncfusion ej2-ng-dropdowns dependency packages in the systemjs.config.js configuration file.
/**
 * System configuration for Angular samples.
 * Adjust as necessary for your application needs.
 */
(function (global) {
System.config({
    map: {
        // our app is within the app folder
        app: 'app',
        // angular bundles
        // syncfusion bundles
        '@syncfusion/ej2-base': 'syncfusion:ej2-base/dist/ej2-base.umd.min.js',
        '@syncfusion/ej2-data': 'syncfusion:ej2-data/dist/ej2-data.umd.min.js',
        '@syncfusion/ej2-inputs': 'syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js',
        '@syncfusion/ej2-popups': 'syncfusion:ej2-popups/dist/ej2-popups.umd.min.js',
        '@syncfusion/ej2-lists': 'syncfusion:ej2-lists/dist/ej2-lists.umd.min.js',
        '@syncfusion/ej2-buttons': 'syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js',
        '@syncfusion/ej2-dropdowns': 'syncfusion:ej2-dropdowns/dist/ej2-dropdowns.umd.min.js',
        '@syncfusion/ej2-ng-base': 'syncfusion:ej2-ng-base/dist/ej2-ng-base.umd.min.js',
        '@syncfusion/ej2-ng-dropdowns': 'syncfusion:ej2-ng-dropdowns/dist/ej2-ng-dropdowns.umd.min.js'
    }
  });
})(this);
    1. Import DropDownListModule into the Angular application (app.module.ts) from the package @syncfusion/ej2-ng-dropdowns.
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { DropDownListModule } from '@syncfusion/ej2-ng-dropdowns';

@NgModule({
  imports:      [ BrowserModule, DropDownListModule ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
    1. Modify the template in the app.component.ts file to render the ejs-dropdownlist component and also include the component theme from the corresponding base package @syncfusion/ej2-dropdowns component through styleUrls.
import { Component, ViewEncapsulation  } from '@angular/core';
@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: ``,
})
export class AppComponent  { name = 'Angular'; }
    1. Import the drop-down list combined CSS files from the Syncfusion package root folder. This can be referenced in your src/styles/styles.css file.
@import '../../node_modules/@syncfusion/ej2-dropdowns/styles/material.css';

Populate data

After initializing, populate the data in the drop-down list using the dataSource property. Here, the retrieval of data from remote data services is performed with the help of the data manager component, so we need to import Query, DataManager, and ODataAdaptor from the ej2-data package.

import { Query, DataManager, ODataAdaptor } from '@syncfusion/ej2-data'
@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [placeholder]='text' [query]='query' [sortOrder]='sorting'></ejs-dropdownlist>`,

})
export class AppComponent  { 
name = 'Angular'; 
     //bind the DataManager instance to dataSource property
    public data: DataManager = new DataManager({
        url: 'https://js.syncfusion.com/demos/ejServices/Wcf/Northwind.svc/',
        adaptor: new ODataAdaptor,
        crossDomain: true
    });
    // maps the appropriate column to fields property
    public fields: Object = { text: 'ContactName', value: 'CustomerID' };
    //bind the Query instance to query property
    public query: Query = new Query().from('Customers').select(['ContactName', 'CustomerID']).take(6);
    //set the placeholder text for drop-down list input
    public text: string = "Select a customer";
}

Note: The drop-down list loads data either from local data sources or remote data services using the dataSource property.

Group the drop-down list items

We can arrange logically related items as groups in the drop-down list. A group’s header can be displayed both inline and statically above groups of items. To display the grouping header, map the groupBy field on data binding.

Drop-Down List with Grouping

Filter data

When you want to show more options in a drop-down list, users may encounter some difficulty in finding and selecting the option they want. The drop-down list has built-in filtering to solve this problem. It is enabled by setting allowFiltering as true.

@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [allowFiltering]='true' [placeholder]='text' [query]='query' [sortOrder]='sorting'></ejs-dropdownlist>`,
})

Note: You can also apply custom logic to filter items using the filtering event.

Sort data

Sorting allows users to select the correct item from the list of options by arranging the items in ascending or descending order. Use the sortOrder setting and the value Descending or Ascending to enable sorting.

@Component({
  selector: 'my-app',
   // specifies the template string for the DropDownList component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' sortOrder='Descending' [placeholder]='text' [query]='query'></ejs-dropdownlist>`
})

Drop-Down List Sorted in Descending Order

Customize appearance using templates

The Essential JS 2 drop-down list has template options for customizing the default UI based on your needs. You can customize all the elements in the drop-down list, such as items, values, headers, and footers.

Item template

The content of each list item within the drop-down list can be customized with the help of the itemTemplate property.

@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [allowFiltering]='true' [placeholder]='text' [query]='query'>
                <ng-template #itemTemplate="" let-data="">
                      <!--set the value to itemTemplate property-->
                      <span><span class='name'> {{data.ContactName}}</span>  <span class ='city'><i><sub> -- {{data.City}}</sub></i></span></span>
                </ng-template>
             </ejs-dropdownlist>`
})

Drop-Down List with Item Template

Value template

The style of the selected value can be customized using the valueTemplate property.

@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [allowFiltering]='true' [placeholder]='text' [query]='query'>
                  <ng-template #valueTemplate="" let-data="">
                    <!--set the value to valueTemplate property-->
                    <span class='value'>{{data.ContactName}}  <i><sub> - {{data.City}}</sub></i></span>
                 </ng-template>
            </ejs-dropdownlist>`
})

Drop-Down List with Value Template

Header template

The header element is shown statically at the top of the pop-up, and any custom element can be placed as a header element using the headerTemplate property.

@Component({
  selector: 'my-app',
   // specifies the template string for the drop-down list component
  template: `<ejs-dropdownlist id='ddlelement' #samples [dataSource]='data' [fields]='fields' [allowFiltering]='true' [placeholder]='text' [query]='query'>
                 <ng-template #headerTemplate="" let-data="">
                    <!--set the value to headerTemplate property-->
                    <span class='head'><span class='name'>Name</span><span class='city'>City</span></span>
                 </ng-template>
            </ejs-dropdownlist>`
})

Drop-Down List with Header Template

Footer template

You can place any custom elment as a footer element of the drop-down list pop-up by using the footerTemplate property.

Drop-Down List with Footer Template

Note: The drop-down list also provides noRecordsTemplate, actionFailureTemplate, and groupTemplate. Refer to our documentation for more information.

Conclusion

We hope you found this a useful overview of the drop-down list and its important features. There are other advanced features you can explore in our documentation. If you would like to try the drop-down list component, you can download the free trial or play with our online demo. You can even check out the package on GitHub at https://github.com/syncfusion/ej2-angular-ui-components/tree/master/components/dropdowns. If you have any questions or feedback, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac.

Tags:

Share this post:

Comments (2)

this is very difficult method please tell some easy way

Hi Harkirat,

Yes, you can find the easy steps in the below user guidance document link.

https://ej2.syncfusion.com/angular/documentation/drop-down-list/getting-started/

Kindly check the above steps and get back to us if you need any further assistance on this.

Regards,
Saravanan G

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed