# Grid View

## Overview

The Grid View component is used to display a collection of items. By default, it contains an image field and a caption which can be replaced with the required components.\
If you need to create a custom layout, you can add the necessary components to the first list item. The added components are replicated for the other grid view items.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2Fdaz5wJ9SNel4u9H732L6%2FCleanShot%202025-07-31%20at%2015.29.25%402x-min.png?alt=media&#x26;token=593d156d-0d6e-4600-bc06-845bfdf3d63d" alt=""><figcaption></figcaption></figure>

### Properties

<table><thead><tr><th width="173.203125">Name</th><th width="172.0390625">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td><code>string</code></td><td>Component name</td></tr><tr><td><code>pageSize</code></td><td><code>number</code></td><td>Number of items displayed per page</td></tr><tr><td><code>pageCount</code></td><td><code>number</code></td><td>Total number of pages</td></tr><tr><td><code>activePageIndex</code></td><td><code>number</code></td><td>Current active page index</td></tr><tr><td><code>paginationOffset</code></td><td><code>number</code></td><td>Offset used for pagination</td></tr><tr><td><code>afterCursor</code></td><td><code>string</code></td><td>Cursor for paginated data fetching</td></tr><tr><td><code>value</code></td><td><code>value[]</code></td><td>Current component value</td></tr><tr><td><code>valid</code></td><td><code>boolean</code></td><td>Indicates if the component is valid</td></tr><tr><td><code>children</code></td><td><code>GridViewItemApi[]</code></td><td>Child grid items inside the grid view</td></tr></tbody></table>

### Methods

<table><thead><tr><th width="156.58203125">Name</th><th width="140.3125">Parameters</th><th width="103.10546875">Returns</th><th>Description</th></tr></thead><tbody><tr><td><code>setPage</code></td><td><code>index: number</code></td><td><code>void</code></td><td>Navigate to a specific page within the grid view</td></tr><tr><td><code>setPageSize</code></td><td><code>size: number</code></td><td><code>void</code></td><td>Sets the number of items displayed per page in the grid view</td></tr><tr><td><code>setValue</code></td><td><code>data: object</code></td><td><code>void</code></td><td>Set component data. Data is an <code>object[]</code> with arbitrary structure</td></tr><tr><td><code>resetValue</code></td><td>–</td><td><code>void</code></td><td>Reset component to the initial value</td></tr><tr><td><code>validate</code></td><td>–</td><td><code>void</code></td><td>Trigger form validation</td></tr><tr><td><code>resetValidation</code></td><td>–</td><td><code>void</code></td><td>Clear validation errors</td></tr></tbody></table>

### Triggers

<table><thead><tr><th width="246.73046875">Name</th><th>Description</th></tr></thead><tbody><tr><td><strong>On Init</strong></td><td>Triggered when the component is initialized</td></tr><tr><td><strong>On Page Change</strong></td><td>Triggered when the grid page changes</td></tr><tr><td><strong>On Items Per Page Change</strong></td><td>Triggered when the grid page item changes</td></tr></tbody></table>

## Working with the component

Gird View supports both static and dynamic data. To display data dynamically from your action, select the action you need in the *Data* property dropdown of the component.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FJ2wKAJArnONQnYYuxxDY%2FCleanShot%202025-07-31%20at%2015.51.39%402x-min.png?alt=media&#x26;token=0ed0a1be-83bf-46ed-9d21-281aded4662a" alt=""><figcaption></figcaption></figure>

### Accessing Grid items

You can access any grid item by referencing it as `{{item}}`, for example, `{{item.first_name}}`. To reference the index of the item, you can use the `{{index}}` variable.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FcAeIED5L8zZEIHbmfp94%2FCleanShot%202025-07-31%20at%2016.06.43%402x-min.png?alt=media&#x26;token=f0120c4e-5488-4b9a-b018-901771c12262" alt=""><figcaption></figcaption></figure>

### Keeping Grid items in sync

If you need to display Checkbox or Input components, for example, and monitor their values corresponding to the Grid items, you can configure it in UI Bakery. Check out the flow below:point\_down:

1. Start by creating a state variable (App scope) that will contain the list of items to be displayed in the Grid View - select the *Array* type.

{% hint style="success" %}
Refer to [this article](https://docs.uibakery.io/concepts/app-state-variables#creating-a-state-variable) if you need a reminder of how to create state variables.
{% endhint %}

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FzsR0BcDOMd7yfnZcUvxk%2FCleanShot%202025-08-01%20at%2016.35.40%402x-min.png?alt=media&#x26;token=66fe6111-05ca-49b2-8bba-dfdc8a04742a" alt=""><figcaption></figcaption></figure>

2. Next, create a new action of the *JavaScript Code* type to load the data into the variable:

```javascript
const list = [
  {
    id: 1,
    name: 'USA',
    selected: false,
  },
  {
    id: 2,
    name: 'Canada',
    selected: false,
  },
  {
    id: 3,
    name: 'France',
    selected: false,
  },
 ];
{{state.setValue('list', list)}}
```

{% hint style="info" %}
Each item must have a unique id for later identification purposes. Also, note that each item includes the `selected` attribute - it indicates whether the checkbox has been selected or not.
{% endhint %}

3. Now, connect the variable to the Grid view component and the inner Checkbox component inside the Grid:
   1. **Grid View** - assign the state variable to the component's *Data* property.\
      ![](https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FFOH9GUUUVNEAYX8CAbbT%2FCleanShot%202025-08-01%20at%2016.41.01%402x-min.png?alt=media\&token=38e30da1-40d2-4e34-a2e6-8dfa404c9b9e)
   2. **Checkbox** - set the item name in the component's *Label* field and the `selected` property in the *Value* field.\
      Now, if we change the **list** variable values and set some items to be selected, the changes will be reflected on the UI.\
      ![](https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FzbqZEFljBgooqZqJ1Hy2%2FCleanShot%202025-08-01%20at%2016.43.24%402x-min.png?alt=media\&token=8caa7567-a8ac-4938-aac9-6ab2df89827e)
4. Finally, create another action of the *JavaScript Code* type to track checkbox value changes and update the `selected` value inside the **list** variable:

```javascript
const updatedList = {{state.list}}.map((item) => {
  if (item.id === {{params.id}}) {
    // reverse value
    return {
      ...item,
      selected: !item.selected,
    };
  }
  // unchanged
  return item;
});
{{state.setValue('list', updatedList)}};
```

5. Now, assign this action to the Checkbox's *On Change* trigger and add the id of the current grid item to the *Action Arguments* field - `{ id: {{item.id}} }`.\
   This will indicate which checkbox has changed its value.

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2F4JQQuKjFsT2Gl06c4hYu%2FCleanShot%202025-08-01%20at%2016.46.44%402x-min.png?alt=media&#x26;token=dbde2280-d731-4b1b-9f27-3bea8ff58cc6" alt=""><figcaption></figcaption></figure>

That's it! This way, you will keep your Grid checkbox items in sync with the data model and you can use this data to send all updated values to the server.

### Server-side pagination

If you have a large dataset, you may find it useful to use server-side pagination. You will find details on how to set it up in the article below:point\_down:

{% content-ref url="../../concepts/components/work-with-components/table-server-side-pagination" %}
[table-server-side-pagination](https://docs.uibakery.io/concepts/components/work-with-components/table-server-side-pagination)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.uibakery.io/reference/working-with-components/grid-view.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
