Initialize buttongroup using util function in EJ2 TypeScript Button group control

10 May 20235 minutes to read

Though, it is a CSS component for easy initialization of ButtonGroup createButtonGroup util function can be used.

To use createButtonGroup util function, SplitButton dependencies should be configured and added in system.config.js and it should also be imported in script file.

Using createButtonGroup method, the button options, selector, and cssClass is passed and the corresponding classes is added to the elements.

Create basic ButtonGroup

To create a basic ButtonGroup, the target element along with the button elements should be created in index.html file.

Create radio type ButtonGroup

To create a radio type ButtonGroup, the target element along with the input elements should be created with type radio in index.html.

Create checkbox type ButtonGroup

Checkbox type ButtonGroup creation is similar to radio type ButtonGroup, instead the type of the input elements should be checkbox.

The following example illustrates how to create ButtonGroup using createButtonGroup function for basic, checkbox, and radio
type behaviors.

import { createButtonGroup } from '@syncfusion/ej2-splitbuttons';

// To create basic ButtonGroup.
createButtonGroup('#basic', {
    buttons: [
        { content: 'HTML' },
        { content: 'CSS' },
        { content: 'Javascript'}
    ]
});

// To create checkbox type ButtonGroup.
createButtonGroup('#checkbox', {
    buttons: [
        { content: 'Bold' },
        { content: 'Italic' },
        { content: 'Undeline'}
    ]
});

// To create radio type ButtonGroup.
createButtonGroup('#radio', {
    buttons: [
        { content: 'Left' },
        { content: 'Center' },
        { content: 'Right'}
    ]
});
<!DOCTYPE html>
<html lang="en">

<head>
    <title>EJ2 Button-Group</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="https://cdn.syncfusion.com/ej2/27.2.2/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="styles.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'>
         <h5>Normal behavior</h5>
        <div id='basic'>
            <button></button>
            <button></button>
            <button></button>
        </div>
        <h5>Checkbox type behavior</h5>
        <div id='checkbox'>
            <input type="checkbox" id="checkbold" name="font" value="bold"/>
            <input type="checkbox" id="checkitalic" name="font" value="italic"/>
            <input type="checkbox" id="checkundeline" name="font" value="underline"/>
        </div>
        <h5>Radiobutton type behavior</h5>
        <div id='radio'>
            <input type="radio" id="radioleft" name="align" value="left"/>
            <input type="radio" id="radiomiddle" name="align" value="middle"/>
            <input type="radio" id="radioright" name="align" value="right"/>
        </div>
    </div>
</body>

</html>
#container {
    visibility: hidden;
}

#loader {
  color: #008cff;
  height: 40px;
  left: 45%;
  position: absolute;
  top: 45%;
  width: 30%;
}
.e-btn-group {
  margin: 0 5px 5px 5px;
}

If null value is passed in button options, then that particular button will be skipped from processing in createButtonGroup util function.