Search results

How-To

Set the nested Accordion

Accordion supports to render nested level of Accordion by using content property. You can give nested Accordion content inside the parent Accordion content property by using id of nested element.

razor
nested.cs
@using Syncfusion.EJ2.Navigations;

<div style="display: none">
    @Html.EJS().Accordion("childNested1").Items(new List<AccordionAccordionItem>
    {
        new AccordionAccordionItem { Header = "Video-1" },
        new AccordionAccordionItem { Header = "Video-2" }
    }).Render()
</div>
<div style="display: none">
    @Html.EJS().Accordion("childNested2").Items(new List<AccordionAccordionItem>
        {
        new AccordionAccordionItem { Header = "Music-1" },
        new AccordionAccordionItem { Header = "Music-2" },
        new AccordionAccordionItem { Header = "New Playlist", Expanded = true, Content= "#childNested4" }
        }).Render()
</div>
<div style="display: none">
    @Html.EJS().Accordion("childNested3").Items(new List<AccordionAccordionItem>
    {
        new AccordionAccordionItem { Header = "Image-1" },
        new AccordionAccordionItem { Header = "Image-2" }
    }).Render()
</div>
<div style="display: none">
    @Html.EJS().Accordion("childNested4").Items(new List<AccordionAccordionItem>
    {
        new AccordionAccordionItem { Header = "New Track-1" },
        new AccordionAccordionItem { Header = "New Track-2" }
    }).Render()
</div>
<div>
    @Html.EJS().Accordion("ParentNested").Width("50%").ExpandMode(ExpandMode.Multiple).Items(new List<AccordionAccordionItem>
    {
        new AccordionAccordionItem { Header = "Videos", Expanded = true, Content = "#childNested1" },
        new AccordionAccordionItem { Header = "Music", Expanded = true, Content = "#childNested2" },
        new AccordionAccordionItem { Header = "Images", Expanded = true, Content = "#childNested3" }
    }).Render()
</div>
public ActionResult Index()
{
    return View();
}

Output be like the below.

Accordion Control with nested structure

Load content through Ajax

Accordion supports to load external contents through AJAX library. Refer the below steps.

  • Import the Ajax module from ej2-base and initialize with URL path.

  • Get data from the Ajax Success event to initialize Accordion with retrieved external path data.

razor
ajax.cs
<div id="element"></div>
<div id="acrdnContnet1" style="display:none">
    <ul style="margin : 0px;padding:0px 16px; list-style-type: none">
        <li>Testing</li>
        <li>Development</li>
    </ul>
</div>
<div id="acrdnContnet2" style="display:none">
    <ul style="margin : 0px;padding:0px 16px; list-style-type: none">
        <li>Mobile</li>
        <li>Web</li>
    </ul>
</div>

<script>
    var ajax = new ej.base.Ajax('../Scripts/accordion/ajax.html', 'GET', true);
    ajax.send().then();
    ajax.onSuccess = function (data) {
        var ctn2 = data;
        //Initialize Accordion component
        var accordion = new ej.navigations.Accordion({
            items: [
                { header: 'Department', content: '#acrdnContnet1' },
                { header: 'Platform', content: '#acrdnContnet2' },
                { header: 'Employee Details', content: ctn2 },
            ]
        });

        //Render initialized Accordion component
        accordion.appendTo('#element');
    }
</script>

<style>
    .accordion-control-section {
        margin: 0 10% 0 10%;
    }

    @@media screen and (max-width: 768px) {
        .accordion-control-section {
            margin: 0;
        }
    }
</style>
public ActionResult Index()
{
    return View();
}

To keep single pane open always

By default, all Accordion panels are collapsible. You can customize the Accordion to keep one panel as expanded state always. This is applicable for Single expand mode.

razor
openalways.cs
@using Syncfusion.EJ2.Navigations;

@(Html.EJS().Accordion("defaultAccordion")
    .Clicked("clicked")
    .Expanding("beforeExpand")
    .Items(new List<AccordionAccordionItem> {
        new AccordionAccordionItem { Header = "ASP.NET", Expanded = true, Content = "Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services." },
        new AccordionAccordionItem { 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." },
        new AccordionAccordionItem { Header = "JavaScript", Content = "JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed." }
    })
    .Render()
)

<script type="text/javascript">
    var clickEle;
    function clicked(e) {
        clickEle = e.originalEvent.target;
    }
    function beforeExpand(e) {
        var expandCount = this.element.querySelectorAll('.e-selected').length;
        var ele = this.element.querySelectorAll('.e-selected')[0];
        if (ele) {
            ele = ele.firstChild;
        }
        if (expandCount === 1 && ele === clickEle) {
            e.cancel = true;
        }
    }
</script>
public ActionResult Index()
{
    return View();
}

Load accordion with DataSource

You can bind any data object to Accordion items, by mapping it to header and content  property.

In the below demo, Data is fetched from an OData service using DataManager. The result data is formatted as a JSON object with header and content fields, which is set to items property of Accordion.

razor
datasource.cs
<div id='element'></div>  

<script>
var itemsData = [];
var mapping = { header: 'FirstName', content: 'Notes' };

const SERVICE_URI = 'http://services.odata.org/V4/Northwind/Northwind.svc/Employees';

new ej.data.DataManager({ url: SERVICE_URI, adaptor: new ej.data.ODataV4Adaptor })
    .executeQuery(new ej.data.Query().range(4, 7)).then((e) => {

    var result = e.result;

    for (var i = 0; i < result.length; i++) {
        itemsData.push({ header: result[i][mapping.header], content: result[i][mapping.content] });
    }

    //Initialize Accordion component
    var accordion = new ej.navigations.Accordion({
        items: itemsData
    });

    //Render initialized Accordion component
    accordion.appendTo('#element');
});
</script>

<style>
    .accordion-control-section {
        margin: 0 10% 0 10%;
    }

    @@media screen and (max-width: 768px) {
        .accordion-control-section {
            margin: 0;
        }
    }
</style>
public ActionResult Index()
{
    return View();
}