Getting Started with EJ2 JavaScript AutoComplete

23 Nov 202615 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Autocomplete and demonstrate the basic usage of the Autocomplete control in a JavaScript application.

Dependencies

The list of dependencies required to use the Autocomplete component in your application is given below:

|-- @syncfusion/ej2-dropdowns
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-notifications
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Setup for local environment

Refer to the following steps to set up your local environment.

Step 1: Create a root folder named my-app for your application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Autocomplete control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts

Add Syncfusion® AutoComplete CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base,ej2-data,ej2-popups,ej2-lists,ej2-buttons,ej2-inputs script is the required dependency package for the AutoComplete component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/auto-complete/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-data/dist/global/ej2-data.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-inputs/dist/global/ej2-inputs.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-lists/dist/global/ej2-lists.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-buttons/dist/global/ej2-buttons.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-dropdowns/dist/global/ej2-dropdowns.min.js

Adding AutoComplete component to the application

Add the input element and initiate the Essential® JS 2 AutoComplete component in the index.html by using following code.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>
         <!-- Essential JS 2 autoComplete's dependent bootstrap5.3 theme -->
          <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/auto-complete/index.css" rel="stylesheet" />

        <!-- Essential JS 2 all script -->
        <!-- <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script> -->

        <!-- Essential JS 2 autoComplete's dependent scripts -->
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/dist/global/ej2-dropdowns.min.js" type="text/javascript"></script>

   </head>
   <body>
       <!-- Add the HTML <input> element  -->
         <input type="text" id='atcelement' />
        <script>
            // initialize autoComplete component
           var autocomplete = new ej.dropdowns.AutoComplete();
            // Render initialized autoComplete.
            autocomplete.appendTo('#atcelement')
        </script>
   </body>
  </html>

Binding data source

After initialization, populate the AutoComplete with data by using the dataSource property. Here, an array of string values is passed to the AutoComplete component.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
            <title>Essential JS 2</title>
             <!-- Essential JS 2 autoComplete's dependent bootstrap5.3 theme -->
            <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/auto-complete/index.css" rel="stylesheet" />

            <!-- Essential JS 2 all script -->
            <!-- <script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script> -->

            <!-- Essential JS 2 autoComplete's dependent scripts -->
            <script src="//cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
            <script src="//cdn.syncfusion.com/ej2/ej2-dropdowns/dist/global/ej2-dropdowns.min.js" type="text/javascript"></script>
       </head>
       <body>
           <!-- Add the HTML <input> element  -->
             <input type="text" id='atcelement' />
            <script>
                let sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
                // initialize AutoComplete component
                var listObj = new ej.dropdowns.AutoComplete({
                //set the data to dataSource property
                    dataSource: sportsData,
                    });
                listObj.appendTo('#atcelement');
            </script>
       </body>
  </html>

Custom values

The AutoComplete allows users to enter custom values that are not in the predefined set. By default, this is enabled through the allowCustom property. Custom values are sent to the post-back handler when a form is submitted.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>
         <!-- Essential JS 2 autoComplete's dependent bootstrap5.3 theme -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/auto-complete/index.css" rel="stylesheet" />

        <!-- Essential JS 2 all script -->
        <!-- <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script> -->

        <!-- Essential JS 2 autoComplete's dependent scripts -->
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/dist/global/ej2-dropdowns.min.js" type="text/javascript"></script>
   </head>
   <body>
       <!-- Add the HTML <input> element  -->
         <input type="text" id='atcelement' />
        <script>
            let sportsData = [
            { Id: 'Game1', Game: 'Badminton' },
            { Id: 'Game2', Game: 'Basketball' },
            { Id: 'Game3', Game: 'Cricket' },
            { Id: 'Game4', Game: 'Football' },
            { Id: 'Game5', Game: 'Golf' },
            { Id: 'Game6', Game: 'Hockey' },
            { Id: 'Game7', Game: 'Rugby' },
            { Id: 'Game8', Game: 'Snooker' }
            ];
            // initialize AutoComplete component
            var listObj = new ej.dropdowns.AutoComplete({
                //set the data to dataSource property
                dataSource: sportsData,
                // By default, its enabled. For your better understanding, showcase this property.
                allowCustom: true,
                // maps the appropriate column to fields property
                fields: { value: 'Game' },
                // set placeholder to AutoComplete input element
                placeholder: "Find a game"});
            listObj.appendTo('#atcelement');
        </script>
   </body>
  </html>

Configure the suggestion list

By default, the suggestion list width automatically adjusts to the AutoComplete input element’s width, and the suggestion list height is 300px.

The height and width of the popup list can also be customized using the popupHeight and popupWidth properties, respectively.

In the following sample, suggestion list’s width and height are configured.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
   <head>
                
        <title>Essential JS 2</title>
        <!-- Essential JS 2 autoComplete's dependent bootstrap5.3 theme -->
        <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/auto-complete/index.css" rel="stylesheet" />

      <!-- Essential JS 2 all script -->
        <!-- <script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script> -->

        <!-- Essential JS 2 autoComplete's dependent scripts -->
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/dist/global/ej2-base.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-data/dist/global/ej2-data.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/dist/global/ej2-inputs.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/dist/global/ej2-buttons.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/dist/global/ej2-lists.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/dist/global/ej2-popups.min.js" type="text/javascript"></script>
        <script src="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/dist/global/ej2-dropdowns.min.js" type="text/javascript"></script>

   </head>
   <body>
       <!-- Add the HTML <input> element  -->
         <input type="text" id='atcelement' />
        <script>
            let sportsData = ['Badminton', 'Basketball', 'Cricket', 'Football', 'Golf', 'Gymnastics', 'Hockey', 'Rugby', 'Snooker', 'Tennis'];
            // initialize AutoComplete component
            var listObj = new ej.dropdowns.AutoComplete({
            //set the data to dataSource property
                dataSource: sportsData,
                //set width to suggestion list
                popupWidth: '250px',
                // set the popup list height
                popupHeight: "250px",
                // set placeholder to AutoComplete input element
                placeholder: "Find a game"});
            listObj.appendTo('#atcelement');
        </script>
   </body>
  </html>

See Also