Variations in EJ2 JavaScript Sidebar control

2 May 202311 minutes to read

The Sidebar component’s expand behaviour can be modified based on the purpose of use.

Expanding types of Sidebar

The Sidebar can be set to initialize based on four different types that are consistent with the main component as explained below. When dataBind is invoked, this applies the pending property changes immediately to the component.

Item Description
Over Sidebar floats over the main content area.
Push Sidebar pushes the main content area to appear side-by-side, and shrinks the main content within the screen width.
Slide Sidebar translates the x and y positions of main content area based on the Sidebar width. The main content area will not be adjusted within the screen width.
Auto Sidebar with Over type in mobile resolution, and Push type in other higher resolutions.

Auto is the default expand mode. Sidebar with type Auto will always expand on initial rendering, irrespective of enableDock and isOpen properties.

In the following sample, the types of Sidebar are demonstrated.

ej.base.enableRipple(true);
   //Sidebar initialization
   var defaultSidebar = new ej.navigations.Sidebar({
        type: "Push",
        target: document.querySelector('.maincontent')
    });
    defaultSidebar.appendTo('#default-sidebar');
    //end of sidebar initialization

    //toggle button initialization

    var togglebtn = new ej.buttons.Button({iconCss: 'e-icons burg-icon', isToggle: true, content:'Open'}, '#toggle');

    //Click Event.
    document.getElementById('toggle').onclick = function(){
        if (document.getElementById('toggle').classList.contains('e-active')) {
            togglebtn.content = 'Close';
            defaultSidebar.show();
        } else {
            togglebtn.content = 'Open';
            defaultSidebar.hide();
        }
    };

    // Close the sidebar
    document.getElementById('close').onclick = function(){
        defaultSidebar.hide();
        document.getElementById('toggle').classList.remove('e-active');
        togglebtn.content = 'Open'
    };

    var typeOver = new ej.buttons.RadioButton({ label: 'Over', name: 'state', checked: true, change: Change });
    typeOver.appendTo('#over');

    //unchecked state.
    var typePush = new ej.buttons.RadioButton({ label: 'Push', name: 'state', change: Change });
    typePush.appendTo('#push');

    var typeSlide = new ej.buttons.RadioButton({ label: 'Slide', name: 'state', change: Change });
    typeSlide.appendTo('#slide');

    //unchecked state.
    var typeAuto = new ej.buttons.RadioButton({ label: 'Auto', name: 'state', change: Change });
    typeAuto.appendTo('#auto');

    // Change the type of the Sidebar
  function Change(args) {
        if (args.event.target.id == "over") {
            defaultSidebar.type = "Over";
             defaultSidebar.dataBind();
        } else if (args.event.target.id == "push") {
            defaultSidebar.type = "Push";
             defaultSidebar.dataBind();
        }
        else if (args.event.target.id == "slide") {
            defaultSidebar.type = "Slide";
             defaultSidebar.dataBind();
        }
        else {
            defaultSidebar.type = "Auto";
            defaultSidebar.dataBind();
        }
}
<!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/25.1.35/ej2-base/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-lists/styles/material.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-navigations/styles/material.css" rel="stylesheet">
    <link href="styles.css" rel="stylesheet">
    <link href="https://cdn.syncfusion.com/ej2/25.1.35/ej2-buttons/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/25.1.35/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="title"> Sidebar content</div>
            <div id="list"></div>
            <div class="sub-title">
                Click the button to close the Sidebar.
            </div>
            <div class="center-align">
                <button id="close" class="e-btn close-btn">Close Sidebar</button>
            </div>
        </aside>
        <!-- end of sidebar element -->
        <!-- main content declaration -->

        <div id="head">
            <button id="toggle" class="e-btn e-info"></button>
        </div>
            <div class="maincontent" style="height:335px;border:1px solid gray">
                <div class="content">
                    <div class="title">Main content</div>
                    <div class="sub-title">
                        <div class="radiobutton">
                            <input type="radio" id="over">
                        </div>
                        <div class="radiobutton">
                            <input type="radio" id="push">
                        </div>
                        <div class="radiobutton">
                            <input type="radio" id="slide">
                        </div>
                        <div class="radiobutton">
                            <input type="radio" id="auto">
                        </div>
                    </div>
                </div>
            </div>
            <!-- end of main content -->
        </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

See Also