Getting started in EJ2 TypeScript MultiColumn ComboBox control

14 Jun 202419 minutes to read

This section explains how to create a simple MultiColumn ComboBox control and configure its available functionalities in TypeScript, using Essential JS 2 quickstart seed repository.

This application is integrated with the webpack.config.js configuration and uses the latest version of the webpack-cli. It requires node v14.15.0 or higher. For more information about webpack and its features, refer to the webpack documentation.

Dependencies

The following list of dependencies are required to use the MultiColumn ComboBox control in your application.

|-- @syncfusion/ej2-multicolumn-combobox
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-data
    |-- @syncfusion/ej2-grids
    |-- @syncfusion/ej2-lists
    |-- @syncfusion/ej2-navigations
    |-- @syncfusion/ej2-inputs
    |-- @syncfusion/ej2-popups
        |-- @syncfusion/ej2-buttons

Set up development environment

Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.

git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart

After cloning the application in the ej2-quickstart folder, run the following command line to navigate to the ej2-quickstart folder.

cd ej2-quickstart

Add Syncfusion JavaScript packages

Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.

The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json file. Use the following command to install the dependent npm packages from the command prompt.

npm install

Import the Syncfusion CSS styles

To render MultiColumn ComboBox control, need to import dropdowns and its dependent components styles as given below in the ~/src/styles/styles.css file, as shown below:

@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-inputs/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-lists/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-grids/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-multicolumn-combobox/styles/material.css';

Initialize the MultiColumn ComboBox

The MultiColumn ComboBox can be initialized through three different tags select, UL and input element.

Add the HTML input element that needs to be initialized as MultiColumn ComboBox in index.html.

[src/index.html]

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 MultiColumn ComboBox control</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
    <meta name="description" content="Essential JS 2" />
    <meta name="author" content="Syncfusion" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
    <div id='container' style="margin:50px 0 auto; width:500px;">
        <!--element which is going to render the MultiColumn ComboBox-->
        <input type="text" tabindex="1" id='multicolumn' />
    </div>

</body>

</html>

Now, import the MultiColumn ComboBox component to your app.ts and initialize it to the element #multicolumn as shown below.

[src/app/app.ts]

import { MultiColumnComboBox } from '@syncfusion/ej2-multicolumn-combobox';

// initialize MultiColumn ComboBox component
let multiComboBoxObject: MultiColumnComboBox = new MultiColumnComboBox();

// render initialized MultiColumn ComboBox
multiComboBoxObject.appendTo('#multicolumn');

Binding data source with fields and columns

After initializing, populate the MultiColumn ComboBox with data by using the dataSource property, to map the data for each specified columns use the columns property and the fields property to map the data fields from the dataSource.

Here an array of object values is passed to the MultiColumn ComboBox control.

import { MultiColumnComboBox } from '@syncfusion/ej2-multicolumn-combobox';

// define the array of object data for datasource
let employeeData: {[key: string]: Object}[] = [
    { "EmpID": 1001, "Name": "Andrew Fuller", "Designation": "Team Lead", "Country": "England" },
    { "EmpID": 1002, "Name": "Robert", "Designation": "Developer", "Country": "USA" },
    { "EmpID": 1003, "Name": "John", "Designation": "Tester", "Country": "Germany" },
    { "EmpID": 1004, "Name": "Robert King", "Designation": "Product Manager", "Country": "India" },
    { "EmpID": 1005, "Name": "Steven Buchanan", "Designation": "Developer", "Country": "Italy" },
    { "EmpID": 1006, "Name": "Jane Smith", "Designation": "Developer", "Country": "Europe" },
    { "EmpID": 1007, "Name": "James Brown", "Designation": "Developer", "Country": "Australia" },
    { "EmpID": 1008, "Name": "Laura Callahan", "Designation": "Developer", "Country": "Africa" },
    { "EmpID": 1009, "Name": "Mario Pontes", "Designation": "Developer", "Country": "Russia" },
];

