Search results

Load accordion items dynamically

Accordion items can be added dynamically by passing the item and index value with the addItem method.

In the following demo, new items are added dynamically when you expand an Accordion header using expanded event.

  • Data is fetched from the data source and it is formatted as a JSON object with header and content fields.

  • Here last index is calculated to append the new Accordion item at the end and the number of items are limited to 10, since the data bound have only 10 items.

Source
Preview
index.js
index.html
ej.base.enableRipple(true);

//Initialize Accordion component

    var dbFlag = 0;
    var dynamciAcrdnCount = 2;
    var acrdnObj = new ej.navigations.Accordion({
    expanded: expanded,
        items: [
            { header: 'ASP.NET', content: 'Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.' },
            { header: ' ASP.NET Core ', content: ' ASP.NET Core is a free and open-source web framework, and the next generation of ASP.NET, developed by Microsoft and the community. It is a modular framework that runs on both the full .NET Framework, on Windows, and the cross-platform .NET Core.' },
            { header: 'ASP.NET MVC', content: 'The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.' },
        ]
    });

//Render initialized Accordion component
    acrdnObj.appendTo('#Accordion_default');

function expanded(e) {
    //Find the expanded state item in accordion
    var Elementindex = document.getElementsByClassName("e-expand-state e-selected e-active")[0];
    //Check the expand state item and currently selected item index. If it is expanded state add dyanmic items or else does nothing
    if ([].slice.call(e.element.parentElement.children).indexOf(e.element) == [].slice.call(e.element.parentElement.children).indexOf(Elementindex)) {
        var array = accordion;
        for (var i = 0; i < dynamciAcrdnCount; i++) {
            if (dbFlag === array.length) { //checking whether all the items in data source added
                return;
            }
            // Item object and the index argument passed into the additem method to add a new accordion
            acrdnObj.addItem(array[dbFlag], acrdnObj.items.length);
            ++dbFlag;
        }
    }
}
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Accordion</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="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    <link href="index.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="control-section">
            <div class="control_wrapper accordion-control-section">
                <div id="Accordion_default"></div>
            </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>