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. The nested Accordion can be rendered with the use of provided events, such as clicked and expanding.

Source
Preview
app.vue
<template>
    <div id="app">
    <ejs-accordion ref="Accordion_Nested" :expanding="expanding">
        <e-accordionitems>
        <e-accordionitem expanded='true' header='Video' content='<div id="nested_video"></div>'>
        </e-accordionitem>
        <e-accordionitem header='Music' content='<div id="nested_music"></div>' ></e-accordionitem>
        <e-accordionitem header='Images' content='<div id="nested_images"></div>'></e-accordionitem>
      </e-accordionitems>
    </ejs-accordion>
  </div>
</template>
<script>
import Vue from 'vue';
import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
import { ExpandEventArgs, Accordion, AccordionClickArgs} from '@syncfusion/ej2-navigations';

Vue.use(AccordionPlugin);
export default {
  name: 'app',
  data () {

},
  methods: {
    clicked: function (e) {
    var ele = e.originalEvent.target;
    if (ele.querySelectorAll('.e-accordion').length > 0) {
        return;
    }
    var nestAcrdn_musNew = new Accordion({
        items: [{
                header: 'New Track1'
            },
            {
                header: 'New Track2'
            }
        ]
    }, '#nested_musicNew');

}
        expanding: function (e) {
        var obj = this.$refs.Accordion_Nested.ej2Instances
        if (e.isExpanded && [].indexOf.call(obj.items, e.item) === 0) {
            if (e.element.querySelectorAll('.e-accordion').length > 0) {
                return;
            }
            var nestAcrdn_vid = new Accordion({
                items: [{
                        header: 'Video Track1'
                    },
                    {
                        header: 'Video Track2'
                    }
                ]
            }, '#nested_video');

        }
        if (e.isExpanded && [].indexOf.call(obj.items, e.item) === 1) {
            if (e.element.querySelectorAll('.e-accordion').length > 0) {
                return;
            }

            var nestAcrdn_mus = new Accordion({
                clicked: this.clicked,
                items: [{
                        header: 'Music Track1'
                    },
                    {
                        header: 'Music Track2'
                    },
                    {
                        header: 'Music New',
                        content: '<div id="nested_musicNew"></div>'
                    }
                ]
            }, '#nested_music');

        }
        if (e.isExpanded && [].indexOf.call(obj.items, e.item) === 2) {
            if (e.element.querySelectorAll('.e-accordion').length > 0) {
                return;
            }

            var nestAcrdn_img = new Accordion({
                items: [{
                        header: 'Track1'
                    },
                    {
                        header: 'Track2'
                    },
                ]
            }, '#nested_images');

        }
  }
}
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>

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.

Source
Preview
app.vue
<template>
    <div id="app">
    <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>
    <ejs-accordion ref="acrdnInstance">
      <e-accordionitems>
        <e-accordionitem header='Department' content = '#acrdnContnet1'></e-accordionitem>
        <e-accordionitem header='Platform' content = '#acrdnContnet2'></e-accordionitem>
         <e-accordionitem header='Employee Details'></e-accordionitem>
      </e-accordionitems>
    </ejs-accordion>
  </div>
</template>
<script>
  import Vue from 'vue';
  import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
  import { ExpandEventArgs, Accordion, AccordionClickArgs} from '@syncfusion/ej2-navigations';
import { Ajax } from '@syncfusion/ej2-base';

Vue.use(AccordionPlugin);
export default {
  name: 'app',
  data () {
 let ajax: Ajax = new Ajax('./Ajax.html', 'GET', true);
 ajax.send().then();
 ajax.onSuccess = (data: string): void => {
     var obj = this.$refs.acrdnInstance.ej2Instances
     obj.items[2].content = data;
     obj.refresh();
}
}
}
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>

Set custom animation

Accordion supports custom animations for both expand and collapse actions from the provided animation option of Animation library.

Default animation is given as SlideDown for expanding the panel and SlideUp for collapsing the panel. You can also disable the animation by setting animation effect as none.

The sample demonstrates some types of animation that suits for Accordion. You can check all the animation effects here.

Source
Preview
app.vue
<template>
    <div id="app">
   <div style='padding-top: 25px'>
    <div class='row'>
    <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
      <label> Expand Animation </label></div>
      <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
      <div class='custom_drop'><ejs-dropdownlist ref="expandInstance" :change= 'expandAnimationChange' index='0' :dataSource='expandAni' placeholder='Expand Animation'></ejs-dropdownlist></div>
    </div></div>
    <div class='row'>
    <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
    <label> Collapse Animation </label></div>
    <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
    <div class='custom_drop'><ejs-dropdownlist ref="expandInstance" :change= 'collapseAnimationChange' index='1' :dataSource='expandAni' placeholder='Collapse Animation'></ejs-dropdownlist></div>
    </div></div>
    <div style='padding-top: 25px'>
    <ejs-accordion ref="acrdnInstance">
      <e-accordionitems>
        <e-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-accordionitem>
        <e-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-accordionitem>
        <e-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-accordionitem>
      </e-accordionitems>
    </ejs-accordion>
    </div></div>
  </div>
</template>
<script>
import Vue from 'vue';
import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
import {Accordion, AccordionEffect} from '@syncfusion/ej2-navigations';
import { DropDownList } from '@syncfusion/ej2-dropdowns';
import { DropDownListPlugin } from "@syncfusion/ej2-vue-dropdowns";

Vue.use(AccordionPlugin);
Vue.use(DropDownListPlugin);
export default {
  name: 'app',
 data: function() {
    return {
 expandAni:['SlideDown', 'SlideUp', 'FadeIn', 'FadeOut', 'FadeZoomIn', 'FadeZoomOut', 'ZoomIn', 'ZoomOut', 'None'];
    };
  },
methods: {
 expandAnimationChange: function(e) {
   var obj = this.$refs.acrdnInstance.ej2Instances;
   var ddlobj = this.$refs.expandInstance.ej2Instances;
  obj.animation.expand.effect = ddlobj.value;
  }
 collapseAnimationChange: function(e) {
    var obj = this.$refs.acrdnInstance.ej2Instances;
     var ddlobj = this.$refs.collapseInstance.ej2Instances;
      obj.animation.collapse.effect = ddlobj.value;
  }
}
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>

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.

Source
Preview
app.vue
<template>
    <div id="app">
  <ejs-accordion ref="element" expandMode='Single' :expanding="beforeExpand" :clicked="clicked">
      <e-accordionitems>
        <e-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-accordionitem>
        <e-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-accordionitem>
        <e-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-accordionitem>
      </e-accordionitems>
    </ejs-accordion>
  </div>
</template>
<script>
import Vue from 'vue';
import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
import {Accordion, ExpandEventArgs, AccordionClickArgs} from '@syncfusion/ej2-navigations';

Vue.use(AccordionPlugin);
export default {
  name: 'app',
  data(){

  },
  methods:{

 clicked: function(e) {
  var clickEle = e.originalEvent.target;
}
 beforeExpand: function(ExpandEventArgs) {
   var obj = this.$refs.element.ej2Instances;
  var expandCount = obj.element.querySelectorAll('.e-selected').length;
  var ele = obj.element.querySelectorAll('.e-selected')[0];
  if (ele) {
    ele = ele.firstChild
  }
  if (expandCount === 1 && ele === clickEle) {
    e.cancel = true;
  }
}
  }
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>

Create wizard using Accordion

Accordion items can be disabled dynamically by passing the index and boolean value with the enableItem method.

The below Wizard sample is designed for Online Shopping model. In this, each Accordion item is integrated with required components to fill the details and designed for getting user details and making payment at the end. Each field is provided with validation for all mandatory option to enable/disable to next Accordion.

Source
Preview
app.vue
<template>
    <div id="app">
    <div id="Sign_In_Form" style="display:none; padding: 3px 0">
       <form id="formId">
           <div class="form-group">
               <div class="e-float-input">
                   <input type="text" id="email" name="Email" required="" />
                   <span class="e-float-line"></span>
                   <label class="e-float-text" for="email">Email</label>
               </div>
               <div class="e-float-input">
                   <input id="password" type="password" name="Password" required="" />
                   <span class="e-float-line"></span>
                   <label class="e-float-text" for="password">Password</label>
               </div>
           </div>
       </form>
       <div style="text-align: center">
           <button class='e-btn' id="Continue_Btn" >Continue</button>
           <div id="err1">* Please fill all fields</div>
       </div>
   </div>
   <div id="Address_Fill" style="display:none; padding: 3px 0">
       <form id="formId_Address">
           <div class="form-group">
               <div class="e-float-input">
                   <input type="text" id="name" name="Name" required="" />
                   <span class="e-float-line"></span>
                   <label class="e-float-text" for="name">Name</label>
               </div>
           </div>
           <div class="form-group">
               <div class="e-float-input">
                   <input type="text" id="address" name="Address" required="" />
                   <span class="e-float-line"></span>
                   <label class="e-float-text" for="address">Address</label>
               </div>
           </div>
           <div class="form-group">
               <ejs-numerictextbox id="mobile" format='0' placeholder='Mobile' floatLabelType='Auto' showSpinButton=false></ejs-numerictextbox>
           </div>
       </form>
       <div style="text-align: center">
           <button class='e-btn' id="Continue_BtnAdr" >Continue</button>
           <div id="err2">* Please fill all fields</div>
       </div>
   </div>
   <div id="Card_Fill" style="display:none; padding: 3px 0;">
       <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
           <div class="form-group">
               <ejs-numerictextbox id="cardNo" format='0' placeholder='Card No' floatLabelType='Auto' showSpinButton=false></ejs-numerictextbox>
           </div>
       </div>
       <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
           <div class="form-group">
               <div class="e-float-input">
                   <input type="text" id="cardHolder" name="cardHolder" required="" />
                   <span class="e-float-line"></span>
                   <label class="e-float-text" for="cardHolder">CardHolder Name</label>
               </div>
           </div>
       </div>
       <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
           <ejs-datepicker id="expiry" width='100%' format='MM/yyyy' placeholder='Expiry Date' floatLabelType='Auto' ></ejs-datepicker>
       </div>
       <div class="col-xs-6 col-sm-6 col-lg-6 col-md-6">
           <div class="form-group">
               <ejs-numerictextbox id="CVV" format='0' placeholder='CVV' floatLabelType='Auto' showSpinButton=false></ejs-numerictextbox>
           </div>
       </div>
       <div style="text-align: center">
           <button class='e-btn' id="Back_Btn" >Back</button>
           <button class='e-btn' id="Save_Btn" >Save</button>
           <div id="err3">* Please fill all fields</div>
       </div>
   </div>
   <ejs-dialog ref="alertDlg" header='Alert' width=200 isModal=true content='' visible=false :target='dlgTarget' :buttons='dlgButtons' ></ejs-dialog>
   <ejs-accordion ref="accordionInc" :expanding='expanding'>
       <e-accordionitems>
           <e-accordionitem expanded='true' header='Sign In' content='#Sign_In_Form'></e-accordionitem>
           <e-accordionitem header='Delivery Address' content='#Address_Fill'></e-accordionitem>
           <e-accordionitem header='Card Details' content='#Card_Fill'></e-accordionitem>
       </e-accordionitems>
   </ejs-accordion>
     </div>
</template>
<script>
import Vue from 'vue';
import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
import {Accordion, ExpandEventArgs, AccordionClickArgs} from '@syncfusion/ej2-navigations';
import { DialogPlugin} from '@syncfusion/ej2-vue-popups';
import { DatePickerPlugin } from '@syncfusion/ej2-vue-calendars';
import { NumericTextBoxPlugin } from '@syncfusion/ej2-vue-inputs';
Vue.use(AccordionPlugin);
Vue.use(DialogPlugin);
Vue.use(DatePickerPlugin);
Vue.use(NumericTextBoxPlugin);
export default {
  name: 'app',
    data(){
        return {
     dlgButtons: [{
       buttonModel: { content: 'Ok', isPrimary: true },
       click: (() => {
         this.$refs.alertDlg.ej2Instances.hide();
          var obj1 = this.$refs.accordionInc.ej2Instances;
         if (obj1.expandedItems[0] === 2 && obj.content === success) {
           obj1.enableItem(0, true);
           obj1.enableItem(1, false);
           obj1.enableItem(2, false);
           obj1.expandItem(true, 0);
         }
       })
     }];
     dlgTarget : document.body;
       };
     },
  methods:{
     checkMail:function(mail) {
     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail)) {
       return (true);
     } else {

       this.$refs.alertDlg.ej2Instances.content = 'Enter valid email address'
       this.$refs.alertDlg.ej2Instances.show();
       return (false);
     }
   }

    checkMobile: function(mobile) {
     if (mobile.match(/^\d{10}$/)) {
       return (true);
     } else {
       this.$refs.alertDlg.ej2Instances.content = 'Mobile number length should be 10'
       this.$refs.alertDlg.ej2Instances.show();
       return (false);
     }
   }

    checkCardNo:function(cardNo) {
     if (cardNo.match(/^\d{16}$/)) {
       return (true);
     } else {
       this.$refs.alertDlg.ej2Instances.content = 'Card number length should be 16'
       this.$refs.alertDlg.ej2Instances.show();
       return (false);
     }
   }

    checkCVV:function(cvv) {
     if (cvv.match(/^\d{3}$/)) {
       return (true);
     } else {
       this.$refs.alertDlg.ej2Instances.content = 'CVV number length should be 3'
       this.$refs.alertDlg.ej2Instances.show();
       return (false);
     }
   }

    expanding:function(e) {

     if (e.name === 'expanding' && [].indexOf.call(this.$refs.accordionInc.ej2Instances.items, e.item) === 0) {
       document.getElementById('Continue_Btn').onclick = (e) => {
         var email = document.getElementById('email');
         var password = document.getElementById('password');
         if (email.value !== '' && password.value !== '') {
           if (this.checkMail(email.value)) {
             email.value = password.value = '';
             var acrdnObj = this.$refs.accordionInc.ej2Instances;
             acrdnObj.enableItem(1, true);
             acrdnObj.enableItem(0, false);
             acrdnObj.expandItem(true, 1);
           }
           document.getElementById('err1').classList.remove('show');
         } else {
           document.getElementById('err1').classList.add('show');
         }
       }
     } else if (e.name === 'expanding' && [].indexOf.call(this.$refs.accordionInc.ej2Instances.items, e.item) === 1) {
       document.getElementById('Continue_BtnAdr').onclick = (e) => {
         var name = document.getElementById('name');
         var address = document.getElementById('address');
         var mobile = document.getElementById('mobile');
         if ((name.value !== '') && (address.value !== '') && (mobile.value !== '')) {
           if (this.checkMobile(mobile.value)) {
             var acrdnObj = this.$refs.accordionInc.ej2Instances;
             acrdnObj.enableItem(2, true);
             acrdnObj.enableItem(1, false);
             acrdnObj.expandItem(true, 2);
           }
           document.getElementById('err2').classList.remove('show');
         } else {
           document.getElementById('err2').classList.add('show');
         }
       }
     } else if (e.name === 'expanding' && [].indexOf.call(this.$refs.accordionInc.ej2Instances.items, e.item) === 2) {
       document.getElementById('Back_Btn').onclick = (e) => {
         acrdnObj = this.$refs.accordionInc.ej2Instances;
         acrdnObj.enableItem(1, true);
         acrdnObj.enableItem(2, false);
         acrdnObj.expandItem(true, 1);
       }
       document.getElementById('Save_Btn').onclick = (e) => {
         var cardHolder = document.getElementById('cardHolder');
         var expiry = document.getElementById('expiry');
         var cardNo = document.getElementById('cardNo');
         var cvv = document.getElementById('CVV');
         if ((cardNo.value !== '') && (cardHolder.value !== '') && (expiry.value !== '') && (cvv.value !== '')) {
           if (this.checkCardNo(cardNo.value)) {
             if (this.checkCVV(cvv.value)) {
               this.$refs.alertDlg.ej2Instances.content = 'Your payment successfully processed'
               this.$refs.alertDlg.ej2Instances.show();
             }
           }
           document.getElementById('err3').classList.remove('show');
         } else {
           document.getElementById('err3').classList.add('show');
         }
       }
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>

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.

Source
Preview
app.vue
<template>
    <div id="app">

    <ejs-accordion ref="accordionInc">

    </ejs-accordion>
  </div>
</template>
<script>
import Vue from 'vue';
import { AccordionPlugin } from '@syncfusion/ej2-vue-navigations';
import { DataManager, Query, ODataAdaptor, ReturnOption } from '@syncfusion/ej2-data';
Vue.use(AccordionPlugin);
export default {
  name: 'app',
  data () {

}, mounted(){

  new DataManager({ url: 'https://js.syncfusion.com/ejServices/Wcf/Northwind.svc/Employees', adaptor: new ODataAdaptor})
    .executeQuery(new Query().range(4, 7)).then((e) => {

    var result = e.result;
        var obj = this.$refs.accordionInc.ej2Instances
         var itemsData =[];
        var  mapping =  { header: 'FirstName', content: 'Notes' };
      for(var i= 0; i < result.length; i++) {

        itemsData.push({ header: result[i][mapping.header], content: result[i][mapping.content] });
      }
     obj.items = itemsData;
      obj.refresh();

}
}
}
</script>
<style>
  @import "../../node_modules/@syncfusion/ej2-base/styles/material.css";
  @import "../../node_modules/@syncfusion/ej2-vue-navigations/styles/material.css";
</style>