Sidebar with treeview in EJ2 TypeScript Sidebar control
2 May 202316 minutes to read
The following example demonstrates how to render TreeView component inside the Sidebar with dock state and how to achieve expand and collapse the functionalities simultaneously in the sidebar and Treeview.
On collapse, the LI elements of TreeView show icons only to represent the short sign of the hidden text content. On expand, hidden text content will be set to be visible.
import { Sidebar, TreeView } from '@syncfusion/ej2-navigations';
import { ListView } from '@syncfusion/ej2-lists';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
let sidebarMenu: Sidebar = new Sidebar({
width: '290px',
dockSize:"44px",
enableDock: true,
target: '.main-content',
mediaQuery: '(min-width: 600px)',
created:onCreate,
close: onClose
});
sidebarMenu.appendTo('#sidebar-treeview');
let data: { [key: string]: Object }[] = [
{
nodeId: '01', nodeText: 'Installation', iconCss: 'icon-microchip icon',
nodeChild: [
{ nodeId: '05-01', nodeText: 'Calendar', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-02', nodeText: 'DatePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-03', nodeText: 'DateTimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-04', nodeText: 'DateRangePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-05', nodeText: 'TimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-06', nodeText: 'SideBar', iconCss: 'icon-circle-thin icon' }
]
},
{
nodeId: '02', nodeText: 'Deployment', iconCss: 'icon-thumbs-up-alt icon',
},
{
nodeId: '03', nodeText: 'Quick Start', iconCss: 'icon-docs icon',
},
{
nodeId: '04', nodeText: 'Components', iconCss: 'icon-th icon',
nodeChild: [
{ nodeId: '04-01', nodeText: 'Calendar', iconCss: 'icon-circle-thin icon' },
{ nodeId: '04-02', nodeText: 'DatePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '04-03', nodeText: 'DateTimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '04-04', nodeText: 'DateRangePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '04-05', nodeText: 'TimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '04-06', nodeText: 'SideBar', iconCss: 'icon-circle-thin icon' }
]
},
{
nodeId: '05', nodeText: 'API Reference', iconCss: 'icon-code icon',
nodeChild: [
{ nodeId: '05-01', nodeText: 'Calendar', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-02', nodeText: 'DatePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-03', nodeText: 'DateTimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-04', nodeText: 'DateRangePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-05', nodeText: 'TimePicker', iconCss: 'icon-circle-thin icon' },
{ nodeId: '05-06', nodeText: 'SideBar', iconCss: 'icon-circle-thin icon' }
]
},
{
nodeId: '06', nodeText: 'Browser Compatibility', iconCss: 'icon-chrome icon'
},
{
nodeId: '07', nodeText: 'Upgrade Packages', iconCss: 'icon-up-hand icon'
},
{
nodeId: '08', nodeText: 'Release Notes', iconCss: 'icon-bookmark-empty icon'
},
{
nodeId: '09', nodeText: 'FAQ', iconCss: 'icon-help-circled icon'
},
{
nodeId: '10', nodeText: 'License', iconCss: 'icon-doc-text icon'
}
];
// TreeView initialization
let mainTreeView: TreeView = new TreeView({
fields: { dataSource: data, id: 'nodeId', text: 'nodeText', child: 'nodeChild' }, expandOn: 'Click',
});
mainTreeView.appendTo('#main-treeview');
document.getElementById('hamburger').onclick = (): void => {
if(sidebarMenu.isOpen)
{
sidebarMenu.hide();
mainTreeView.collapseAll();
}
else {
sidebarMenu.show();
mainTreeView.expandAll();
}
};
function onCreate(): void {
this.element.style.visibility = '';
}
function onClose(args: any) {
mainTreeView.collapseAll();
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Sidebar</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://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
<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-lists/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/ej2-navigations/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'>
<!-- sidebar element declaration -->
<div class="main-header" id="header-section">
<ul class="header-list">
<li class="float-left header-style icon-menu" id="hamburger"></li>
<li class="float-left header-style nav-pane"><b>Navigation Pane</b></li>
<li class="header-style float-right support border-left"><b>Support</b></li>
</ul>
</div>
<aside id="sidebar-treeview" style="visibility: hidden">
<div class="main-menu">
<div>
<ul id="main-treeview">
</ul>
</div>
</div>
</aside>
<!-- end of sidebar element -->
<!-- main content declaration -->
<div class="main-content" id="main-text">
<div class="sidebar-content">
<h2 class="sidebar-heading"> Responsive Sidebar With Treeview</h2>
<p class="paragraph-content">This is a graphical aid for visualising and categorising the site, in the
style of an expandable and collapsable treeview component. It auto-expands to display the node(s),
if any, corresponding to the currently viewed title, highlighting that node(s) and its ancestors.
Load-on-demand when expanding nodes is available where supported (most graphical browsers), falling
back to a full-page reload. MediaWiki-supported caching, aside from squid, has been considered so
that unnecessary re-downloads of content are avoided where possible. The complete
expanded/collapsed state of the treeview persists across page views in most situations.</p>
<div class="line"></div>
<h2 class="sidebar-heading">Lorem Ipsum Dolor</h2>
<p class="paragraph-content">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>
<div class="line"></div>
<h2 class="sidebar-heading"> Lorem Ipsum Dolor</h2>
<p class="paragraph-content">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<div class="line"></div>
<h2 class="sidebar-heading"> Lorem Ipsum Dolor</h2>
<p class="paragraph-content">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
<!-- end of main scontent element -->
</div>
</body>
</html>