Calculated fields
Calculate Field, whose value is automatically calculated based on other field values, can implement DataField.valueExpression and valueCallback.
And the values of calculated fields cannot be modified. Value cannot be changed with setValue(), cannot be edited, and can only be used as ReadOnly.
valueExpression
You can refer to values using values['field name'] and values[field index], and references to other calculated fields in formulas are not allowed.
The demo below shows Payment Amount = Payment Number * Payment Amount
.
var fields = [
...
{
fieldName: "PaymentAmount",
dataType: "number",
valueExpression: "values['ToMonth'] * values['SaveCost']"
}
];
dataProvider.setFields(fields);
valueCallback
If the value being calculated is not a simple arithmetic operation and requires various calculation formulas, you can use valueCallback. If the calculations require a lot of time, the grid can be slow. The demo below shows ‘payment amount = payment number * payment amount * 2’ when the number of payments is 15 or more.
var fields = [
...
{
fieldName: "PaymentAmount",
dataType: "number",
valueCallback: function (prod, dataRow, fieldName, fieldNames, values) {
let paymentCount = values[fieldNames.indexOf("ToMonth")];
let paymentAmount = values[fieldNames.indexOf("SaveCost")];
if (isNaN(paymentCount) || isNaN(paymentAmount))
return undefined;
else
return paymentCount >= 15 ? paymentCount * paymentAmount * 2 : paymentCount * paymentAmount;
}
}
];
dataProvider.setFields(fields);