Show last authors
1 Computed Pseudofields, also known as calculated fields or computed fields, are fields that rather compute than store their value. Unlike other field types, like text fields, they contain a "formula" or script that describes the computation of the value instead of a user-entered value.
2
3 = Use cases =
4
5 Computed fields are mainly used to:
6
7 * Extract values from another field's value, e.g. the middle initial from a middle name field.
8 * Combine multiple fields, e.g. the display name is a combination of first, middle and last names.
9 * Calculations based on a value from one or more other fields, e.g. the grand total based on the VAT and the net price fields.
10
11 = Features and Limitations =
12
13 Let's have a look at their features and limitations:
14
15 ; defined at design-time
16 : Calculated fields are entirely defined by the author of a class, that means standard users without programming rights cannot change anything about them. They don't have a value property and the script is defined by the author of the class.
17 ; calculated at run-time
18 : Everytime a page containing a computed field is requested the value of the computed field is calculated. That means, that the computed field always shows current value, even if it is used to display something highly volatile like the current date and time. But that also means, that you should not overdo it with the number of computed fields and their complexity, else you wiki may decrease in performance
19 ; read-only
20 : The standard display for view and edit mode of an calculated field is read-only. So users of your wiki cannot enter any data into the calculated field but only read the value of the calculated field. The value of the computed field will change if the user changes a value of a field that the computed field is based upon.
21 ; not queryable
22 : Unlike other fields the value of the computed field is not stored in the database and therefore cannot be part of xwql or hql queries.
23
24 (((
25 = Usage =
26
27 Now it's time to have a look into how you use computed fields. To be ready to start you should have some knowledge about defining and using an XClass. 
28
29 If you create an XClass with a computed property you will see the following in the property editor:
30
31 {{image reference="1520812187988-900.png" width="736"/}}
32
33 The property we are most interested in is the script property. You can use any of the script languages in XWiki to create your script. If you use Velocity for your scripts you also have some "extras" at your hand (see [[Velocity Tools>>http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVelocityTools]]).
34
35 Let's start with the most basic example for a computed field:
36
37 == Hello World ==
38
39 This example only displays the string "Hello World" in a computed field. This can be achieved in multiple ways, the most basic is to use the string "Hello World" as script.
40
41 {{code}}
42 Hello World
43 {{/code}}
44 )))
45
46 The next version is to use Velocity scripting:
47
48 {{code}}
49 {{velocity}}
50 Hello world
51 {{/velocity}}
52 {{/code}}
53
54 == Echoing a field ==
55
56 For this example you need a class with a field "source" of type string and a calculated field "echo". Paste the following code into your script to see the value of the field "source" echoed in the "echo" field:
57
58 {{code}}
59 {{velocity}}
60 #set($source=$object.getValue('source'))
61 $source
62 {{/velocity}}
63 {{/code}}
64
65 == Getting a substring of a field value ==
66
67 Let's reuse the class from above and use the following code to get the first two letters of the "source" field:
68
69 {{code}}
70 {{velocity}}
71 #set($source=$object.getValue('source'))
72 $source.substring(0,2)
73 {{/velocity}}
74 {{/code}}
75
76 But what happens if the source field is empty?
77
78 {{image reference="image.png" width="729"/}}
79
80 This is not good, so let's prepare for source being empty:
81
82 {{code}}
83 {{velocity}}
84 #set($source=$object.getValue('source'))
85 #if($source.length()>=2)
86 $source.substring(0,2)
87 #end
88 {{/velocity}}
89 {{/code}}

Get Connected