# Accordion Panel

## Overview

The AccordionPanel component is a container similar to a TabPanel, but instead of having tabs for showing / hiding forms, it has buttons that behave like an accordion, moving one way or the other.

It can hold one or more forms. But only one of them is visible at a time. The active/visible form can be changed via button clicks or from code. Forms can be added and removed both at design-time and at runtime. Child forms can optionally show data through a relation.

It can be used in both responsive and anchored layouts.

## Get Started

<div align="left"><figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-589bcee1c3b02d69cca3a8704349b9af161c1532%2Faccordion_demo.gif?alt=media" alt=""><figcaption><p>Accordion</p></figcaption></figure></div>

### Creating an Accordion Panel

<div align="left"><figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-d043f0c5580af620e4b961bc658cd04bbc852c2e%2Faccordion-create.gif?alt=media" alt=""><figcaption><p>Create an Accordion Panel</p></figcaption></figure></div>

Here are the steps for creating an Accordion Panel:

1. Open the [Form Editor](../../../../../reference/servoy-developer/object-editors/form-editor) of the form where you need to place an **Accordion Panel**
2. Find **AccordionPanel** in _Form Containers_ section in the components' pallet
3. Drag and drop the **Accordion Panel** component in the desired place of the form
4. Set the tabs; this can be done via [wizard](#setting-the-tabs-property-via-the-wizard) or in the [properties panel](#setting-the-tabs-property-in-the-properties-panel)
5. Edit other [**Accordion Panel** properties](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#properties) if needed

{% hint style="info" %}
It is recommended to set a specific `name` in the **Accordion Panel** properties in order to make it easier to use it later in scripting.\
Example: `"accordionpanel_orders"`.

<img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-db33f76c1ebb1218430155a8119de92f36bf8e66%2Faccordion-name.jpg?alt=media" alt="Accordion Panel name" data-size="original">
{% endhint %}

{% hint style="info" %}
It is recommended to set a specific `name` in the **Accordion Panel tab** properties in order to make it easier to use them later in scripting.\
Example: `"tab_customers"`.

<img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-f89dc1604ab1dd26ccbc550ace79bcd0efa3e9f2%2Faccordion-tab-name.jpg?alt=media" alt="Accordion Panel tab name" data-size="original">
{% endhint %}

### Setting the tabs property via the wizard

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-bfb382e524adba496c0a14e0e60ab28e29eeb7e7%2Faccordion-wizard2.jpg?alt=media" alt=""><figcaption><p>Property configurator for tabs wizard</p></figcaption></figure>

After dragging the container in the form, the `Property configurator for tabs` wizard appears. In order to set the tabs using the wizard, you need to do the following steps:

1. Find in the left side of the wizard the contained form you need: it can have a related or unrelated datasource
2. Click on the form name
3. The selected form will appear on the right side of the wizard, showing some of the tab properties:
   1. `CONTAINEDFORM` : the form shown in the tab
   2. `RELATIONNAME` : the name of the relation between datasources of main and contained forms; this field will be empty if the relation doesn't exist
   3. `TEXT`: the tab's title text ([i18n](../../../../../../reference/servoy-developer/solution-explorer/resources/i18n#i18n) and custom HTML supported)
   4. `delete` icon : you can remove a tab by clicking the icon
4. Click `OK` button after all tabs have been added

### Setting the tabs property in the properties panel

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-752a03dcd2e6a1531ccf820ffa78317ee1059b27%2Faccordion-tab-add-edit.jpg?alt=media" alt=""><figcaption><p>Adding and editing tab properties</p></figcaption></figure>

After dragging the container in the form, the `Property configurator for tabs` wizard appears. Close the editor without setting anything here, find the **Accordion Panel** in the form editor, click it and proceed with the following steps:

1. Add a tab. There are 2 ways of adding a tab:
   1. Select the [`tabs`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#tabs) property and click the `+` button in order to add a tab. Next tabs can be added the same way or by clicking the `+` button (`insert a new array item below`) of another tab. You can change the tabs' order by dragging them into the desired placed inside the **Accordion Panel** (in the form editor).
   2. Drag and drop `tab` component (of an **AccordionPanel** in _Form Containers_ section in the components' pallet) into the **AccordionPanel** container (in the form editor)
2. Expand the [`tabs`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#tabs) property to see the list of tabs. They are also shown in the **AccordionPanel** container (in the form editor)
3. In order to edit each tab, expand it or click the tab name in the **AccordionPanel** container (in the form editor) and set the properties:
   1. [`containedForm`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#containedform): the form shown in the tab
   2. [`disabled`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#disabled): whether that tab is enabled or not
   3. [`name`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#name): a name for this tab
   4. [`relationName`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#relationname): optional relation for the form to be shown
   5. [`text`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#text): the tab's title text ([i18n](../../../../../../reference/servoy-developer/solution-explorer/resources/i18n#i18n) and custom HTML supported)

### Using a relation

The contained form of the tabs can show related data. In order to do this, you need to have the following:

1. Set the [`datasource`](../../../../../../reference/servoycore/object-model/solution/form#datasource) property on the main form
2. Set the [`datasource`](../../../../../../reference/servoycore/object-model/solution/form#datasource) property on the contained form
3. Make sure that the [relation](../../data-modeling/relations) from datasource table of the main form to datasource table of the contained form exists, otherwise it needs to be created
4. Set the above relation in the [`relationName`](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#relationname) property of the corresponding tab

Once everything is set, the contained form will show related data according to the selected record in the main form. When the selected record changes in the main form, so will the related data from the contained form.

## Customize the Tabs

### Set the tab text

Tab's title [text](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#text) property can be set by entering a value in the property field or by entering the [Text Property Editor](../../../../../reference/servoy-developer/object-editors/text-property-editor). Usually this will be plain text or it can contain data from table columns, aggregations, calculations, relations or from and scopes variables, all of them can be combined, as well. [i18n](../../../../../../reference/servoy-developer/solution-explorer/resources/i18n#i18n) is also supported.

Examples:

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-4b2318cf7eaa21aaa2d0f7a8784c4004832face0%2Faccordion-tab-text-plain.jpg?alt=media" alt=""><figcaption><p>Tab text - plain text</p></figcaption></figure>

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-c0a4f5545a9582d5be0089dd101236a2c92fb2ca%2Faccordion-tab-text-db.jpg?alt=media" alt=""><figcaption><p>Tab text - including database column</p></figcaption></figure>

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-4c646a3d7295757e26f13cf8b3908595c56538e7%2Faccordion-tab-text-form-var.jpg?alt=media" alt=""><figcaption><p>Tab text - including form variable</p></figcaption></figure>

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-b711c2e2fc8ef57f9bf4cac8cfa2ae46c93219d9%2Faccordion-tab-text-i18n.jpg?alt=media" alt=""><figcaption><p>Tab text - i18n</p></figcaption></figure>

### Custom Tab Markup

Tab's title [text](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#text) property supports custom HMTL content.

Example:

<figure><img src="https://3933488479-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FjpWd52BKwABWxF2lScUK%2Fuploads%2Fgit-blob-4137abb5a9ac9e913cd02d3b39a3e514bc95db17%2Faccordion-tab-text-html.jpg?alt=media" alt=""><figcaption><p>Tab text - custom HMTL</p></figcaption></figure>

{% hint style="warning" %}
In order for the custom HTML content to be correctly displayed we need to make sure that it will not be sanitized by using `.putClientProperty(UICONSTANTS.TRUST_DATA_AS_HTML,true)`

```javascript
function onShow(firstShow, event) {
	// TODO Auto-generated method stub
    elements.accordionpanel_orders.putClientProperty(UICONSTANTS.TRUST_DATA_AS_HTML,true);
}
```

**Only enable this setting if the data shown can always be trusted and is never composed of data from an external system or user.**
{% endhint %}

## Scripting an Accordion

[**AccordionPanel** properties](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#properties) can be also set in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor).\
You can find a list of AccordionPanel API methods [here](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#api).

### Changing Selected Tab

Here is an example of how to programmatically set the active tab in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor) of the main form:

```javascript
/**
 * TODO generated, please specify type and doc for the params
 * @param index {Number}
 *
 * @properties={typeid:24,uuid:"78CE78AF-AC5A-458B-A611-84DDC07878B3"}
 */
function setActiveTab(index){
	elements.accordionpanel_orders.selectTabAt(index)
} // call this function after the AccordionPanel container is fully loaded on the page and set the desired index value
```

### Adding a Tab

Here is an example of how to programmatically add a tab in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor) of the main form:

```javascript
function onShow(firstShow, event) {
	// TODO Auto-generated method stub
    elements.accordionpanel_orders.addTab(forms.suppliers,'Suppliers'); // Adds a tab to this accordion with that form and text
} 
```

### Removing a Tab

Here is an example of how to programmatically remove a tab in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor) of the main form:

```javascript
function onShow(firstShow, event) {
	// TODO Auto-generated method stub
    var x = elements.accordionpanel_orders.tabs.length;
    elements.accordionpanel_orders.removeTabAt(x-1);  //Removes a tab of the given index. Returns true if this was sucessfull.  
} 
```

### Enabling/Disabling a tab

Here is an example of how to enable and disable a tab in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor) of the main form:

```javascript
function onShow(firstShow, event) {
	// TODO Auto-generated method stub
    elements.accordionpanel_orders.getTabAt(1).disabled = true;   //disables a tab 
    elements.accordionpanel_orders.getTabAt(2).disabled = false;  //enables a tab
} 
```

### Handling Tab Change Event

Here is an example of how to use the [`onChange` event](../../../../../../reference/servoyextensions/ui-components/form-containers/accordionpanel#events) of an AccordionPanel in the [Scripting Editor](../../../../../reference/servoy-developer/object-editors/scripting-editor) of the main form:

Saving the active tab index in the `onChange` event(fired after a different tab is selected) of the AccordionPanel container:

```javascript
/**
 * Fired after a different tab is selected.
 *
 * @param {Number} previousIndex The previous tab index
 * @param {JSEvent} event
 *
 * @private
 *
 * @properties={typeid:24,uuid:"0A044286-CB21-4CCC-A8AA-284137F1D22E"}
 */
function onChange(previousIndex, event) {
	// TODO Auto-generated method stub
	application.setUserProperty(controller.getName()+"."+elements.accordionpanel_orders+".activeTab",elements.accordionpanel_orders.activeTabIndex.toString())
}
```

Checking if there is a saved previous active tab index and set it in the `onShow` main form event:

```javascript
/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"2FD6AD36-9779-49D2-B4A7-985C7C73B66C",variableType:8}
 */
var activeTab;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @private
 *
 * @properties={typeid:24,uuid:"37E091B7-6FD0-47E8-94E4-A22585A3C717"}
 */
function onShow(firstShow, event) {
    // TODO Auto-generated method stub
    activeTab = application.getUserProperty(ontroller.getName()+"."+elements.accordionpanel_orders+".activeTab");
    if (activeTab)
    {
        elements.accordionpanel_orders.selectTabAt(parseInt(activeTab));
    }   
}
```
