# Selecting DataSets

Canonical page: https://www.adaptabletools.com/docs/handbook-data-sets

- The DataSet Selector supplies an entirely new DataSet to AdapTable based on user selection
- DataSets suit scenarios when there is too much data to send to the client at start-up
- Users switch at run time between DataSets provided by developers at design-time
- DataSets can include Form definitions, shown when that DataSet is selected
- AdapTable re-populates AG Grid after each selection

DataSets allow developers to supply large amounts of data to AdapTable, in differing sets as required.

All this is done while still using AG Grid's [Client Side Row Model](https://www.ag-grid.com/javascript-data-grid/client-side-model/#client-side-row-model).

- DataSets are a great alternative to the [Server-Side Row Model](https://www.adaptabletools.com/docs/dev-guide-row-models-server-overview/index.md)
- Data can be fetched when the user picks a DataSet, without building full server-side filtering infrastructure
- Users can still filter the returned rows on the client as usual

Developers provide multiple DataSets for users to choose from, based on the **same column set**.

- DataSets cannot be used for data with non-overlapping fields
- The same AdapTable instance and column-based objects require a consistent column set

## Topics in this Section

<section className="my-8 sm:my-10 grid grid-cols-1 lg:grid-cols-2 gap-x-8 gap-y-4">
  <div className="flex flex-col justify-center">
    
  </div>
  <div className="flex flex-col justify-center">
    
  </div>
</section>

## DataSet Selector

AdapTable lists configured DataSets in the **DataSet Selector**.

- DataSets typically map to concepts in the user's business model — a stored procedure, a time period, or a familiar category (e.g. 'book')

The DataSet Selector appears in:

- [Dashboard Toolbar](https://www.adaptabletools.com/docs/ui-dashboard/index.md#tabs-and-toolbars)
- [Tool Panel](https://www.adaptabletools.com/docs/ui-tool-panel/index.md)
- [Status Bar](https://www.adaptabletools.com/docs/ui-status-bar/index.md)

### DataSet Forms

DataSets can optionally include Form definitions so users can narrow what data is returned.

When a DataSet with a `form` is selected, AdapTable displays an [Adaptable Form](https://www.adaptabletools.com/docs/ui-tutorial-configuring-adaptable-forms/index.md).

See [DataSet Forms](https://www.adaptabletools.com/docs/handbook-data-set-forms/index.md)

## 3 Ways to Load Data

AdapTable does not fetch data by itself when a DataSet Is selected:

Instead you do it one of 3 different ways:

This is for DataSets **without** a `form`

- Using `loadData` on the DataSet
- Using onSelect for custom or async loading
- Subscribing to DataSetSelected

### Using loadData on the DataSet (recommended for row data)

Return row data (or a `Promise` of rows). AdapTable calls `gridApi.loadGridData` when the DataSet is selected. Runs before `onSelect`.

```ts
dataSetOptions: {
  dataSets: [
    {
      name: 'JavaScript',
      description: 'JavaScript Frameworks',
      loadData: () => rowData.filter(r => r.language === 'JavaScript'),
    },
  ],
},
```

### Using onSelect for custom or async loading

Use for server requests or logic that should not go in `loadData`. The [DataSet Selected Event](https://www.adaptabletools.com/docs/technical-reference-data-set/index.md) is still fired for backward compatibility.

```ts
{
  name: 'LatestTrades',
  description: 'Trades for today',
  onSelect: async (info) => {
    const rows = await fetchTrades(info.dataSet.info);
    info.adaptableApi.gridApi.loadGridData(rows);
  },
},
```

### Subscribing to DataSetSelected

Use when you prefer a single event handler for many DataSets. Call [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) `loadGridData` (or your own loader) from the handler.

```ts
adaptableApi.eventApi.on('DataSetSelected', (info: DataSetSelectedInfo) => {
  if (info.dataSet?.name === 'JavaScript') {
    adaptableApi.gridApi.loadGridData(
      rowData.filter(r => r.language === 'JavaScript')
    );
  }
});
```

- Do not use `loadData` and `onSelect` together unless you intend both to run (`loadData` runs first)
- For DataSets **with** a `form`, AdapTable does **not** fire `DataSetSelected`; use [DataSet Forms](https://www.adaptabletools.com/docs/handbook-data-set-forms/index.md) and `onFormSubmit`

**Example: Using DataSets**

Loading Data in AdapTable with DataSets

- This example (usually real DataSets are much larger) defines 3 DataSets in `dataSetOptions.dataSets`, each with a `loadData` function that filters demo `rowData` by `Language`
- `gridOptions.rowData` is `null` so the grid starts **empty** until the user selects a DataSet — typical when data is loaded on demand rather than at start-up

- Open the DataSet Selector and pick a DataSet to load rows
- Switch between DataSets to replace grid data

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {rowData, WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Data Sets',
  dataSetOptions: {
    dataSets: [
      {
        name: 'HTML',
        description: 'HTML Frameworks',
        loadData: () => rowData.filter(r => r.language === 'HTML'),
      },
      {
        name: 'JavaScript',
        description: 'JavaScript Frameworks',
        loadData: () => rowData.filter(r => r.language === 'JavaScript'),
      },
      {
        name: 'TypeScript',
        description: 'TypeScript Frameworks',
        loadData: () => rowData.filter(r => r.language === 'TypeScript'),
      },
    ],
  },
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Data Sets',
          Toolbars: ['DataSet'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['DataSet'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_wiki',
            'created_at',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'description',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,
    enableRowGroup: true,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  // set row data to null as we will get it later
  rowData: null,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```

## Defining DataSets

DataSets are provided in the `dataSets` property of [DataSet Options](https://www.adaptabletools.com/docs/technical-reference-data-set/index.md).

Each DataSet includes a `name` and `description`, and optionally `form`, `loadData`, `onSelect`, and `onFormSubmit` (see the [`DataSet`](https://www.adaptabletools.com/docs/reference/dataset.md) reference).

### `dataSets`

DataSets provided for users to select the Grid's data source

  [`DataSet[]`](https://www.adaptabletools.com/docs/reference/dataset.md)

Use this property to set which DataSets appear in the DataSet Selector.

The [`DataSet`](https://www.adaptabletools.com/docs/reference/dataset.md) object is defined as follows:

| Property | Type | Description | Default |
| --- | --- | --- | --- |
| [description](https://www.adaptabletools.com/docs/reference/dataset.md#description) | `string` | Describes the Data Set |   |
| [form](https://www.adaptabletools.com/docs/reference/dataset.md#form) | [`AdaptableForm`](https://www.adaptabletools.com/docs/reference/adaptableform.md)`<`[`DataSetFormContext`](https://www.adaptabletools.com/docs/reference/datasetformcontext.md)`>` | Params for Data Set popup form |   |
| [info](https://www.adaptabletools.com/docs/reference/dataset.md#info) | `Record<string, string \| number \| boolean \| Date>` | Additional info for Data Set |   |
| [loadData](https://www.adaptabletools.com/docs/reference/dataset.md#loaddata) | `(info: `[`DataSetSelectedInfo`](https://www.adaptabletools.com/docs/reference/datasetselectedinfo.md)`) => TData[] \| Promise<TData[]>` | Optional shortcut that loads row data into the Grid when this Data Set is<br/>selected (only when no `form` is defined). Runs before `onSelect`. | undefined |
| [name](https://www.adaptabletools.com/docs/reference/dataset.md#name) | `string` | Name of Data Set |   |
| [onFormSubmit](https://www.adaptabletools.com/docs/reference/dataset.md#onformsubmit) | `(context: `[`DataSetFormContext`](https://www.adaptabletools.com/docs/reference/datasetformcontext.md)`) => void \| Promise<void>` | Invoked when the user submits the Data Set form (Enter key or form button).<br/>When no form `buttons` are defined, an OK button is added automatically. | undefined |
| [onSelect](https://www.adaptabletools.com/docs/reference/dataset.md#onselect) | `(info: `[`DataSetSelectedInfo`](https://www.adaptabletools.com/docs/reference/datasetselectedinfo.md)`) => void \| Promise<void>` | Invoked when this Data Set is selected and it does not define a `form`.<br/><br/>Use for custom loading logic (e.g. server requests). The `DataSetSelected`<br/>event is still fired for backward compatibility. | undefined |
| [IsReadOnly](https://www.adaptabletools.com/docs/reference/dataset.md#isreadonly) | `boolean` | Sets Entity to ReadOnly (overwriting a Strategy Entitlement of &#39;Full&#39;) |   |

```ts {5-7}
const adaptableOptions: AdaptableOptions = {
  dataSetOptions: {
    dataSets: [
      {
        name: 'C#',
        description: 'C# Libraries',
        loadData: () => fetchLibraries('C#'),
      },
      {
        name: 'Java',
        description: 'Java Libraries',
        loadData: () => fetchLibraries('Java'),
      },
      {
        name: 'JavaScript',
        description: 'JavaScript Libraries',
        form: { fields: [/* see DataSet Forms */] },
        onFormSubmit: context => {
          /* load rows from context.formData */
        },
      },
    ],
  },
};
```

Full Form examples: [DataSet Forms](https://www.adaptabletools.com/docs/handbook-data-set-forms/index.md)

## UI Entitlements

[UI Entitlements](https://www.adaptabletools.com/docs/handbook-permissioning/index.md) behaviour is as follows:

- `Full` — DataSets can be selected; all DataSet UI components are available
- `Hidden` — no DataSet UI is visible or available
- `ReadOnly` — same as `Full` for this module: users only **select** DataSets; there is nothing to create, edit, or persist in DataSet UI, so ReadOnly does not add restrictions beyond `Hidden`

### FAQ

**Do DataSets support parameterised queries?**
Yes. Add an [Adaptable Form](https://www.adaptabletools.com/docs/ui-tutorial-configuring-adaptable-forms/index.md) on the DataSet definition. AdapTable shows it when that DataSet is selected. See [DataSet Forms](https://www.adaptabletools.com/docs/handbook-data-set-forms/index.md).
