0

Comboitem value attribute problem

asked 2009-01-19 08:16:42 +0800

HananW gravatar image HananW
160 2

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>

In the above code the value is translated to text (as expected, by if I change it to use EL (value="${MyEmum.enum1}) it gives an error since MyEnum is not defined (even when I use a zscript to import it).

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.

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2009-01-19 10:07:33 +0800

RyanWu gravatar image RyanWu
533 2
about.me/flyworld

updated 2009-01-20 08:22:00 +0800

how about use data binding ?
take a look of this

link publish delete flag offensive edit

answered 2009-01-19 10:42:18 +0800

HananW gravatar image HananW
160 2

updated 2009-01-19 10:55:56 +0800

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"/>

link publish delete flag offensive edit

answered 2009-01-23 01:59:45 +0800

henrichen gravatar image henrichen
3869 2
ZK Team

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

link publish delete flag offensive edit

answered 2009-01-24 10:07:20 +0800

HananW gravatar image HananW
160 2

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.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-01-19 08:16:42 +0800

Seen: 257 times

Last updated: Jan 24 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More