# Selection Changed Events

Canonical page: https://www.adaptabletools.com/docs/handbook-selecting-changed-event

- AdapTable provides 2 Selection Changed Events:
  - Cell Selection Changed - fired whenever the cell or column selection changes in AG Grid
  - Row Selection Changed - fired whenever the row selection changes in AG Grid
- Both events provide full details of the selection

## Cell Selection Changed

AdapTable fires the [Cell Selection Changed Event](https://www.adaptabletools.com/docs/technical-reference-selecting/index.md) whenever the **Cell** selection in AG Grid changes.

This provides very detailed information of all the currently selected Cells and the Columns they are in.

**Example: Cell Selection**

Selecting Cells in AdapTable

- This demo illustrates how AdapTable provides information about which **cells** are selected in AG Grid.
- In the example we listen to the **Cell Selection Changed Event** and output the results as [System Status](https://www.adaptabletools.com/docs/ui-tutorial-using-toast-notifications/index.md) messages.
- Additionally, if a Single Cell is selected in the `License` column we highlight the row with a yellow font for 2 seconds (using the [Highlighting API functions](https://www.adaptabletools.com/docs/handbook-highlighting-jumping/index.md))

- Open the System Status section of the Settings Panel to see full details of the message

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
  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: 'Cell Selection Changed Event',
  initialState: {
    Dashboard: {
      ModuleButtons: ['SystemStatus'],
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['SystemStatus', 'Export'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export', 'CellSummary'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Export: {
      CurrentFormat: 'Excel',
      CurrentReport: 'Selected Cells',
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          RowSelection: {
            Mode: 'multiRow',
            Checkboxes: true,
            HeaderCheckbox: true,
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

```ts
import {
  AdaptableReadyInfo,
  GridCell,
  RowHighlightInfo,
  SelectedCellInfo,
} from '@adaptabletools/adaptable';
import {CellSelectionChangedInfo} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.eventApi.on(
    'CellSelectionChanged',
    (eventInfo: CellSelectionChangedInfo) => {
      const selectedCellInfo: SelectedCellInfo = eventInfo.selectedCellInfo;
      // Highlight row if single cell selected in License Column
      if (selectedCellInfo.gridCells?.length == 1) {
        const selectedGridCell: GridCell = selectedCellInfo.gridCells[0];
        if (selectedGridCell.column?.columnId == 'license') {
          const rowHighlightInfo: RowHighlightInfo = {
            primaryKeyValue: selectedGridCell.primaryKeyValue,
            highlightStyle: {
              ForeColor: 'var(--ab-color-palette-21)',
            },
            timeout: 2000,
          };
          adaptableApi.gridApi.highlightRow(rowHighlightInfo);
        }
      }

      // Output result of selection to System Status Info
      const selectedColumnValues = selectedCellInfo.gridCells.map(
        item => item.rawValue
      );
      if (selectedColumnValues.length) {
        const message = `${selectedColumnValues.length} Cells Selected`;
        const info = `Cell Values: ${selectedColumnValues.join(',')}`;
        adaptableApi.systemStatusApi.setInfoSystemStatus(message, info);
      }
    }
  );

  // Set some cells to select at the start
  setTimeout(() => {
    adaptableApi.gridApi.selectCellRange({
      columnIds: ['name', 'language'],
      rowIndexStart: 0,
      rowIndexEnd: 4,
    });
  }, 10);
};
```

## Row Selection Changed

AdapTable fires the [Row Selection Changed Event](https://www.adaptabletools.com/docs/technical-reference-selecting/index.md) whenever the **Row** selection in AG Grid changes.

Like AG Grid, AdapTable lists **all** rows that are currently selected, even if they are not part of current filters

Use `isDisplayed` property in the `rowInfo` property to find just a collection of _displayed_ (i.e. filtered) rows:
```ts
api.eventApi.on('RowSelectionChanged', (info: RowSelectionChangedInfo) => {
  const rows: GridRow[] = info.selectedRowInfo.gridRows.filter(
    gr => gr.rowInfo?.isDisplayed
  );
});
```

**Example: Row Selection**

Selecting Rows in AdapTable

- This demo illustrates how AdapTable provides information about which **rows** are selected in AG Grid.
- In the example we listen to the **Row Selection Changed Event** and output the results as [System Status](https://www.adaptabletools.com/docs/ui-tutorial-using-toast-notifications/index.md) messages.
- In the `RowSelection` section of the Layout we set `mode` to *multiRow* and `EnableClickSelection` to *true* to facilitate easy row Selection

- Open the System Status section of the Settings Panel to see full details of the message

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

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

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Selection Changed Event',
  initialState: {
    Dashboard: {
      ModuleButtons: ['SystemStatus'],
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['SystemStatus', 'Export'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Export', 'CellSummary'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Export: {
      CurrentFormat: 'Excel',
      CurrentReport: 'Selected Rows',
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'has_projects',
            'has_pages',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'week_issue_change',
          ],
          Name: 'Standard Layout',
          RowSelection: {
            Mode: 'multiRow',
            Checkboxes: true,
            HeaderCheckbox: true,
            EnableClickSelection: true,
          },
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/onAdaptableReady.ts`**

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

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.eventApi.on(
    'RowSelectionChanged',
    (eventInfo: RowSelectionChangedInfo) => {
      const selectedRowNames = eventInfo.selectedRowInfo.gridRows.map(
        node => node.rowData.name
      );
      if (selectedRowNames.length) {
        const message = `${selectedRowNames.length} Rows Selected`;
        const info = `Names: ${selectedRowNames.join(',')}`;
        adaptableApi.systemStatusApi.setSuccessSystemStatus(message, info);
      }
    }
  );
  setTimeout(() => {
    adaptableApi.gridApi.selectRows([10270250, 11730342], false);
  }, 10);
};
```

**`src/columnDefs.ts`**

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

export const columnDefs: ColDef[] = [
  {
    field: 'name',
    cellDataType: 'text',
  },

  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: 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: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {field: 'has_projects', headerName: 'Has Projects', cellDataType: 'boolean'},
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
];
```

**`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,
    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  rowData: rowData,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
