JavaScript field: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 105: | Line 105: | ||
==== Example: Birthday==== | ==== Example: Birthday==== | ||
We have birthday field. If a person younger than 18 years we want | We have a birthday field. If a person is younger than 18 years old, we want to display the string "youth", otherwise the string "adult". | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var years = moment().diff(field('Birthday') , 'years' ) | var years = moment().diff(field('Birthday') , 'years' ) | ||
Line 112: | Line 112: | ||
else | else | ||
'Adult' | 'Adult' | ||
</source> | |||
=== Examples of calculations on linked entries === | |||
If you have a many-to-many Link To Entry field — linking the current entry to zero or more entries in another library — and if you have an amount field in that other library, you may want to perform calculations on that field across those entries, in similar fashion to Memento aggregation functions. | |||
==== Example: Sum of linked amounts ==== | |||
<source lang="javascript"> | |||
var amountField = "Amount"; // CHANGE Amount to your amount field name | |||
var linkedEntries = field("Them"); // CHANGE Them to your link field name | |||
var numLE = linkedEntries.length; // The number of linked entries | |||
// Loop through linked entries, creating the sum | |||
for (var lex = 0, result = 0; lex < numLE; lex++) | |||
result += linkedEntries[lex].field(amountField); | |||
result; // The result is the SUM | |||
</source> | |||
==== Example: Average of linked amounts ==== | |||
<source lang="javascript"> | |||
var amountField = "Amount"; // CHANGE Amount to your amount field name | |||
var linkedEntries = field("Them"); // CHANGE Them to your link field name | |||
var numLE = linkedEntries.length; // The number of linked entries | |||
// Loop through linked entries, creating the sum | |||
for (var lex = 0, result = 0; lex < numLE; lex++) | |||
result += linkedEntries[lex].field(amountField); | |||
if (numLE != 0) | |||
result / numLE; // The result is the AVERAGE | |||
</source> | |||
==== Example: Minimum of linked amounts ==== | |||
<source lang="javascript"> | |||
var amountField = "Amount"; // CHANGE Amount to your amount field name | |||
var linkedEntries = field("Them"); // CHANGE Them to your link field name | |||
var numLE = linkedEntries.length; // The number of linked entries | |||
var temp; | |||
// Loop through linked entries, finding the MINIMUM | |||
for (var lex = 0, result = 0; lex < numLE; lex++) { | |||
temp = linkedEntries[lex].field(amountField); | |||
if (temp < result) | |||
result = temp; | |||
} | |||
result; // The result is the MINIMUM | |||
</source> | |||
==== Example: Maximum of linked amounts ==== | |||
<source lang="javascript"> | |||
var amountField = "Amount"; // CHANGE Amount to your amount field name | |||
var linkedEntries = field("Them"); // CHANGE Them to your link field name | |||
var numLE = linkedEntries.length; // The number of linked entries | |||
var temp; | |||
// Loop through linked entries, finding the MAXIMUM | |||
for (var lex = 0, result = 0; lex < numLE; lex++) { | |||
temp = linkedEntries[lex].field(amountField); | |||
if (temp > result) | |||
result = temp; | |||
} | |||
result; // The result is the MAXIMUM | |||
</source> | </source> | ||