DataField
This is the base class for Model field classes. A field class defines how to handle the data for a particular type of field. Many of these behaviors can be configured on individual field instances.
While defining fields on Model in TypeScript, data fields should be of type ModelFieldConfig
instead of DataField, because it is a union type that gives completion based on specified type.
class Person extends Model {
static name: string = 'Person'
static fields: ModelFieldConfig[] = [
{ name: 'address', type: 'string' },
{ name: 'contact', type: 'int' }
]
}
Calculated fields
A field value can also be calculated using the other data fields, using the calculate config.
const store = new Store({
fields : [
{ name : 'revenue', type : 'number' },
{ name : 'tax', type : 'number' },
{ name : 'net', calculate : record => record.revenue * (1 - (record.tax / 100)) },
],
data : [
{ id : 1, revenue : 100, tax : 30 }
]
});
const record = store.getById(1).net; // 70
Configs
18
Configs
18Setting to true will ensure this field is included in any update/insert request payload
when a Store / Project / CrudManager performs a request.
When this flag is enabled, this field will skip the equality check when store is syncing the new dataset (see syncDataOnLoad config). This means, that even if the new value in new dataset is the same as old, it will still be applied to the model. It is useful in certain edge case scenarios, when the update of the field does not preserve extra context information, which should be provided by other fields.
Lets you define a function to calculate the value of this field based on other record fields. The Model will
then try to auto-detect the dependencies used in the function. The calculate function can also return a
Promise, meaning you can fetch its value from a remote/async source.
const store = new Store({
fields : [
{
name : 'revenue',
type : 'number'
},
{
name : 'tax',
type : 'number'
},
{
name : 'net',
calculate : record => record.revenue * (1 - (record.tax / 100)),
}
],
data : [
{ id : 1, revenue : 100, tax : 30 }
]
});
const record = store.getById(1).net; // 70
You can also pass an object with a dependsOn array of the dependent fields, along with the calculate fn:
const store = new Store({
fields : [
{
name : 'revenue',
type : 'number'
},
{
name : 'tax',
type : 'number'
},
{
name : 'net',
calculate : {
fn : record => record.revenue * (1 - (record.tax / 100)),
dependsOn : ['revenue', 'tax']
}
}
],
data : [
{ id : 1, revenue : 100, tax : 30 }
]
});
const record = store.getById(1).net; // 70
| Parameter | Type | Description |
|---|---|---|
calculate.fn | function | The calculate function |
calculate.fn.record | Model | The record |
calculate.dependsOn | String[] | An array of the other data fields this calculated field depends
on. If omitted, dependent fields are auto-detected and a |
The calculated value or a Promise yielding the value
A column config object for a column to display this field in a grid. For simple, atomic
data types, such as date, string, boolean, number and integer, this is optional
and the appropriate column type can be inferred.
This also provides default values for column configuration if a configured column definition for a grid lacks a property.
For complex fields, such as identifiers which link to other records, a more capable
column type may be specified, for example a type : 'number' field may be configured
with
column : 'percent'
or
column : {
type : 'percent',
width : 100
}
if it represents a percentage value and needs appropriate rendering and editing.
A function that compares two values and returns a value < 0 if the first is less than the second, or 0 if the values are equal, or a value > 0 if the first is greater than the second.
| Parameter | Type | Description |
|---|---|---|
value1 | Model | |
value2 | Model |
Indicates that this field points to nested data (path separated by dots in the field name). Normally this is
set automatically when a field's name or dataSource contains a dot (.).
Configure it as false to allow data field names that actually contain dots.
class MyModel extends Model {
static fields = [
// complexMapping automatically set to true,
// `name.first` will point to { name : { first : "value" } }
{ name : 'name.first', type : 'string' },
// complexMapping automatically set to true,
// `last` will point to { name : { last : "value" } }
{ name : 'last', type : 'string', dataSource : 'name.last' },
// complexMapping set to false,
// `not.nested.name` will point to { "not.nested.name" : "value" }
{ name : 'not.nested.name', complexMapping : false }
];
}
The property in a record's data object that contains the field's value.
Defaults to the field's name.
The default value to assign to this field in a record if no value is provided.
A description used by the AI features to explain the field to the AI agent
An object which names formula prefixes for use when editing this field.
Each entry is keyed by a formula prefix, and each value is how to instantiate and configure a
FormulaProvider when that prefix is used in the typed vale, eg: =XXX(
If the configured value contains a type property, that is used to determine a registered
formula provider subclass to instantiate.
fields : [{
name : 'review',
formulaProviders : {
AI : {
type : 'remote',
url : 'https://my-ai-service.com'
},
SUM : {
type : 'sum'
}
}
}]
Formula providers may be added to dynamically:
// Enable registered MYFormula class to be used as a formula provider in the Grid
grid.store.modelClass.fieldMap.review.formulaProviders.MY = { ... };
By default, defined Model fields may be used to create a grid column
suitable for displaying that field in a grid cell. Some fields may not be suitable for
features which automatically generate columns for view. These fields are created using
internal : true. Some examples are the expanded and rowHeight fields which are used
internally.
The name of the field.
If the name contains a dot (.), it is assumed to be a nested field, and the complexMapping config is
automatically set to true. This means that the field will point to nested data (path separated by dots in
the field name). Configure it as false to allow data field names that actually contain dots. See
complexMapping for more information.
Setting to false indicates that null is not a valid value.
Set to false to exclude this field when saving records to a server.
Set to true for the field's set accessor to ignore attempts to set this field.
Properties
6
Properties
6Functions
19
Functions
19Other
This method transforms a data value into the desired form for storage in the record's data object.
export default class Task extends TaskModel {
static get fields() {
return [
{
name : 'status',
convert : (value, data) => {
if (value >= 100) {
return 'done';
}
else if (value > 0) {
return 'started';
}
}
}
];
}
}
| Parameter | Type | Description |
|---|---|---|
value | * | The value to convert for storage in a record. |
data | Object | The raw record data object |
The converted value.
Compares two values for this field and returns true if they are equal, and false if not.
| Parameter | Type | Description |
|---|---|---|
first | * | The first value to compare for equality. |
second | * | The second value to compare for equality. |
true if first and second are equal.
Returns the given field value as a String. If value is null or undefined, the value specified by
nullText is returned.
| Parameter | Type | Description |
|---|---|---|
value | * | The value to convert to a string. |
Returns the given, non-null field value as a String.
| Parameter | Type | Description |
|---|---|---|
value | * | The value to convert to a string (will not be |