Template configuration in EJ2 TypeScript Toolbar control

2 May 202315 minutes to read

The JavaScript Toolbar can be rendered by item based collection and by HTML elements. To render it based on the given HTML element, use id as the target property. To render the Toolbar, follow the below structure of the HTML elements:

  <div id='template_toolbar'>   --> Root Toolbar Element
    <div>      --> Toolbar Items Container
       <div>   --> Toolbar Item Element
       </div>
    </div>
  </div>

Here, the template ID, #template_toolbar is directly appended to the Toolbar.

import {Toolbar} from '@syncfusion/ej2-navigations';
let toolbar: Toolbar = new Toolbar();
toolbar.appendTo('#template_toolbar');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toolbar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.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'>
        <div id='template_toolbar'>
            <div>
                <div><button class='e-btn e-tbar-btn'>Cut</button> </div>
                <div><button class='e-btn e-tbar-btn'>Copy</button> </div>
                <div><button class='e-btn e-tbar-btn'>Paste</button> </div>
                <div class='e-separator'> </div>
                <div><button class='e-btn e-tbar-btn'>Bold</button> </div>
                <div><button class='e-btn e-tbar-btn'>Italic</button> </div>
            </div>
        </div>
        <br/><br/>
        <div id='result'></div>
    </div>
</body>

</html>

Popup is one of the supported responsive modes of the Toolbar. The Toolbar commands, popup mode priority and button text mode customizations are achieved in the item based rendering through property declaration. For more information on popup mode, refer here

The above behavior can also be achieved with template rendering by defining equivalent class names instead of property declaration.

Equivalent class names listed below are needed to add the Toolbar items’ div element.

Priority

Class Description
e-overflow-show Commands are always displayed on the Toolbar with primary priority.
e-overflow-hide Commands are always displayed in the popup with secondary priority.

Button text mode

Class Description
e-popup-text Button text is only visible in the Popup.
e-toolbar-text Button text is only visible on the Toolbar.
import {Toolbar} from '@syncfusion/ej2-navigations';

let toolbar: Toolbar = new Toolbar({ width: 300 , overflowMode: 'Popup'  });
toolbar.appendTo('#template_toolbar');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toolbar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.1.48/material.css" rel="stylesheet" />
    <link href="https://ej2.syncfusion.com/angular/demos/src/toolbar/toolbar.component.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'>
        <div id='template_toolbar'>
            <div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-cut-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Cut</div></button> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-copy-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Copy</div></button> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-paste-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Paste</div></button> </div>
                <div class='e-separator'> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-bold-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Bold</div></button> </div>
                <div class='e-overflow-hide e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-underline-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Underline</div></button> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-italic-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Italic</div></button> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-ascending-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">A-Z Sort</div></button> </div>
                <div class='e-overflow-show e-popup-text'><button class='e-btn e-tbar-btn'><span class="e-descending-icon tb-icons e-icons e-btn-icon"></span><div class="e-tbar-btn-text">Z-A Sort</div></button> </div>
            </div>
        </div>
        <br/><br/>
        <div id='result'></div>
    </div>
</body>

</html>

Integrate menu component

You can integrate menu component as toolbar item in Toolbar using template property. Menu can be populated with items as needed.

import { Toolbar,Menu,MenuItemModel } from '@syncfusion/ej2-navigations';

let menuTemplate: string = '<ul id="menu"></ul>';
let data: MenuItemModel[] = [
  {
    text: 'Appliances',
    items: [
      {
        text: 'Kitchen',
        items: [
          { text: 'Electric Cookers' },
          { text: 'Coffee Makers' },
          { text: 'Blenders' },
        ],
      },
      {
        text: 'Washing Machine',
        items: [{ text: 'Fully Automatic' }, { text: 'Semi Automatic' }],
      },
      {
        text: 'Air Conditioners',
        items: [
          { text: 'Inverter ACs' },
          { text: 'Split ACs' },
          { text: 'Window ACs' },
        ],
      },
    ],
  },
  {
    text: 'Accessories',
    items: [
      {
        text: 'Mobile',
        items: [
          { text: 'Headphones' },
          { text: 'Memory Cards' },
          { text: 'Power Banks' },
        ],
      },
      {
        text: 'Computer',
        items: [
          { text: 'Pendrives' },
          { text: 'External Hard Disks' },
          { text: 'Monitors' },
        ],
      },
    ],
  },
  {
    text: 'Fashion',
    items: [
      {
        text: 'Men',
        items: [
          { text: 'Shirts' },
          { text: 'Jackets' },
          { text: 'Track Suits' },
        ],
      },
      {
        text: 'Women',
        items: [{ text: 'Kurtas' }, { text: 'Salwars' }, { text: 'Sarees' }],
      },
    ],
  },
  {
    text: 'Home & Living',
    items: [
      {
        text: 'Furniture',
        items: [
          { text: 'Beds' },
          { text: 'Mattresses' },
          { text: 'Dining Tables' },
        ],
      },
      {
        text: 'Decor',
        items: [
          { text: 'Clocks' },
          { text: 'Wall Decals' },
          { text: 'Paintings' },
        ],
      },
    ],
  },
];

let toolbarObj: Toolbar = new Toolbar({
  items: [
    { text: "Cut" },
    { text: "Copy" },
    { template: menuTemplate },
    { text: "Paste" }
  ],
});
toolbarObj.appendTo('#element');
let menuObj: Menu = new Menu({items: data,},'#menu');
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toolbar</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.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'>
        <div id='element'></div>
    </div>
</body>

</html>