// define the column values for data columns
let columnsData: ColumnModel[] = [
    { field: 'EmpID', header: 'Employee ID', width: 120 },
    { field: 'Name', width: 120, header: 'Name' },
    { field: 'Designation', header: 'Designation', width: 120 },
    { field: 'Country', header: 'Country', width: 100 }
];

// initialize MultiColumn ComboBox component
let multiComboBoxObject: MultiColumnComboBox = new MultiColumnComboBox({
    //set the data to dataSource property
    dataSource: employeeData,
    //set the column data to the columns property
    columns: columnsData,
    //set the fields of the multicolumn combobox
    fields: { text: 'Name', value: 'EmpID' }
});

// render initialized MultiColumn ComboBox
multiComboBoxObject.appendTo('#multicolumn');

Run the application

After completing the configuration required to render a basic MultiColumn ComboBox, run the following command to display the output in your default browser.

npm run start

The following example illustrates the output in your browser.

import { MultiColumnComboBox, ColumnModel } from '@syncfusion/ej2-multicolumn-combobox';

// defined the array of object data
let empData: { [key: string]: Object }[] = [ 
    { "EmpID": 1001, "Name": "Andrew Fuller", "Designation": "Team Lead", "Country": "England" },
    { "EmpID": 1002, "Name": "Robert", "Designation": "Developer", "Country": "USA" },
    { "EmpID": 1003, "Name": "Michael", "Designation": "HR", "Country": "Russia" },
    { "EmpID": 1004, "Name": "Steven Buchanan", "Designation": "Product Manager", "Country": "Ukraine" },
    { "EmpID": 1005, "Name": "Margaret Peacock", "Designation": "Developer", "Country": "Egypt" },
    { "EmpID": 1006, "Name": "Janet Leverling", "Designation": "Team Lead", "Country": "Africa" },
    { "EmpID": 1007, "Name": "Alice", "Designation": "Product Manager", "Country": "Australia" },
    { "EmpID": 1008, "Name": "Bob", "Designation": "Developer", "Country": "India" },
    { "EmpID": 1009, "Name": "John", "Designation": "Product Manager", "Country": "Ireland"},
    { "EmpID": 1010, "Name": "Mario Pontes", "Designation": "Team Lead", "Country": "South Africa" },
    { "EmpID": 1011, "Name": "Yang Wang", "Designation": "Developer", "Country": "Russia" },
    { "EmpID": 1012, "Name": "David", "Designation": "Product Manager", "Country": "Egypt" },
    { "EmpID": 1013, "Name": "Antonio Bianchi", "Designation": "Team Lead", "Country": "USA" },
    { "EmpID": 1014, "Name": "Laura", "Designation": "Developer", "Country": "England" },
    { "EmpID": 1015, "Name": "Carlos Hernandez", "Designation": "Developer", "Country": "Canada" },
    { "EmpID": 1016, "Name": "Lily", "Designation": "Product Manager", "Country": "France" },
    { "EmpID": 1017, "Name": "Tom Williams", "Designation": "Developer", "Country": "Ukraine" },
    { "EmpID": 1018, "Name": "Grace", "Designation": "Developer", "Country": "Australia" },
    { "EmpID": 1019, "Name": "Olivia", "Designation": "Team Lead", "Country": "Ireland" },
    { "EmpID": 1020, "Name": "James", "Designation": "Developer", "Country": "China" },
];

let columnsData: ColumnModel[] = [
    { field: 'EmpID', header: 'Employee ID', width: 120 },
    { field: 'Name', header: 'Name', width: 120 },
    { field: 'Designation', header: 'Designation', width: 120 },
    { field: 'Country', header: 'Country', width: 100 }
];

// initialize MultiColumn Combobox component
let multiComboBoxObject: MultiColumnComboBox = new MultiColumnComboBox({
    //set the data to dataSource property
    dataSource: empData,
    //set the column of the multicolumn combobox
    columns: columnsData,
    //set the fields of the multicolumn combobox
    fields: { text: 'Name', value: 'EmpID' },
    // set placeholder to ComboBox input element
    placeholder: "Select a employee",
});

