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
alwaysWrite: Boolean= false

Setting 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.

calculate: Object | function

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
ParameterTypeDescription
calculate.fnfunction

The calculate function

calculate.fn.recordModel

The record

calculate.dependsOnString[]

An array of the other data fields this calculated field depends on. If omitted, dependent fields are auto-detected and a console.warn debug messages will be issued if it fails, and then you will need to declaratively add the dependent fields.

Returns: * | Promise -

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.

compare: function

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.

ParameterTypeDescription
value1Model
value2Model
Returns: Number

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 }
   ];
}
dataSource: String

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.

description: String

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 = { ... };
internal: Boolean= false

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.

label: String

The label text for a form item generated for this field. This is also used to create a column header for a column for this field.

If no label is specified, the value yielded by this property is a prettified version of the field name created using separate.

name: String

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.

nullable: Boolean= true

Setting to false indicates that null is not a valid value.

nullText: String

The value to return from print for a null or undefined value.

The value to replace null when the field is not nullable.

persist: Boolean= true

Set to false to exclude this field when saving records to a server.

readOnly: Boolean= false

Set to true for the field's set accessor to ignore attempts to set this field.

Properties

6

Class hierarchy

isDataField: Boolean= truereadonly
Identifies an object as an instance of DataField class, or subclass thereof.
isDataField: Boolean= truereadonlystatic
Identifies an object as an instance of DataField class, or subclass thereof.

Lifecycle

configBase

Functions

19

Other

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';
                   }
               }
           }
       ];
   }
}
ParameterTypeDescription
value*

The value to convert for storage in a record.

dataObject

The raw record data object

Returns: * -

The converted value.

Compares two values for this field and returns true if they are equal, and false if not.

ParameterTypeDescription
first*

The first value to compare for equality.

second*

The second value to compare for equality.

Returns: Boolean -

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.

ParameterTypeDescription
value*

The value to convert to a string.

Returns: String

Returns the given, non-null field value as a String.

ParameterTypeDescription
value*

The value to convert to a string (will not be null or undefined).

Returns: String

This method transforms a data value into the desired form for transmitting to a server.

ParameterTypeDescription
value*

The value to serialize

recordModel

The record that contains the value being serialized.

Returns: * -

The serialized value.

Configuration

applyDefaultsstaticBase

Events

Lifecycle

destroystaticBase

Misc

initClassstaticBase
isOfTypeNamestaticBase
mixinstaticBase