# Action Column Commands

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

- A `command` Control runs a built-in Row action
- The 4 Commands are `create`, `clone`, `edit` and `delete`
- Most of these are shortcuts to open the matching AdapTable Row Form

Action Columns can be provided with a Command Button.

This is a built-in alternative to wiring up a custom `onClick` yourself.

These are designed to cater for the most common use cases and to reduce development overhead

AdapTable provides 4 Action Column Commands, which open the matching [Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md):

| Command  | Action when clicked                                                                                        |
| -------- | ---------------------------------------------------------------------------------------------------------- |
| `create` | Opens the [Create Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md#create-row-form)                  |
| `clone`  | Opens the [Clone Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md#clone-row-form)                    |
| `edit`   | Opens the [Edit Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md#edit-row-form)                      |
| `delete` | Triggers the (non-visible) [Delete Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md#delete-row-form) |

If no `icon` is provided, AdapTable uses a default one for that Command.

### Defining Command Controls

Put the Column in `actionColumnOptions.actionColumns`.

Supply `columnId` and an optional `friendlyName`.

Each Command Control has `type: 'command'`.

`command` is `'create'`, `'clone'`, `'edit'` or `'delete'`.

Add an optional `label` to place next to the icon.

Provide an icon for the Command button.

AdapTable renders the default icon if none is provided.

```ts [[1, 1, "actionColumnOptions"], [1, 2, "actionColumns"], [2, 4, "columnId"], [2, 5, "friendlyName"], [3, 8, "type"], [3, 13, "type"], [3, 18, "type"], [4, 9, "command"], [4, 14, "command"], [4, 19, "command"], [5, 15, "label"], [5, 20, "label"], [6, 10, "icon"]]
actionColumnOptions: {
  actionColumns: [
    {
      columnId: 'action',
      friendlyName: 'Actions',
      controls: [
        {
          type: 'command',
          command: 'clone', // opens Clone Row Form
          icon: {name: 'pie-chart'},
        },
        {
          type: 'command',
          command: 'edit', // opens Edit Row Form
          label: 'Edit',
        },
        {
          type: 'command',
          command: 'delete', // triggers Delete Row Form event
          label: 'Delete',
        },
      ],
    },
  ],
},
```

**Example: Action Column Commands**

Using Action Column Commands

- The `Actions` Column contains 3 Command Controls:
  - `Clone`
  - `Edit`
  - `Delete`

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

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Action Column Commands',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Actions',
        controls: [
          {type: 'command', command: 'clone'},
          {type: 'command', command: 'edit'},
          {type: 'command', command: 'delete'},
        ],
      },
    ],
  },
  rowFormOptions: {
    autoHandle: true,
    disableInlineEditing: true,
    setPrimaryKeyValue: context => ({
      ...context.rowData,
      id: context.adaptableApi.gridApi.getRowCount() + 1,
    }),
  },
  initialState: {
    StatusBar: {
      StatusBars: [{Key: 'Center Panel', StatusBarPanels: ['Alert']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'action',
            'name',
            'github_stars',
            'week_issue_change',
            'github_watchers',
            'license',
            'language',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Pinning Command Columns

Action Columns can use standard [Column Pinning](https://www.adaptabletools.com/docs/handbook-layouts-table-pinning/index.md) so the Command stays visible.

**Example: Action Column Commands Pinned**

Pinned Edit and Delete Commands

- `Edit` is pinned left (with a label)
- `Delete` is pinned right

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

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Action Column Commands Pinned',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'edit',
        friendlyName: ' ',
        controls: [
          {
            type: 'command',
            command: 'edit',
            label: 'Edit',
          },
        ],
      },
      {
        columnId: 'delete',
        friendlyName: ' ',
        controls: [{type: 'command', command: 'delete'}],
      },
    ],
  },
  rowFormOptions: {
    autoHandle: true,
    disableInlineEditing: true,
    setPrimaryKeyValue: context => ({
      ...context.rowData,
      id: context.adaptableApi.gridApi.getRowCount() + 1,
    }),
  },
  initialState: {
    StatusBar: {
      StatusBars: [{Key: 'Center Panel', StatusBarPanels: ['Alert']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'edit',
            'name',
            'github_stars',
            'week_issue_change',
            'github_watchers',
            'license',
            'language',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'delete',
          ],
          ColumnPinning: {
            edit: 'left',
            delete: 'right',
          },
          ColumnSizing: {
            edit: {Width: 50},
            delete: {Width: 50},
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Configuring Commands

Command Buttons are very configurable - style, label, visibility etc can all be set on a per-row basis.

- The Command Controls use the same [`BaseControl`](https://www.adaptabletools.com/docs/reference/basecontrol.md) properties as every other Action Column Control
- i.e. `label`, `style`, `icon`, `hidden`, `disabled`, and optional `confirm`

**Example: Configuring Action Column Commands**

Hidden, disabled and styled Commands

- `clone` is **hidden** when `Language` is _TypeScript_ (and has a custom icon)
- `edit` is **disabled** when `Language` is _HTML_
- `delete` **tone** changes when `Language` is _JavaScript_

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

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Action Column Commands Configuring',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Actions',
        controls: [
          {
            type: 'command',
            command: 'clone',
            icon: {name: 'pie-chart'},
            hidden: (context: ActionColumnContext<WebFramework>) =>
              context.data?.language === 'TypeScript',
          },
          {
            type: 'command',
            command: 'edit',
            disabled: (context: ActionColumnContext<WebFramework>) =>
              context.data?.language === 'HTML',
          },
          {
            type: 'command',
            command: 'delete',
            style: (context: ActionColumnContext<WebFramework>) => ({
              variant: 'text',
              tone:
                context.data?.language === 'JavaScript' ? 'info' : 'error',
            }),
          },
        ],
      },
    ],
  },
  rowFormOptions: {
    autoHandle: true,
    disableInlineEditing: true,
    setPrimaryKeyValue: context => ({
      ...context.rowData,
      id: context.adaptableApi.gridApi.getRowCount() + 1,
    }),
  },
  initialState: {
    StatusBar: {
      StatusBars: [{Key: 'Center Panel', StatusBarPanels: ['Alert']}],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'action',
            'name',
            'github_stars',
            'week_issue_change',
            'github_watchers',
            'license',
            'language',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
