From version 6.1
edited by Robert
on 2018/03/11
To version 7.1
edited by Robert
on 2018/03/11
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,7 +1,7 @@
1 1  Computed Pseudofields, also known als //calculated fields// or //computed fields//, are fields that rather compute than store they value. Unlike the 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 2  
3 += Use cases =
3 3  
4 -=Use cases=
5 5  Computed fields are mainly used to:
6 6  
7 7  * Extract values from another field's value, e.g. the middle inital from a middle name field
... ... @@ -8,8 +8,8 @@
8 8  * Combine multiple fields, e.g. the display name as a combination of first name, middle inital and last name
9 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 10  
11 += Features and Limitations =
11 11  
12 -= Features and Limitations=
13 13  Let's have a look at their features and limitations:
14 14  
15 15  ; defined at design-time
... ... @@ -20,7 +20,63 @@
20 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 21  ; not queryable
22 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 +:
23 23  
26 +(((
27 += Usage =
24 24  
25 -In xwiki the "formula" describing how to compute the value is defined in the script property of the field definition and can be further adjusted using the custom display property.
29 +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.
26 26  
31 +If you create an XClass with a computed property you will see the following in the property editor:
32 +
33 +[[image:image.png||height="730" width="749"]]
34 +
35 +The property, we are most interested in is the "script" property. You can use all the script languages in Xwiki to create your script. If you use velocity for your scripts you also have the some "extras" at your hand (see [[Velocity Tools>>http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVelocityTools]])
36 +
37 +Let's start with the most basic example for a computed field:
38 +
39 +
40 +== Hello World ==
41 +
42 +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.
43 +
44 +{{code}}
45 +Hello World
46 +{{/code}}
47 +)))
48 +
49 +The next version is to use Velocity scripting:
50 +
51 +{{code}}
52 +{{velocity}}
53 +Hello world
54 +{{/velocity}}
55 +{{/code}}
56 +
57 +== Echoing a field ==
58 +
59 +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:
60 +
61 +{{code}}
62 +{{velocity}}
63 +#set($source=$object.getValue('source'))
64 +$source
65 +{{/velocity}}
66 +{{/code}}
67 +
68 +== Getting a substring of a field value ==
69 +
70 +Let's reuse the class from above and use the following code to get the first two letters of the "source" field:
71 +
72 +{{code}}
73 +{{velocity}}
74 +#set($source=$object.getValue('source'))
75 +$source.substring(0,2)
76 +{{/velocity}}
77 +{{/code}}
78 +
79 +But what happens if the source field is empty?
80 +
81 +
82 +

Get Connected