Sidebar with list view in EJ2 JavaScript Sidebar control
2 May 20235 minutes to read
Any HTML element can be placed in the Sidebar content area. Sidebar supports all types of HTML structures like TreeView
, ListView
, etc.
In the following example, the Sidebar is rendered with ListView component in its content area.
ej.base.enableRipple(true);
// Initialize ListView component
var data = [
{ text: 'Home', id: 'list-02' },
{ text: 'Offers', id: 'list-03' },
{ text: 'Support', id: 'list-04' },
{ text: 'Logout', id: 'list-06' }
];
var listInstance = new ej.lists.ListView({
//Set defined data to dataSource property
dataSource: data
});
listInstance.appendTo('#list');
// Initialize Sidebar component
var defaultSidebar= new ej.navigations.Sidebar({
type: "Over",
width: '100%'
});
defaultSidebar.appendTo('#default-sidebar');
//Click to toggle the Sidebar
document.getElementById('toggle').onclick = function() {
defaultSidebar.toggle();
};
// Close the sidebar
document.getElementById('close').onclick = function() {
defaultSidebar.hide();
};
<!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://cdn.syncfusion.com/ej2/27.2.2/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<!-- sidebar element declaration -->
<aside id="default-sidebar">
<div class="title1">Menu</div>
<div class="closebtn">
<button id="close" class="e-btn close-btn">
<span id="innerclose" class="e-icons close-icon"></span>
</button>
</div>
<div id="listcontainer">
<div id="list"></div>
</div>
<div class="sub-title">
* ListView component is placed inside the sidebar content area.
</div>
</aside>
<!-- end of sidebar element -->
<!-- main content declaration -->
<div>
<div class="title2">Main content</div>
<div class="sub-title"> Click the button to open/close the Sidebar.</div>
<div style="padding:20px" class="center-align">
<button id="toggle" class="e-btn e-info">Toggle Sidebar</button>
</div>
</div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>