// render initialized MultiColumn ComboBox
multiComboBoxObject.appendTo('#multicolumn');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 MultiColumn Combobox</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.1.48/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container' style="margin:50px auto 0; width:500px;">
        <br>
        <input type="text" tabindex="1" id='multicolumn' />
    </div>
</body>

</html>

Configure the popup list

By default, the width of the popup list automatically adjusts according to the MultiColumn ComboBox input element’s width, and the height of the popup list has 300px.

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

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

import { MultiColumnComboBox, ColumnModel } from '@syncfusion/ej2-multicolumn-combobox';

// defined the array of object data
let empData: { [key: string]: Object }[] = [ 
    { "EmpID": 1001, "Name": "Andrew Fuller", "Designation": "Team Lead", "Country": "England" },
    { "EmpID": 1002, "Name": "Robert", "Designation": "Developer", "Country": "USA" },
    { "EmpID": 1003, "Name": "Michael", "Designation": "HR", "Country": "Russia" },
    { "EmpID": 1004, "Name": "Steven Buchanan", "Designation": "Product Manager", "Country": "Ukraine" },
    { "EmpID": 1005, "Name": "Margaret Peacock", "Designation": "Developer", "Country": "Egypt" },
    { "EmpID": 1006, "Name": "Janet Leverling", "Designation": "Team Lead", "Country": "Africa" },
    { "EmpID": 1007, "Name": "Alice", "Designation": "Product Manager", "Country": "Australia" },
    { "EmpID": 1008, "Name": "Bob", "Designation": "Developer", "Country": "India" },
    { "EmpID": 1009, "Name": "John", "Designation": "Product Manager", "Country": "Ireland"},
    { "EmpID": 1010, "Name": "Mario Pontes", "Designation": "Team Lead", "Country": "South Africa" },
    { "EmpID": 1011, "Name": "Yang Wang", "Designation": "Developer", "Country": "Russia" },
    { "EmpID": 1012, "Name": "David", "Designation": "Product Manager", "Country": "Egypt" },
    { "EmpID": 1013, "Name": "Antonio Bianchi", "Designation": "Team Lead", "Country": "USA" },
    { "EmpID": 1014, "Name": "Laura", "Designation": "Developer", "Country": "England" },
    { "EmpID": 1015, "Name": "Carlos Hernandez", "Designation": "Developer", "Country": "Canada" },
    { "EmpID": 1016, "Name": "Lily", "Designation": "Product Manager", "Country": "France" },
    { "EmpID": 1017, "Name": "Tom Williams", "Designation": "Developer", "Country": "Ukraine" },
    { "EmpID": 1018, "Name": "Grace", "Designation": "Developer", "Country": "Australia" },
    { "EmpID": 1019, "Name": "Olivia", "Designation": "Team Lead", "Country": "Ireland" },
    { "EmpID": 1020, "Name": "James", "Designation": "Developer", "Country": "China" },
];

let columnsData: ColumnModel[] = [
    { field: 'EmpID', header: 'Employee ID', width: 90 },
    { field: 'Name', width: 90, header: 'Name' },
    { field: 'Designation', header: 'Designation', width: 90 },
    { field: 'Country', header: 'Country', width: 70 }
];

// initialize MultiColumn Combobox component
let multiComboBoxObject: MultiColumnComboBox = new MultiColumnComboBox({
    //set the data to dataSource property
    dataSource: empData,
    //set the column of the multicolumn combobox
    columns: columnsData,
    //set the fields of the multicolumn combobox
    fields: { text: 'Name', value: 'EmpID' },
    // set placeholder to ComboBox input element
    placeholder: "Select a employee",
     // set the height of the popup element
     popupHeight: '250px',
     // set the width of the popup element
     popupWidth: '550px',
});

// render initialized MultiColumn ComboBox
multiComboBoxObject.appendTo('#multicolumn');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 MultiColumn Combobox</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript UI Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="styles.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.1.48/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container' style="margin:50px auto 0; width:350px;">
        <br>
        <input type="text" tabindex="1" id='multicolumn' />
    </div>
</body>

</html>