# Configuring Action Columns

Canonical page: https://www.adaptabletools.com/docs/handbook-action-column-configuring

- Action Columns are defined at Design Time in [Action Column Options](https://www.adaptabletools.com/docs/technical-reference-action-column/index.md)
- Each Column has identifiers, a `controls` array, optional `settings`, and optional `rowScope`

## Defining Action Columns

Action Columns are provided in the `actionColumns` property of [Action Column Options](https://www.adaptabletools.com/docs/technical-reference-action-column/index.md).

### `actionColumns`

Columns which contain interactive Controls used for performing Actions

  [`ActionColumn[]`](https://www.adaptabletools.com/docs/reference/actioncolumn.md)

This property is used for setting [Action Columns](https://www.adaptabletools.com/docs/handbook-action-column/index.md) in AdapTable.

The full definition of the [`ActionColumn`](https://www.adaptabletools.com/docs/reference/actioncolumn.md) object is as follows:

| Property | Type | Description |
| --- | --- | --- |
| [columnId](https://www.adaptabletools.com/docs/reference/actioncolumn.md#columnid) | `string` | Mandatory &#39;Id&#39;; if no value set for `friendlyName`, this will also be Column name |
| [controls](https://www.adaptabletools.com/docs/reference/actioncolumn.md#controls) | [`ActionColumnControl`](https://www.adaptabletools.com/docs/reference/actioncolumncontrol.md)`<TData>[]` | Interactive Controls to display in each Column cell (each is identified by `type` e.g. `'button'` or `'command'`) |
| [friendlyName](https://www.adaptabletools.com/docs/reference/actioncolumn.md#friendlyname) | `string` | How Column appears in Column Header, Menus; if no value set, `columnId` is used |
| [rowScope](https://www.adaptabletools.com/docs/reference/actioncolumn.md#rowscope) | [`RowScope`](https://www.adaptabletools.com/docs/reference/rowscope.md) | Which types of Rows should contain the Controls (i.e. data, grouped, summary) |
| [settings](https://www.adaptabletools.com/docs/reference/actioncolumn.md#settings) | [`ActionColumnSettings`](https://www.adaptabletools.com/docs/reference/actioncolumnsettings.md) | Optional properties to configure the Column (e.g. width, overflow, resizable) |

Developers define 4 main sets of information:

### Identifiers

- `columnId` — used internally when referencing the Column in Layouts and other objects
- `friendlyName` — used in the UI (falls back to `columnId`)

### Controls

`controls` is an array of [`ActionColumnControl`](https://www.adaptabletools.com/docs/reference/actioncolumncontrol.md) objects. Each item has a `type` (`button`, `toggle`, `link`, `select`, `command`, `menu`, `form`, or `custom`).

### Action Column Context

Many Control properties are functions of [`ActionColumnContext`](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md):

| Property | Type | Description |
| --- | --- | --- |
| [actionColumn](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#actioncolumn) | [`ActionColumn`](https://www.adaptabletools.com/docs/reference/actioncolumn.md)`<TData>` | Action Column in question |
| [control](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#control) | [`ActionColumnControl`](https://www.adaptabletools.com/docs/reference/actioncolumncontrol.md)`<TData, any>` | The Control currently being rendered / invoked |
| [data](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#data) | `TData` | The current row&#39;s data |
| [primaryKeyValue](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#primarykeyvalue) | `any` | Primary Key Value in current row |
| [rowNode](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#rownode) | `IRowNode<TData>` | Current AG Grid Row Node |
| [value](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#value) | `any` | Control&#39;s current value where applicable (e.g. `toggle`&#39;s checked state or `select`&#39;s selected value) |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/actioncolumncontext.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

### Settings

`settings` is an [`ActionColumnSettings`](https://www.adaptabletools.com/docs/reference/actioncolumnsettings.md) object:

| Property         | Purpose                                                                 |
| ---------------- | ----------------------------------------------------------------------- |
| `width`          | Fixed width in pixels (overrides `autoWidth` and `minWidth`)            |
| `minWidth`       | Minimum width; also used as the width when `autoWidth` is false         |
| `autoWidth`      | Estimates width from static Control labels/icons at setup time          |
| `overflow`       | `'none'` (default) or `'menu'` — collapse overflowing Controls to "⋯"   |
| `resizable`      | Whether the Column can be resized                                       |
| `suppressMenu`   | Hide the Column header menu                                             |
| `suppressMovable`| Prevent dragging the Column                                             |

- `autoWidth` uses **static** labels and icons only. If `label` or `icon` are functions, set `width` explicitly.
- Layout `AutoSizeColumns` does not replace Action Column width calculation.

### Row Scope

By default Controls appear in every row. Use [`rowScope`](https://www.adaptabletools.com/docs/reference/rowscope.md) to exclude grouped, summary or data rows.

Prefer the Control's `hidden` property when the decision is per-row rather than per-row-kind.

### Putting it Together

### Defining an Action Column

This example is the `Update Stars` Button from [Action Column Buttons](https://www.adaptabletools.com/docs/handbook-action-column-button/index.md).

Supply `columnId` and optional `friendlyName`.

Use `settings` for width, menus, and whether the Column can move or resize.

Each item in `controls` needs a `type`. This example is a `button` with a context-aware `label`, `style`, `onClick`, `hidden` and `disabled`.

```ts [[1, 2, "columnId"], [1, 3, "friendlyName"], [2, 4, "settings"], [3, 11, "type"], [3, 12, "label"], [4, 16, "style"], [5, 20, "onClick"], [6, 30, "hidden"], [6, 33, "disabled"]]
{
  columnId: 'update_stars',
  friendlyName: 'Update Stars',
  settings: {
    suppressMenu: true,
    suppressMovable: true,
    resizable: false,
  },
  controls: [
    {
      type: 'button',
      label: (context) =>
        context.data.language === 'JavaScript'
          ? 'Add Star'
          : 'Remove Star',
      style: (context) =>
        context.data.language === 'JavaScript'
          ? {variant: 'raised', tone: 'accent'}
          : {variant: 'raised', tone: 'success'},
      onClick: (context) => {
        const increment =
          context.data.language === 'JavaScript' ? 1 : -1;
        context.adaptableApi.gridApi.setCellValue({
          columnId: 'github_stars',
          newValue: context.data.github_stars + increment,
          primaryKeyValue: context.primaryKeyValue,
          rowNode: context.rowNode,
        });
      },
      hidden: (context) =>
        context.data.name === 'stencil' ||
        context.data.name === 'polymer',
      disabled: (context) => context.data.github_watchers < 200,
    },
  ],
}
```

## AG Grid Column Definitions

In most cases Action Columns are **not** listed in AG Grid `columnDefs`.

AdapTable creates the AG Grid column from Action Column Options.

Sometimes a developer needs an AG Grid feature on that Column — a tooltip, or placing it inside a Column Group.

That is possible: add the Column to `columnDefs` with a [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) of `actionColumn`. AdapTable wires it to the matching Action Column definition.

- Set the [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) to `actionColumn`
- Make sure `colId` in the AG Grid ColDef and `columnId` in the Action Column definition are the same value

See [Adding Special Column Types](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) for more details

**Example: Action Columns: Defining in AG Grid**

Action Columns inside an AG Grid Column Group

- The AG Grid Column Group `Actions` contains 2 Action Columns
- Both Columns were defined in AG Grid Column Defs with header and cell tooltips
- They use `type: ['actionColumn']` so AdapTable can wire them up
### Expand to see the AG Grid ColDef

```js
{
  headerName: 'Actions',
  children: [
    {
      colId: 'add_issue',
      type: ['actionColumn'],
      headerTooltip: 'Add an issue',
    },
    {
      colId: 'delete_issue',
      type: ['actionColumn'],
      headerTooltip: 'Delete an Issue',
    },
  ],
}
```

- Hover over a Button in an Action Column and see the Tooltip which AG Grid provides

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  gridOptions.ts       [template]
  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 {
  ActionColumnContext,
  AdaptableOptions,
  CellUpdateRequest,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

const bumpIssueChange = (
  context: ActionColumnContext<WebFramework>,
  delta: number
) => {
  const cellUpdateRequest: CellUpdateRequest = {
    columnId: 'week_issue_change',
    newValue: (context.data?.week_issue_change ?? 0) + delta,
    primaryKeyValue: context.primaryKeyValue,
    rowNode: context.rowNode,
  };
  context.adaptableApi.gridApi.setCellValue(cellUpdateRequest);
};

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Defining Action Columns in AG Grid',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'add_issue',
        friendlyName: 'Add Issue',
        controls: [
          {
            type: 'button',
            label: 'Up',
            style: {variant: 'outlined', tone: 'info'},
            onClick: context => bumpIssueChange(context, 1),
          },
        ],
      },
      {
        columnId: 'delete_issue',
        friendlyName: 'Delete Issue',
        controls: [
          {
            type: 'button',
            label: 'Down',
            style: {variant: 'outlined', tone: 'warning'},
            onClick: context => bumpIssueChange(context, -1),
          },
        ],
      },
    ],
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'github_stars',
            'license',
            'language',
            'week_issue_change',
            'add_issue',
            'delete_issue',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/columnDefs.ts`**

```ts
import {ColDef, ColGroupDef, ITooltipParams} from 'ag-grid-enterprise';

export const columnDefs: (ColDef | ColGroupDef)[] = [
  {
    field: 'name',
    cellDataType: 'text',
    sortable: true,
  },

  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    headerName: 'Actions',
    children: [
      {
        colId: 'add_issue',
        type: ['calculatedColumn'],
        cellDataType: 'number',
        headerTooltip: 'Add an issue',
        tooltip: (params: ITooltipParams) => {
          return 'Add 1 to ' + params.data.week_issue_change;
        },
      },
      {
        colId: 'delete_issue',
        type: ['calculatedColumn'],
        cellDataType: 'number',
        headerTooltip: 'Delete an Issue',
        tooltip: (params: ITooltipParams) => {
          return 'Delete 1 from ' + params.data.week_issue_change;
        },
      },
    ],
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'created_at',
    headerName: 'Created',
    cellDataType: 'date',
  },
  {
    field: 'has_wiki',
    headerName: 'Has Wiki',
    cellDataType: 'boolean',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'updated_at',
    headerName: 'Updated',
    cellDataType: 'date',
  },
  {
    field: 'pushed_at',
    headerName: 'Pushed',
    cellDataType: 'date',
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {
    field: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {field: 'has_projects', headerName: 'Has Projects', cellDataType: 'boolean'},
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
];
```

## Grouped & Summary Rows

Use `rowScope` to hide Controls on grouped or summary rows.

**Example: Action Columns: Grouped Rows**

Hiding Action Columns in Grouped and Summary Rows

- `rowScope` excludes Grouped Rows and Summary Rows
- The `Add Star` Control appears only on data rows
### Expand to see the Action Column Definition

```ts
rowScope: {
  ExcludeGroupRows: true,
  ExcludeSummaryRows: true,
},
```

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  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 {
  ActionColumnContext,
  AdaptableOptions,
  CellUpdateRequest,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Action Columns Grouping',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'add_star',
        friendlyName: 'Add Star',
        settings: {
          suppressMenu: true,
          resizable: false,
        },
        rowScope: {
          ExcludeGroupRows: true,
          ExcludeSummaryRows: true,
        },
        controls: [
          {
            type: 'button',
            label: 'Add Star',
            style: {variant: 'raised', tone: 'accent'},
            onClick: (context: ActionColumnContext<WebFramework>) => {
              const cellUpdateRequest: CellUpdateRequest = {
                columnId: 'github_stars',
                newValue: (context.data?.github_stars ?? 0) + 1,
                primaryKeyValue: context.primaryKeyValue,
                rowNode: context.rowNode,
              };
              context.adaptableApi.gridApi.setCellValue(cellUpdateRequest);
            },
          },
        ],
      },
    ],
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Grouped Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'name',
            'github_stars',
            'add_star',
            'week_issue_change',
            'github_watchers',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
          ],
          RowGroupedColumns: ['language'],
          AutoSizeColumns: true,
          RowSummaries: [
            {
              Position: 'Top',
              ColumnsMap: {
                github_stars: 'SUM',
              },
            },
          ],
        },
      ],
    },
  },
};
```
