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.

tagHelper
nested.cs
@using Syncfusion.EJ2

@section ControlsSection{
    <div class="control-section">
        <div class="control_wrapper accordion-control-section">
            <div id="nested1" style="display: none">
                <ejs-accordion id="childNested1">
                    <e-accordion-accordionitems>
                        <e-accordion-accordionitem header="Video" content='Video'></e-accordion-accordionitem>
                        <e-accordion-accordionitem header="Music" content='Music'></e-accordion-accordionitem>
                    </e-accordion-accordionitems>
                </ejs-accordion>
            </div>

            <ejs-accordion id="ParentNested" expandMode="Single">
                <e-accordion-accordionitems>
                    <e-accordion-accordionitem header="Video" content='#nested1' ></e-accordion-accordionitem>
                    <e-accordion-accordionitem header="Music" content='Music track 1' ></e-accordion-accordionitem>
                </e-accordion-accordionitems>
            </ejs-accordion>
        </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.

tagHelper
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.

tagHelper
openalways.cs
@using Syncfusion.EJ2

@section ControlsSection{
    <div class="control-section">
        <div class="control_wrapper accordion-control-section">
            <ejs-accordion id="defaultAccordion">
                <e-accordion-accordionitems>
                    <e-accordion-accordionitem expanded='true' 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.'></e-accordion-accordionitem>
                    <e-accordion-accordionitem 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.'></e-accordion-accordionitem>
                    <e-accordion-accordionitem 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.'></e-accordion-accordionitem>
                </e-accordion-accordionitems>
            </ejs-accordion>
        </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.

tagHelper
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();
        }
    }
}