ES5 getting started in EJ2 JavaScript TreeView

14 Sep 20265 minutes to read

The Essential® JS 2 for JavaScript (global script) is an ES5 formatted pure JavaScript framework which can be directly used in latest web browsers.

Create a Folder and HTML File

  • Create a folder named quickstart in your desired directory.

  • Inside the quickstart folder, create a new file named index.html.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts

Add Syncfusion® Treeview CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base, ej2-data, ej2-lists, ej2-inputs, ej2-buttons, and ej2-popups scripts are required dependency packages for the Treeview component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/treeview/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-data/dist/global/ej2-data.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/32.1.37/ej2-lists/dist/global/ej2-lists.min.js
https://cdn.syncfusion.com/ej2/32.1.37/ej2-buttons/dist/global/ej2-buttons.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-navigations/dist/global/ej2-navigations.min.js

Adding Treeview component to the application

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 for TreeView</title>
    <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/treeview/index.css" rel="stylesheet" />

    <!-- Essential JS 2 TreeView's global script -->
    <script src="//cdn.syncfusion.com/ej2/32.1.19/dist/ej2.min.js" type="text/javascript"></script>

</head>

<body>
    <!--Element which will render as TreeView-->
    <div id='container'>
        <div id='treeparent'
            style="display: block;border: 1px solid #dddddd;border-radius: 3px;overflow: auto;max-width: 350px;max-height: 350px;margin: auto;">
            <div id="tree"></div>
        </div>
    </div>

    <script>
        ej.base.enableRipple(true);

        //define the array of JSON
        var data = [
            {
                nodeId: '01', nodeText: 'Music',
                nodeChild: [
                    { nodeId: '01-01', nodeText: 'Gouttes.mp3' }
                ]
            },
            {
                nodeId: '02', nodeText: 'Videos', expanded: true,
                nodeChild: [
                    { nodeId: '02-01', nodeText: 'Naturals.mp4' },
                    { nodeId: '02-02', nodeText: 'Wild.mpeg' },
                ]
            },
            {
                nodeId: '03', nodeText: 'Documents',
                nodeChild: [
                    { nodeId: '03-01', nodeText: 'Environment Pollution.docx' },
                    { nodeId: '03-02', nodeText: 'Global Water, Sanitation, & Hygiene.docx' },
                    { nodeId: '03-03', nodeText: 'Global Warming.ppt' },
                    { nodeId: '03-04', nodeText: 'Social Network.pdf' },
                    { nodeId: '03-05', nodeText: 'Youth Empowerment.pdf' },
                ]
            },
        ];
        //Initialize TreeView control
        var treeViewInstance = new ej.navigations.TreeView({
            fields: { dataSource: data, id: 'nodeId', text: 'nodeText', child: 'nodeChild' }
        });

        //Render initialized TreeView
        treeViewInstance.appendTo("#tree");
    </script>

</body>

</html>

You can also explore our JavaScript (ES5) TreeView to knows how to present and manipulate data.