DataGridXL » Docs
Column Settings
Column Parse Function
The parseFunction
is an optional property in the column configuration of the data grid. It allows developers to process and convert input data (such as pasted values or user input) into a format compatible with the column's value. This is particularly useful for normalizing data or handling type conversions.
Usage
The parseFunction
is called whenever a value is entered or pasted into a cell. The result of the function is used to update the cell's value.
Function Signature
parseFunction: (value: string) => any;
- `value`: The raw string input from the user or a paste operation.
Returns: The parsed value to be stored in the cell.
Example
Here’s an example of using parseFunction
to convert string inputs into specific data types:
const grid = new DataGrid([
{
name: "Is Active",
type: "boolean",
parseFunction: (value) => value.toLowerCase() === "true",
},
{
name: "Age",
type: "number",
parseFunction: (value) => {
const num = parseInt(value, 10);
return isNaN(num) ? null : num; // Return null for invalid numbers
},
},
]);
When to Use parseFunction
Use parseFunction in scenarios where:
Input values need to be normalized, e.g., trimming spaces or handling case sensitivity.
Type conversion is required, such as converting strings like "42" into numbers or "true" into booleans.
Validation or preprocessing of input is necessary before updating the cell value.
Notes
If no parseFunction
is provided, the raw input value will be used directly without any processing.
It’s important to ensure that the returned value is compatible with the column’s type and any validation rules.