Conditional Evaluation"
m (remove empty version history (via JWB)) |
|||
(13 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{ZKDevelopersReferencePageHeader}} | {{ZKDevelopersReferencePageHeader}} | ||
=If and Unless= | =If and Unless= | ||
− | The | + | The component creation can be conditional. By specifying the <code>if</code>, <code>unless</code> attribute or both, developers can control whether to create the associated component. It is also the most straightforward way. |
− | For example, suppose we want to use [[ZK Component Reference/Essential Components/Label|label]], if readonly, and [[ZK Component Reference/Input/Textbox|textbox]], otherwise: | + | For example, suppose that we want to use [[ZK Component Reference/Essential Components/Label|label]], if readonly, and [[ZK Component Reference/Input/Textbox|textbox]], otherwise: |
<source lang="xml"> | <source lang="xml"> | ||
Line 10: | Line 10: | ||
</source> | </source> | ||
− | + | Besides, if a parent component is ignored (not created), all of its child components are ignored too. | |
+ | |||
+ | Here is another example: | ||
<source lang="xml"> | <source lang="xml"> | ||
Line 17: | Line 19: | ||
</window> | </window> | ||
</source> | </source> | ||
+ | * [[ZK Component Reference/Containers/Window|window]] is created only if <code>a</code> is 1 and <code>b</code> is not 2. | ||
=Switch and Case= | =Switch and Case= | ||
− | With the < | + | With the <code>switch</code> and <code>case</code> attributes of [[ZUML Reference/ZUML/Elements/zk|the <code>zk</code> element]], you can evaluate a section of a ZUML document only if a variable matches a certain value. It is similar to Java's switch statement. |
For example, | For example, | ||
Line 40: | Line 43: | ||
ZK Loader will evaluate from the first case to the last case, until it matches the switch condition, which is the value specified in the switch attribute. The evaluation is mutually exclusive conditional. Only the first matched case is evaluated. | ZK Loader will evaluate from the first case to the last case, until it matches the switch condition, which is the value specified in the switch attribute. The evaluation is mutually exclusive conditional. Only the first matched case is evaluated. | ||
− | The zk element without any case | + | The zk element without any case is the default – i.e., it always matches and is evaluated if all the cases above it failed to match. |
== Multiple Cases == | == Multiple Cases == | ||
Line 55: | Line 58: | ||
== Regular Expressions == | == Regular Expressions == | ||
− | Regular expressions are allowed in the < | + | Regular expressions are allowed in the <code>case</code> attribute too, as shown below. |
<source lang="xml"> | <source lang="xml"> | ||
Line 67: | Line 70: | ||
== Used with forEach == | == Used with forEach == | ||
− | Like any other elements, you can use | + | Like any other elements, you can use the [[ZK Developer's Reference/UI Composing/ZUML/Iterative Evaluation|forEach attribute]] (so are if and unless). The <code>forEach</code> attribute is evaluated first, so the following is the same as multiple cases. |
<source lang="xml"> | <source lang="xml"> | ||
Line 76: | Line 79: | ||
<source lang="xml"> | <source lang="xml"> | ||
− | <zk case="apple, orange"> | + | <zk case= "apple, orange"> |
</source> | </source> | ||
=Choose and When= | =Choose and When= | ||
− | The < | + | The <code>choose</code> and <code>when</code> attributes of [[ZUML Reference/ZUML/Elements/zk|the <code>zk</code> element]] are the third approach of conditional evaluation. |
− | As shown below, it is enclosed with a < | + | As shown below, it is enclosed with a <code>zk</code> element with the <code>choose</code> attribute, and the ZK Loader will evaluate its child elements (the <code>zk</code> elements with the <code>when</code> attribute) one-by-one until the first one matches: |
<source lang="xml"> | <source lang="xml"> | ||
Line 96: | Line 99: | ||
</source> | </source> | ||
− | You don't have to assign any value to the < | + | You don't have to assign any value to the <code>choose</code> attribute, which is used only to identify the range of the mutually exclusive conditional evaluation. |
+ | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{{ZKDevelopersReferencePageFooter}} | {{ZKDevelopersReferencePageFooter}} |
Latest revision as of 05:54, 6 February 2024
If and Unless
The component creation can be conditional. By specifying the if
, unless
attribute or both, developers can control whether to create the associated component. It is also the most straightforward way.
For example, suppose that we want to use label, if readonly, and textbox, otherwise:
<label value="${customer.label}" if="${param.readonly == 'true'}"/>
<textbox value="${customer.value}" unless="${param.readonly == 'true'}"/>
Besides, if a parent component is ignored (not created), all of its child components are ignored too.
Here is another example:
<window if="${a==1}" unless="${b==2}">
...
</window>
- window is created only if
a
is 1 andb
is not 2.
Switch and Case
With the switch
and case
attributes of the zk
element, you can evaluate a section of a ZUML document only if a variable matches a certain value. It is similar to Java's switch statement.
For example,
<zk switch="${fruit}">
<zk case="apple">
Evaluated only if ${fruit} is apple
</zk>
<zk case="${special}">
Evaluated only if ${fruit} equals ${special}
</zk>
<zk>
Evaluated only if none of the above cases matches.
</zk>
</zk>
ZK Loader will evaluate from the first case to the last case, until it matches the switch condition, which is the value specified in the switch attribute. The evaluation is mutually exclusive conditional. Only the first matched case is evaluated.
The zk element without any case is the default – i.e., it always matches and is evaluated if all the cases above it failed to match.
Multiple Cases
You can specify a list of cases in one case attribute, such that a section of a ZUML document has to be evaluated if one of them matches.
<zk switch="${fruit}">
<zk case="apple, ${special}">
Evaluated if ${fruit} is either apple or ${special}
</zk>
</zk>
Regular Expressions
Regular expressions are allowed in the case
attribute too, as shown below.
<zk switch="${fruit}">
<zk case="/ap*.e/">
Evaluate if the regular expression, ap*.e"., matches the switch condition.
</zk>
</zk>
Used with forEach
Like any other elements, you can use the forEach attribute (so are if and unless). The forEach
attribute is evaluated first, so the following is the same as multiple cases.
<zk case="${each}" forEach="apple, orange">
is equivalent to
<zk case= "apple, orange">
Choose and When
The choose
and when
attributes of the zk
element are the third approach of conditional evaluation.
As shown below, it is enclosed with a zk
element with the choose
attribute, and the ZK Loader will evaluate its child elements (the zk
elements with the when
attribute) one-by-one until the first one matches:
<zk choose="">
<zk when="${fruit == 'apple'}">
Evaluated if the when condition is true.
</zk>
<zk><!-- default -->
Evaluated if none of above cases matches.
</zk>
</zk>
You don't have to assign any value to the choose
attribute, which is used only to identify the range of the mutually exclusive conditional evaluation.