Primary Key

A Primary Key is required by AdapTable for cell identification purposes.

Without it AdapTable would not be able to uniquely identify cells for Cell Summary or Editing, nor specifiy which cells have changed in the Cell Changed Event.

important

If the ​​​showMissingPrimaryKeyWarning ​​property is true in General Options (the default value), AdapTable will warn at start-up if there is no column in the grid which matches the provided value.

Primary Key Column

The Primary Key is set via the (mandatory) primaryKey property in Base Adaptable Options.

The value should be the Id of a column in the grid's data source.

caution

The column does not need to be visible but it does need to exist in the grid's data source.

It is important that Primary Key Column is one which guarantees to contain unique values.

tip

The Alert Module offers a PrimaryKeyDuplicate Predicate which will trigger when an already existing value is entered in the Primary Key column.

note

At the moment it is not possible to provide a composite key of more than one column but this might be available in future versions of AdapTable

Auto Generated Key

If it is absolutely not possible to provide a Primary Key column of your own, AdapTable can auto-generate one.

warning

Only use this option if it is a last resort and there is no column with unique values

Simply set autogeneratePrimaryKey property to true in Adaptable Options and AdapTable will create - and manage - its own internal Primary Key.

Limitations

There are a few limitations which you need to be aware of, if using an auto generated Primary Key:

  • The Free Text Column Module is not available as it requires persistable Primary Key values

  • Live Data updates from Excel back to AdapTable (when using OpenFin) will not work (as Excel needs the column to be in the exported data)

  • The only way to handle providing and updating grid data is through AdapTable's Grid Api. So you must use the following methods for any data manipulation you require:

    important

    This restriction is required because AdapTable needs to intercept all data-related methods in order to ensure Primary Keys are propertly created and then kept correctly updated

    • setGridData or loadGridData for initial data load
    • addGridData to add new columns to the Grid
    • updateGridData to edit / update rows at run-time

Example

To use an auto generated Primary Key requires a few changes in your AdapTable setup.

This is a truncated version of the code in our Getting Started page but with an auto generated Primary Key:

import '@adaptabletools/adaptable/index.css';
import '@adaptabletools/adaptable/themes/dark.css';
import '@ag-grid-community/all-modules/dist/styles/ag-grid.css';
import '@ag-grid-community/all-modules/dist/styles/ag-theme-balham-dark.css';
import Adaptable from '@adaptabletools/adaptable/agGrid';
import { AdaptableOptions, PredefinedConfig, AdaptableApi}
from '@adaptabletools/adaptable/types';
import { GridOptions, ColDef } from '@ag-grid-community/all-modules';
import { AllEnterpriseModules } from '@ag-grid-enterprise/all-modules';
export default async () => {
const gridOptions: GridOptions = {
columnDefs: [
{
headerName: 'Make',
field: 'make',
filter: true,
editable: true,
type: 'abColDefString',
},
{
headerName: 'Model',
field: 'model',
filter: true,
editable: true,
type: 'abColDefString',
},
{
headerName: 'Price',
field: 'price',
filter: true,
editable: true,
type: 'abColDefNumber',
},
],
};
const rowData = [
{ make: 'Toyota', model: 'Yaris', price: 40000 },
{ make: 'Toyota', model: 'Corolla', price: 28000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Ford', model: 'Fiesta', price: 35000 },
],
const demoConfig: PredefinedConfig = {
Dashboard: {
Tabs: [
{
Name: 'Grid',
Toolbars: ['Layout', 'Export','Alert'],
}
],
},
} as PredefinedConfig;
const adaptableOptions: AdaptableOptions = {
// set an empty primary key
primaryKey: '',
userName: 'Demo User',
adaptableId: 'Basic Setup Demo',
// auto generate the Primary Key (only when absolutely necessary)
autogeneratePrimaryKey: true,
predefinedConfig: demoConfig,
vendorGrid: { ...gridOptions, modules: AllEnterpriseModules },
};
const adaptableApi: AdaptableApi = await Adaptable.init(adaptableOptions);
// Because we have an auto generated Primary Key, provide row data via Grid Api
adaptableApi.eventApi.on('AdaptableReady', () => {
adaptableApi.gridApi.setGridData(rowData);
});
};

More Information