# DataSet Forms

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

- DataSets are defined in Adaptable Options
- A DataSet can include a Form so users choose parameters before data is loaded

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).

## Handling Form Submission

Unlike DataSets without a form, AdapTable does **not** fire the [DataSet Selected Event](https://www.adaptabletools.com/docs/technical-reference-data-set/index.md) when the user selects a DataSet that defines a `form`.

After the user fills in the form, **you** load row data into the Grid.

There are **two equivalent ways** to hook that moment — pick one per DataSet; you do not need both.

| Approach                          | Where you wire loading                | Best when                                                                              |
| --------------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------- |
| **`onFormSubmit`** on the DataSet | DataSet definition (alongside `form`) | Default choice: Enter key, optional default OK button, one place for submit logic      |
| **`onClick` on a form button**    | Inside `form.buttons`                 | Custom button labels, multiple actions, or legacy setups that already use form buttons |

Both handlers receive the same [`DataSetFormContext`](https://www.adaptabletools.com/docs/reference/datasetformcontext.md) and usually call [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) `loadGridData`.

### `onFormSubmit` on the DataSet

Define **`onFormSubmit`** on the DataSet object.

If you omit `form.buttons`, AdapTable adds a default **OK** button that submits the form and calls your handler.

### Submitting a DataSet Form with onFormSubmit

Use the same [Adaptable Form](https://www.adaptabletools.com/docs/ui-tutorial-configuring-adaptable-forms/index.md) field types as elsewhere in AdapTable.

AdapTable invokes this when the user presses Enter or clicks a form button (including a default OK button when you omit `buttons`). Use `context.formData` and `context.adaptableApi.gridApi.loadGridData`.

```ts
{
  name: 'TypeScript',
  description: 'TypeScript Frameworks',
  form: {
    fields: [
      { name: 'minStars', label: 'Minimum Stars', fieldType: 'number', defaultValue: 10000 },
    ],
  },
  onFormSubmit: (context: DataSetFormContext) => {
    const rows = rowData.filter(
      r => r.language === 'TypeScript' && r.github_stars > context.formData.minStars
    );
    context.adaptableApi.gridApi.loadGridData(rows);
  },
},
```

### `onClick` on a form button

The second option is to define **`form.buttons`** and load data in a button's **`onClick`**.

Use this instead of `onFormSubmit` when you want explicit buttons (for example **Load** and **Cancel**) or when submit logic already lives on a button handler.

- If the DataSet also defines **`onFormSubmit`**, AdapTable still invokes it when a button submits the form
- Avoid duplicating the same load logic in both places

### Using a custom form button onClick

Define one or more entries in `form.buttons`. Use this when you need custom labels, multiple actions, or you are not using `onFormSubmit` on the DataSet.

The handler receives [`DataSetFormContext`](https://www.adaptabletools.com/docs/reference/datasetformcontext.md) (`formData`, `adaptableApi`, and the selected `dataSet`). Call [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) `loadGridData` when the user clicks the button.

```ts
{
  name: 'TypeScript',
  description: 'TypeScript Frameworks',
  form: {
    fields: [
      { name: 'minStars', label: 'Minimum Stars', fieldType: 'number', defaultValue: 10000 },
    ],
    buttons: [
      {
        label: 'Load',
        onClick: (_button, context: DataSetFormContext) => {
          const rows = rowData.filter(
            r =>
              r.language === 'TypeScript' &&
              r.github_stars > (context.formData?.minStars as number)
          );
          context.adaptableApi.gridApi.loadGridData(rows);
        },
      },
    ],
  },
},
```

### Understanding DataSetFormContext

The submit handler receives [`DataSetFormContext`](https://www.adaptabletools.com/docs/reference/datasetformcontext.md):

| Property | Type | Description |
| --- | --- | --- |
| [dataSet](https://www.adaptabletools.com/docs/reference/datasetformcontext.md#dataset) | [`DataSet`](https://www.adaptabletools.com/docs/reference/dataset.md) | The DataSet which triggered the Form |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/datasetformcontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

It extends [`FormContext`](https://www.adaptabletools.com/docs/reference/formcontext.md):

| Property | Type | Description |
| --- | --- | --- |
| [formData](https://www.adaptabletools.com/docs/reference/formcontext.md#formdata) | [`AdaptableFormData`](https://www.adaptabletools.com/docs/reference/adaptableformdata.md) | Adaptable Form Data |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/formcontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

Together these provide the selected DataSet, form field values, and Adaptable API access.

**Example: Using DataSet Forms**

Loading Data in AdapTable with DataSet Forms

- Two DataSets in `dataSetOptions.dataSets`:
  - **`JavaScript`** — uses `loadData` when selected; loaded at start-up via `setDataSet` in [Adaptable Ready](https://www.adaptabletools.com/docs/getting-started-adaptable-ready/index.md) (short timeout so AdapTable is ready)
  - **`TypeScript`** — opens a form; **`onFormSubmit`** filters TypeScript rows by license, stars, and wiki flag, then calls [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) `loadGridData`
- See `adaptableOptions.ts` and `onAdaptableReady.ts` in the demo tabs

- Switch between the two DataSets
- Change filters on the TypeScript form and submit to refresh rows

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
  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, DataSetFormContext} from '@adaptabletools/adaptable';
import {rowData, WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Data Set Forms',
  dataSetOptions: {
    dataSets: [
      {
        name: 'JavaScript',
        description: 'JavaScript Frameworks',
        loadData: () => rowData.filter(r => r.language === 'JavaScript'),
      },
      {
        name: 'TypeScript',
        description: 'TypeScript Frameworks',
        form: {
          fields: [
            {
              name: 'hasWiki',
              label: 'HasWiki',
              fieldType: 'checkbox',
              defaultValue: true,
            },
            {
              name: 'minStars',
              label: 'Minimum Stars',
              defaultValue: 10000,
              fieldType: 'number',
            },
            {
              name: 'license',
              label: 'License',
              fieldType: 'select',
              defaultValue: 'MIT License',
              options: [
                {
                  label: 'MIT License',
                  value: 'MIT License',
                },
                {
                  label: 'BSD 3-Clause',
                  value: 'BSD 3-Clause',
                },
                {
                  label: 'Other',
                  value: 'Other',
                },
              ],
            },
          ],
        },
        onFormSubmit: (context: DataSetFormContext) => {
          const tsFrameworks = rowData.filter(
            r =>
              r.language === 'TypeScript' &&
              r.license == context.formData?.license &&
              r.github_stars > (context.formData?.minStars as number) &&
              r.has_wiki === context.formData?.hasWiki
          );
          context.adaptableApi.gridApi.loadGridData(tsFrameworks);
        },
      },
    ],
  },
  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/onAdaptableReady.ts`**

```ts
import {AdaptableReadyInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  setTimeout(() => {
    adaptableApi.dataSetApi.setDataSet('JavaScript');
  }, 200);
};
```

**`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',
      },
    ],
  },
};
```
