A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.ag-grid.com/javascript-data-grid/reference-data/ below:

JavaScript Grid: Reference Data | AG Grid

This section describes two different strategies for managing reference data in your application. Both approaches implement the same grid example so they can be easily compared.

The term Reference Data is used here in a general way to describe data which can be defined using a key / value pair relationship (e.g. 'tyt': 'Toyota'). This data is typically static in nature, i.e. it is not expected to change between server requests.

The examples contained within this section use the following reference data. Note that the data returned from the server only contains codes (keys) which must be mapped to names (values) for display purposes.


const rowData = [
    { make: 'tyt', exteriorColour: 'fg', interiorColour: 'bw', price: 35000 },
    { make: 'frd', exteriorColour: 'bw', interiorColour: 'cb', price: 32000 },
    ...
]


const carMappings = {
    'tyt': 'Toyota',
    'frd': 'Ford',
    'prs': 'Porsche',
    'nss': 'Nissan'
};

const colourMappings = {
    'cb': 'Cadet Blue',
    'bw': 'Burlywood',
    'fg': 'Forest Green'
};
Using Value Handlers Copy Link

Value Handlers can be used to map keys contained within the row data to their corresponding display values. This approach involves more coding but allows for different data formats and offers more flexibility managing the data.

The main idea of this approach is to use a valueFormatter to convert the code (key) to a value which is displayed in the cell. Then use a valueParser to convert the name back to a code (key) when saving it down into the underlying data.

const gridOptions = {
    columnDefs: [
        {
            field: 'make',
            cellEditor: 'agSelectCellEditor',
            cellEditorParams: {
                values: extractKeys(carMappings) 
            },
            
            valueFormatter: params => {
                return lookupValue(carMappings, params.value);
            },
            
            valueParser: params => {
                return lookupKey(carMappings, params.newValue);
            }
        }
    ],

    
}

When editing using Cell Editors it's important to ensure the underlying data is updated with the codes (keys) rather than the values that are displayed in the cells.

When using the TextCellEditor with a valueFormatter, you may want to display the formatted text rather than the code when editing. In this case you should also include the useFormatter property as follows:

cellEditor: 'agTextCellEditor',
cellEditorParams: {
    useFormatter: true
}
Example: Value Handlers Copy Link

The following example demonstrates how Value Handlers can be combined to work with reference data:

Using the 'refData' Property Copy Link

Here we present the same example but this time using the refData ColDef property. This approach requires less coding and is more straightforward, but might not be flexible enough for scenarios involving more complex reference data formats.

refDataCopy Link

{ [key: string]: string }

Provided a reference data map to be used to map column values to their respective value from the map.

All that is required with this approach is to specify the refData and the grid will take care of the rest, as shown below:

const gridOptions = {
    columnDefs: [
        {
            field: 'make',
            cellEditor: 'agSelectCellEditor',
            cellEditorParams: {
               values: extractKeys(carMappings)
            },
            refData: carMappings
        }
    ],

    
}

Like in the previous example using Value Handlers, where the underlying data contains codes, the grid will use the specified reference data to display the associated values in the cells and save down the codes (keys) in the data when editing.

Example: 'refData' Property Copy Link

The following example demonstrates how the refData property simplifies working with reference data:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4