-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I would like to set the comboitem value attribute to an enum value:
Assume I have an enum class (MyEnum) with values: enum1, enum2.
Consider the following code:
<combobox> <comboitem label="Label1" value="MyEnum.enum1"/> <comboitem label="Label2" value="MyEnum.enum2"/> </combobox>
The only solution I found is to use the following code:
<zscript> import MyEnum; value1=MyEnum.enum1; value2=MyEnum.enum2; </zscript> <combobox> <comboitem label="Label1" value="${value1}"/> <comboitem label="Label2" value="${value2}"/> </combobox>
Is there a better way to this, without using the zscript to define vaiables?
Thanks,
Hanan.
how about use data binding ?
take a look of this
1. I am trying to avoid zscript (I think it look nicer).
2. I have other cases where I want to use enum values in EL expressions e.g.:
<intbox if="item.type == MyEnum.enum1"/> <doublebox if="item.type == MyEnum.enum2"/>
Well, it is a limit of the EL by spec.. You might have to write some static "adaptive functions" to handle such Enum issue. Then you can use <?xel-method ...?> directive to "register" your "adaptive function"; then you can use it.
Details regarding xel-method here:
http://www.zkoss.org/doc/devref-single/index.html#id4857014
Thanks for your answer, this works great.
I am posting here my code in case someone else will have the same problem:
My class code:
public class CustomEl { // since all my enums are in the same package I do not want to rewrite it in the EL expression private static final String ENUM_PREFIX = "com.company.enums"; public static Enum getEnum(String pValue) throws ClassNotFoundException { String[] tValues = pValue.split("\\."); return Enum.valueOf((Class<Enum>)Class.forName(ENUM_PREFIX + tValues[0]), tValues[1]); } }
My zul page:
<?xel-method prefix="e" name="enum" class="com.hanan.zul.CustomEl" signature="java.lang.Enum getEnum(java.lang.String)"?> <combobox> <comboitem label="Label1" value="${e:enum('MyEnum.enum1')}"/> <comboitem label="Label2" value="${e:enum('MyEnum.enum2')}"/> </combobox>
Regards,
Hanan.
Asked: 2009-01-19 08:16:42 +0800
Seen: 257 times
Last updated: Jan 24 '09