# Setting AG Grid Cell Data Types

Canonical page: https://www.adaptabletools.com/docs/dev-guide-aggrid-cell-data-types

- Developers should provide the `cellDataType` property as part of each AG Grid Column definition
- AdapTable will then set the **data types** of the columns, ensuring filters and menus are correctly configured

AdapTable needs to know the correct data type of each Column in AG Grid.

This will ensure that appropriate [Filter Types](https://www.adaptabletools.com/docs/handbook-column-filter/index.md), [Column Menus](https://www.adaptabletools.com/docs/technical-reference-column-menu/index.md) etc. are configured correctly for the Column

AdapTable does this by leveraging the `cellDataType` property that is part of each AG Grid `ColDef` object.

If this is not provided, AdapTable will attempt to work out the DataType of the column, by analysing the data in the first row of the Grid.

Not providing Column Data Types can cause issues, particularly if the data in the first row is missing or ambiguous

## AG Grid Cell Data Types

When setting up Column Definitions, developers should ensure to set the correct `cellDataType` property.

For the vast majority of Columns, the default values provided by AG grid will suffice:

| Column Data Type | AG Grid `cellDataType` value |
| ---------------- | ---------------------------- |
| String           | `text`                       |
| Numeric          | `number`                     |
| Date             | `date`                       |
| Date (as string) | `dateString`                 |
| Boolean          | `boolean`                    |
| Object           | `object`                     |

```tsx {6,11,16,21,27}
const gridOptions: GridOptions = {
  columnDefs: [
    {
      headerName: 'Make',
      field: 'make',
      cellDataType: 'text',
    },
    {
      headerName: 'Released',
      field: 'released',
      cellDataType: 'date',
    },
    {
      headerName: 'Updated',
      field: 'updated',
      cellDataType: 'dateString',
    },
    {
      headerName: 'Bid',
      field: 'bid',
      cellDataType: 'number',
      type: 'pricing', // `type` is for Column Types, not data types
    },
    {
      headerName: 'Automatic',
      field: 'automaticTransmission',
      cellDataType: 'boolean',
    },
  ],
};
```

**Example: AG Grid Cell Data Types**

Using AG Grid Cell Data Types in AdapTable

- This demo shows how AdapTable leverages AG Grid Cell Data Types using a (nonsensical) data set with these columns:
  - `Make` and `Model` are **text**
  - `Price` and `Rating` are **numbers**
  - `Available` is a **boolean**
  - `Made` is a **date** and `Produced` is a **dateString** - but both become AdapTable Dates (and therefore they share the same Format Column)

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  [template]
  rowData.ts
  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 {Car} from './rowData';

export const adaptableOptions: AdaptableOptions<Car> = {
  primaryKey: 'model',
  adaptableId: 'AG Grid Cell Data Types',
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-date',
          Scope: {
            DataTypes: ['date'],
          },
          DisplayFormat: {
            Formatter: 'DateFormatter',
            Options: {
              Pattern: 'MMM do yyyy',
            },
          },
          Style: {
            ForeColor: 'LightBlue',
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'make',
            'model',
            'made',
            'produced',
            'available',
            'pricePerMile',
            'rating',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

**`src/gridOptions.ts`**

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

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

export const gridOptions: GridOptions<Car> = {
  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',
      },
    ],
  },
};
```

**`src/columnDefs.ts`**

```ts
import {ColDef} from 'ag-grid-enterprise';
import {Car} from './rowData';

