JavaScript field: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Stamp|2017-01-23|4.2.1|1.0.7}}
{{Stamp|2018-04-09|4.4.5|1.3.5}}


The value of the field will be the result of execution of the script defined in the field and executed in the entry context.
The value of the field will be the result of execution of the script defined in the field and executed in the entry context.
Line 42: Line 42:


Compatibility of Memento field types and JavaScript objects is shown in the table:
Compatibility of Memento field types and JavaScript objects is shown in the table:
{| class="wikitable" style="font-size:120%"
{{FieldTypeReturnValues}}
|-
! Field type !! JavaScript object
|-
| Text , Hyperlink, Password, Phone, Email, Calculation (string result) , Single-choice list , Radio buttons || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String '''String''']
|-
| Integer, Real number, Rating, Currency, Calculation (number result) || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number '''Number''']
|-
| Date , Date/Time, Time || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date '''Date''']
|-
| Checkbox (Boolean) || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean '''Boolean''']
|-
| Multiple-choice list, Checkboxes || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array '''Array'''] of [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String '''String''']
|-
| Link to entry || [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array '''Array'''] of [[Triggers#Object Entry|'''Entry''']]<br/>Each array element has a method ''field()'' for access to values of fields of entries of the linked library.<br/>Example: field("linkField")[i].field("remoteField")
|}


== Time of script execution ==
== Time of script execution ==
Line 205: Line 190:
cost;                              // The result is the COMPUTED COST
cost;                              // The result is the COMPUTED COST
</source>
</source>
==== Example: Controlling entry color ====
The color of an entry in the Entries List screen can be controlled using a JavaScript field. The JavaScript field merely returns a color string in the form "#RRGGBB", where RR is the hexadecimal (hex) value of the red component of the color, GG the hex value for the green component, and BB the hex value of the blue component. The user must identify the JavaScript field by editing the library and selecting the field for the Entry color parameter on the MAIN tab.
You could have many entries with distinct colors indicating things like regions, categories, ranges, etc.
For example, suppose you have an numeric field and want to color entries red if the field is negative and green if it is positive. Here's how you would do it...
Create a JavaScript field, called anything, say "Color of entry". Maybe you have a Currency field called "Amount". You can set the script to be something like...
<source lang="JavaScript">
var amount = field("Amount");
if (amount < 0) "#FF0000";      // Red
else if (amount > 0) "#00FF00"; // Green
</source>
Now, go to the MAIN tab of the Library Edit screen. Tap Entry color and select your new JavaScript field, Color of entry.
So, if your Amount field is negative, it will show in the entries list with a red bar on the left.
If it is positive, it'll show with a green bar on the left.
Otherwise it's zero, in which case it will have no bar on the left (barcolor dictated by the theme you're using -- white if you're using the light theme and black if using the dark theme).


== References ==
== References ==
Line 213: Line 219:
{{Template:JavaScriptLinks}}
{{Template:JavaScriptLinks}}


[Category:Spec]
[[Category:en]] [[Category:Spec]]

Navigation menu