Getting Started with TypeScript DropDownList

8 Jun 20238 minutes to read

The external script dependencies of the DropDownList widget are,

And the internal script dependencies of the DropDownList widget are:

File  Description / Usage
ej.core.min.js Must be referred always before using all the JS controls.
ej.data.min.js Used to handle data operation and should be used while binding data to JS controls.
ej.dropdownlist.min.js The dropdownlist’s main file
ej.checkbox.min.js Should be referred when using checkbox functionalities in DropDownList.
ej.scroller.min.js Should be referred when using scrolling in DropDownList.
ej.draggable.min.js Should be referred when using popup resize functionality in DropDownList.

For getting started you can use the ‘ej.web.all.min.js’ file, which encapsulates all the ‘ej’ controls and frameworks in one single file.

For themes, you can use the ‘ej.web.all.min.css’ CDN link from the snippet given. To add the themes in your application, please refer this link.

Creating DropDownList in Typescript

Create an HTML page and add the scripts references in the order mentioned in the following code example.

  • HTML
  • <!DOCTYPE html>
          <html>
          <head>
            <link href="http://cdn.syncfusion.com/24.2.3/js/web/bootstrap-theme/ej.web.all.min.css" rel="stylesheet" />
            <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
            <script src="http://cdn.syncfusion.com/24.2.3/js/web/ej.web.all.min.js" type="text/javascript"></script>
            <script src="app.js"></script> 
          </head>
          <body>
          </body>
          </html>

    The DropDownList can be created from a HTML ‘input’ element with the HTML ‘id’ attribute and pre-defined options set to it. To create the DropDownList, you should call the ‘ejDropDownList’ jQuery plug-in function.

  • HTML
  • <div class="control">
            <input type="text" id="dropdown1" />
        </div>
  • HTML
  • /// <reference path="tsfiles/jquery.d.ts" />
         /// <reference path="tsfiles/ej.web.all.d.ts" />
         module DropDownListComponent {
           var items = [
            { text: "ListItem 1", value: "item 1" }, { text: "ListItem 2", value: "item 2" }, { text: "ListItem 3", value: "item 3"},
            { text: "ListItem 4", value: "item 4" }, { text: "ListItem 5", value: "item 5" }];
            $(function () {
            var sample = new ej.DropDownList($("#dropdown1"), {
                dataSource: items,
                fields: {text: "text", value: "value" }
            });
            
              });
           }

    TypeScript DropDownList Populating data

    Populating data

    The DropDownList can be bounded to both local array and remote data services .You can bind data to DropDownList through services or local data using dataSource property

  • HTML
  • <div class="control">
            <input type="text" id="dropdown1" />
        </div>
  • HTML
  • /// <reference path="tsfiles/jquery.d.ts" />
         /// <reference path="tsfiles/ej.web.all.d.ts" />
          module DropDownListComponent {
            $(function () {
             var customers = [
            { id: "1", text: "ALFKI" }, { id: "2", text: "ANATR" }, { id: "3", text: "ANTON" },
            { id: "4", text: "AROUT" }, { id: "5", text: "BERGS" }, { id: "6", text: "BLAUS" }
              ];
    
        var sample = new ej.DropDownList($("#dropdown1"), {
            dataSource: new ej.DataManager(customers),
            fields: { id: "id", text: "text", value: "text" }
        });
        
          }

    TypeScript DropDownList Setting Dimensions

    Setting Dimensions

    DropDownList dimensions can be set using width and height API.

  • HTML
  • /// <reference path="tsfiles/jquery.d.ts" />
         /// <reference path="tsfiles/ej.web.all.d.ts" />
    	
    	module DropDownListComponent {
        var items = [
            { text: "ListItem 1", value: "item 1" }, { text: "ListItem 2", value: "item 2" }, { text: "ListItem 3", value: "item 3"},
            { text: "ListItem 4", value: "item 4" }, { text: "ListItem 5", value: "item 5" }];
    
        $(function () {
            var sample = new ej.DropDownList($("#dropdown1"), {
                dataSource: items,
                fields: { text: "text", value: "value" },
                width: "200px",
                height:"50px"
                   });
            
              });
          }

    Setting dimensions to Popup list

    PopupWidth and popupHeight can be used to create a fixed size popup list.

  • HTML
  • /// <reference path="tsfiles/jquery.d.ts" />
         /// <reference path="tsfiles/ej.web.all.d.ts" />
    	module DropDownListComponent {
        var items = [
            { text: "ListItem 1", value: "item 1" }, { text: "ListItem 2", value: "item 2" }, { text: "ListItem 3", value: "item 3"},
            { text: "ListItem 4", value: "item 4" }, { text: "ListItem 5", value: "item 5" }];
    
        $(function () {
            var sample = new ej.DropDownList($("#dropdown1"), {
                dataSource: items,
                fields: { text: "text", value: "value" },
                width: "200px",
                height: "50px",
                popupHeight: "100px",
                popupWidth:"200px"
                   });
            
               });
          }

    Setting and Getting Value

    You can select single or multiple values from DropDownList widget. To assign a value initially to the DropDownList, you can use value property.

    NOTE

    To select multiple items based on index, refer here.

  • HTML
  • /// <reference path="tsfiles/jquery.d.ts" />
         /// <reference path="tsfiles/ej.web.all.d.ts" />
    	
         module DropDownListComponent {
          var items = [
            { text: "ListItem 1", value: "item 1" }, { text: "ListItem 2", value: "item 2" }, { text: "ListItem 3", value: "item 3"},
            { text: "ListItem 4", value: "item 4" }, { text: "ListItem 5", value: "item 5" }];
    
          $(function () {
            var sample = new ej.DropDownList($("#dropdown1"), {
                dataSource: items,
                fields: { text: "text", value: "value" },
                value: "item 3",
                change: function ()
                {
                    var obj = $('#dropdown1').data("ejDropDownList");
    
                    console.log("Selected Item's Text - " + obj.option("text"));
    
                    console.log("selected Item's Value - " + obj.option("value"));  
                }
             });
            
           });
    
    
         }