# Accessors

Accessors allow you to retrieve and use data from a query result or from a data store variable. The data can be used to set element properties and styles, and can also be used in an event payload. For more information on accessors specific to event payloads, see [event-payload](https://docs.acho.io/app-builder/app-construction/interactions/event-payload "mention").

## Data structure overview

There are 3 levels of access: element level, page level, and app level. These levels define the scope of the data, or in other words, which pages or elements will have access to it.

In the below table, you'll find 4 data categories as well as their corresponding accessor.&#x20;

Categories include element, page, and app data -- which fall under the [data-store](https://docs.acho.io/app-builder/app-construction/data-store "mention") source -- and [query](https://docs.acho.io/app-builder/app-construction/query "mention").

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FKSYyPufeA30l1Aoi2pq2%2Fimage.png?alt=media&#x26;token=c336d1bf-33f1-4876-85b9-14a91ca7d8e8" alt=""><figcaption></figcaption></figure>

## App level: app data and query results

All elements across all pages can access data stored in app data and query results.&#x20;

* To access app data, use `${#app.variable_name}` to access
* To access query results, employ the syntax `${#query_name}`. **Ensure that the query is renamed to a valid name**. Valid query names should start with a letter or underscore `_`, followed by a combination of letters, numbers, and underscores, with no spaces allowed.

For example, if you want to access a single value from a query "car\_database" in a text:

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F2YbTaWm2NmBgKzv5EQLs%2Fimage%20(44).png?alt=media&#x26;token=be76dd62-7c33-4732-a737-ae809ea0df63" alt=""><figcaption></figcaption></figure>

<div align="left"><figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F9BRggBWAod400h5dCBac%2Fimage%20(43).png?alt=media&#x26;token=b8efeaf7-96cd-4150-b609-2b90f51a8d19" alt=""><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FxsyR84fYDgVrMmpwhzrQ%2Fimage%20(45).png?alt=media&#x26;token=b7db78fd-fcfe-426f-8fb0-ce499985474d" alt=""><figcaption></figcaption></figure></div>

## Page level: page data

All the elements on a given page can access its page data. In the diagram below, you'll see that elements A and B on page 1 are not able to access page data for page 2.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FOqLcGkx511eh4ZvW2Koy%2Fimage.png?alt=media&#x26;token=576b0f90-411a-4a40-a2a5-c07965e2214e" alt=""><figcaption></figcaption></figure>

## Element level: element data

Element data stores variables associated with a specific element. Only the element itself can access the data.

As seen below, element B cannot access element data for element A, and vice versa.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FECrf81g2oAl926y3vlcp%2Fimage.png?alt=media&#x26;token=31b23dda-300d-40fd-8ce5-fc7883ea976b" alt=""><figcaption></figcaption></figure>

### Element data example

Suppose we have a container that we would like to hide, and only show when a user triggers it to display by clicking on a "show more" button.

Here's how we can use element data to initially hide the container, and then unhide it when a user clicks a button.

1. Select the container. In the **Data** tab of the left panel, add a new element level data field. Name the variable `display`, set its type as string, and initialize the value to `none`.\
   ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FLYmQmTs1oeRnVFNM23Bs%2Fimage.png?alt=media\&token=93520ad7-8318-4e59-8be7-52dd7378ba6c)
2. Open the **Style** tab in the right panel, and find Layout -> Display. Use the element data accessor, `${display}`, to set the display. By default, this will be `none`.\
   ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F8TJLfPxb7QLPPUuVdYZ0%2Fimage.png?alt=media\&token=13d1178e-f62e-4345-8813-d48b19c6251d)
3. To show the container, add an interaction to change the display value. In this example, an interaction has been added to a button. In the action panel, select the container, and choose Set Data as the action. Then define the field (name of the variable) and value. This will change the display from `none` to `inline`, and show the now-inline container.<br>

   <figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2Full99fhdryfkQTkpzu7O%2Fimage.png?alt=media&#x26;token=788d3189-b7c4-4bd2-85ee-0c7088291638" alt=""><figcaption></figcaption></figure>

### Element data exceptions

In general elements will not be able to access other elements' element data. However, there is an exception to this when it comes to **inheritance**. Some elements, such as lists or tables, allow other elements to access their element data via accessors.

#### Lists

Elements inside of a list will be able to use accessors to retrieve the list's data. For more details on list accessors, see [list](https://docs.acho.io/app-builder/app-construction/elements/layout-elements/list "mention").

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F8MK9RBQmQgv6qEBf1MAg%2Fimage.png?alt=media&#x26;token=52a43139-142c-4132-8e8f-1aa6fa032514" alt=""><figcaption></figcaption></figure>

#### Tables

Similarly, elements within a table will be able to access the table's column key and row data.

<figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FkffODQNCjxAHdDrP2seG%2Fimage.png?alt=media&#x26;token=39eda935-bcc0-4ac4-8f3a-c3e1142165fe" alt=""><figcaption></figcaption></figure>

## Practical example: Interactive Highlight color

Here is an example to use the techniques above to highlight number of interest.

1. Create query node

   &#x20; ![](https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2F5J9pXSAnZS0vKd0976Dy%2FScreen%20Shot%202023-02-15%20at%2011.50.32%20AM.png?alt=media\&token=af4efdb8-fd26-4568-b822-8e155623ad39)
2. Create a list element and drag a text element inside, set text to ${$item.n}
3. Create a page variable called number in Data Store
4. Create a input element to set page variable(See details in [data-store](https://docs.acho.io/app-builder/app-construction/data-store "mention"))
   1. Set Config -> Property -> Input type to 'number'
   2. Add interactions, attention: Set Action Parameters as the figure below, enter `number` (Page variable name) in `Field` and `${event.value}` in `Value`.

      <figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FIMc6KVOMfp1VWmYwT2Su%2FScreen%20Shot%202023-02-15%20at%2011.55.27%20AM.png?alt=media&#x26;token=22f5da50-348b-4135-a42c-9e7d242dcefb" alt=""><figcaption></figcaption></figure>
5. Set text element styles parameters
   1. set width and height to 64px
   2. set background color to `${$item.n==#page.number&&'red'||'green'}`
6. Click 'Preview' to see the final result. Enter any number between 1 to 10 in the input to activate highlight number.<br>

   <figure><img src="https://3574406564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MB_fx7PCUqvFEdrucJC%2Fuploads%2FU5t1CrBLjN07ltqMIB2O%2FScreen%20Shot%202023-02-15%20at%2012.03.35%20PM.png?alt=media&#x26;token=a80c9421-f590-4134-b07f-698b7221d808" alt=""><figcaption></figcaption></figure>
