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
@using Syncfusion.EJ2.Navigations;

@section ControlsSection{
    <div class="control-section">
        <div class="control_wrapper accordion-control-section">
            <div id="nested1" style="display: none">
             @Html.EJS().Accordion("childNested1").Items(new List<AccordionAccordionItem>
             {
                new AccordionAccordionItem { Header = "Video", Content = "Video" },
                new AccordionAccordionItem { Header = "Music", Content = "Music" }
             }).Render()
            </div>
             @Html.EJS().Accordion("ParentNested").Items(new List<AccordionAccordionItem>
             {
                new AccordionAccordionItem { Header = "Video", Content = "#nested1" },
                new AccordionAccordionItem { Header = "Music", Content = "Music track 1" }
             }).Render()
        </div>
    </div>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class AccordionController : Controller
    {
        // GET: //
        public IActionResult DefaultFunctionalities()
        {
            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
@section ControlsSection{
    <div class="control-section">
        <div class="control_wrapper accordion-control-section">
            <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>
        </div>
    </div>
}

<script>
    var ajax = new ej.base.Ajax('../Content/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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class AccordionController : Controller
    {
        // GET: //
        public IActionResult DefaultFunctionalities()
        {
            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
@using Syncfusion.EJ2.Navigations;

@section ControlsSection{
    <div class="control-section">
        <div class="control_wrapper accordion-control-section">
            @Html.EJS().Accordion("defaultAccordion").Items(new List<AccordionAccordionItem>
        {
        new AccordionAccordionItem { 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. ASP.NET pages execute on the server and generate markup such as HTML, WML, or XML that is sent to a desktop or mobile browser. ASP.NET pages use a compiled,event-driven programming model that improves performance and enables the separation of application logic and user interface." },
        new AccordionAccordionItem { Header = "ASP.NET MVC", Expanded = true, Content = "The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication." },
        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.More recently, however, it has become common in both game development and the creation of desktop applications." }
        }).Render()
        </div>
    </div>
}

@section Scripts{
<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>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class AccordionController : Controller
    {
        // GET: //
        public IActionResult DefaultFunctionalities()
        {
            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
@section ControlsSection{
<div class="control-section">
    <div class="control_wrapper accordion-control-section">
        <div id='element'></div>  
    </div>
</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>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace EJ2CoreSampleBrowser.Controllers
{
    public partial class AccordionController : Controller
    {
        // GET: //
        public IActionResult DefaultFunctionalities()
        {
            return View();
        }
    }
}