export const columnDefs: ColDef<Car>[] = [
  {
    headerName: 'Make',
    field: 'make',
    filter: true,
    editable: false,
    enableRowGroup: true,
    enablePivot: true,
    cellDataType: 'text',
  },
  {
    headerName: 'Model',
    field: 'model',
    filter: true,
    editable: false,
    cellDataType: 'text',
  },
  {
    headerName: 'Price',
    field: 'pricePerMile',
    filter: true,
    editable: false,
    cellDataType: 'number',
  },
  {
    headerName: 'Made',
    field: 'made',
    filter: true,
    editable: false,
    cellDataType: 'date',
  },
  {
    headerName: 'Produced',
    field: 'produced',
    filter: true,
    editable: false,
    cellDataType: 'dateString',
  },
  {
    headerName: 'Available',
    field: 'available',
    filter: true,
    editable: true,
    cellDataType: 'boolean',
  },
  {
    headerName: 'Rating',
    field: 'rating',
    enableValue: true,
    editable: true,
    sortable: true,
    cellDataType: 'number',
    filter: true,
    enablePivot: true,
    resizable: true,
  },
];
```

**`src/rowData.ts`**

```ts
export interface Car {
  make: string;
  model: string;
  made: Date;
  produced: string;
  available: boolean;
  pricePerMile: number;
  rating: number;
}

export const rowData: Car[] = [
  {
    make: 'Toyota',
    model: 'Celica',
    made: new Date(2017, 11, 4),
    produced: '4 December 2017',
    available: true,
    pricePerMile: 21.345676,
    rating: 1,
  },
  {
    make: 'Toyota',
    model: 'Yaris',
    made: new Date(2013, 1, 15),
    produced: '15 February 2013',
    available: true,
    pricePerMile: 29.32432423,
    rating: 4,
  },
  {
    make: 'Toyota',
    model: 'Corolla',
    made: new Date(2017, 6, 9),
    produced: '09-July-2017',
    available: false,
    pricePerMile: 32.9032523473287,
    rating: 5,
  },
  {
    make: 'Ford',
    model: 'Mondeo',
    made: new Date(2009, 10, 2),
    produced: '2 November 2009',
    available: true,
    pricePerMile: 28.247893473289,
    rating: 4,
  },
  {
    make: 'Ford',
    model: 'Fiesta',
    made: new Date(2018, 8, 12),
    produced: '12 September 2018',
    available: false,
    pricePerMile: 34.0001,
    rating: 5,
  },
  {
    make: 'Ford',
    model: 'Focus',
    made: new Date(2017, 3, 3),
    produced: '03 April 2017',
    available: false,
    pricePerMile: 31.2432432423,
    rating: 3,
  },
  {
    make: 'Ford',
    model: 'Galaxy',
    made: new Date(2015, 4, 14),
    produced: '14 May 2015',
    available: false,
    pricePerMile: 29.29432404,
    rating: 2,
  },
  {
    make: 'Porsche',
    model: 'Boxter',
    made: new Date(2016, 1, 28),
    produced: '28 Feb 2016',
    available: true,
    pricePerMile: 32.29580292,
    rating: 4,
  },
  {
    make: 'Porsche',
    model: 'Mission',
    made: new Date(2008, 10, 7),
    produced: '7 November 2008',
    available: false,
    pricePerMile: 35.7822957,
    rating: 5,
  },
  {
    make: 'Mitsubbishi',
    model: 'Outlander',
    made: new Date(2017, 11, 14),
    produced: '14 December 2017',
    available: true,
    pricePerMile: 19.224309,
    rating: 4,
  },
];
```

## Custom Cell Data Types

AG Grid also lets you register custom types via `dataTypeDefinitions` on GridOptions.

AdapTable still uses the column's AdapTable data type for filters and menus. [Calculated Columns](https://www.adaptabletools.com/docs/handbook-calculated-column/index.md) and [FreeText Columns](https://www.adaptabletools.com/docs/handbook-freetext-column/index.md) can optionally set `CellDataType` so AG Grid renders the cell with a registered custom type (for example `currency` or `percentage`) while the AdapTable `DataType` stays `number`.

## Array Types

There are a few use cases (all regarding array data) where the AG Grid types are insufficient.

In this case AdapTable provides a number of different array-based cell data types.

See [creating Array Columns](https://www.adaptabletools.com/docs/dev-guide-aggrid-array-columns/index.md) for full details and accompanying demos
