3 Replies Latest reply on May 27, 2008 1:11 AM by Stephen Friedrich

    How do you customize the label for s:selectItems

    Jim Barrows Newbie

      My
      selectItems value="myVal" var="myVar" label="mylabel"
      tag, needs to have a customized label.  My collection contains multiple different classes, and the label value will change depending on the class. 
      I've tried facets and h:outputText, any other way to get selectItems to customize the label?

        • 1. Re: How do you customize the label for s:selectItems
          Stephen Friedrich Novice

          Your description is a little vague, so I'm not sure if I understand  the problem correctly.
          IMHO you should have a common interface for your entities (actually you don't even need the interface as long as each class has a getLabel() method).


          If that won't do I guess you can always have a helper method like



          <s:selectItems value="#{myVal}" var="myVar" label="#{someBean.getLabel(myVar)}"/>




          You could define an EL function for that getLabel method, but I usually have an application scoped seam componente utils for such methods.

          • 2. Re: How do you customize the label for s:selectItems
            Jim Barrows Newbie

            I have a collection of different classes that can be selected.  Some need one getter called, the others may need two different getters to display as a label.  I just need to be able to do something like rendered = #{ beanName.class.simpleName = 'ClassName'} 
            I'll try it the way you suggest.  I was hoping I was missing something on the tag though. :)
            I'm of the models shouldn't have anything that relates to the view in them camp, and these are all model objects in the list. 

            • 3. Re: How do you customize the label for s:selectItems
              Stephen Friedrich Novice

              Hm, now that I saw your last post, I think this should indeed work, too (though it doesn't feel quite right, either)


              <s:selectItems value="#{myVal}" var="myVar" label="#{myVar.class.simpleName == 'LabeledEntity' ? myVar.getLabel(myVar)} : myVar.getName()"/>



              You can nest the ternary operator to err, increase flexibility (while decreasing readability).