1

I have a RoleStatus enum which is as being a property of Role entity mapped to an integer in the DB (but that part is irrelevant). I'd like to present a List<Role> in a <p:dataTable> in which one column should have a <h:selectOneMenu> for the RoleStatus property of the Role entity. How can I implement this with or without OmniFaces?

Here's the enum:

public enum RoleStatus {

    ACTIVE(1, "Active"),
    DISABLE(2, "Disable");

    private final int intStatus;
    private final String status;

    private RoleStatus(int intStatus, String status) {
        this.intStatus = intStatus;
        this.status = status;
    }

    public int getIntStatus() {
        return status;
    }

    public String getStatus() {
        return status;
    }

}

Here's the backing bean:

@ManagedBean
@ViewScoped
public class RoleController {

    private List<Role> roles;

    @ManagedProperty("#{roleService}")
    private IRoleService roleService;

    @PostConstruct
    public void init() {
        roles = roleService.getRoles();
    }

    public List<Role> getRoles() {
        return roles;
    }

}

Finally, the data table where I'd like to have a <h:selectOneMenu> for the RoleStatus property of Role entity, showing all available enum values as select item options.

<h:form id="roleForm">
    <p:dataTable value="#{roleController.roles}" var="role">
        <p:column>
            <h:outputText value="#{role.roleid}" />
        </p:column>
        <p:column>
            <h:inputText value="#{role.role}" />
        </p:column>
        <p:column>
            <h:inputText value="#{role.description}" />
        </p:column>
        <p:column>
            <h:selectOneMenu value="#{role.roleStatus}">
                <!-- How??? -->
            </h:selectOneMenu>
        </p:column>
    </p:dataTable>
</h:form>

How can I achieve this? Do I need the OmniFaces SelectItemsConverter?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Abiodun Osinaike
  • 155
  • 1
  • 4
  • 14
  • possible duplicate of [JSF 2.0: use Enum values for selectOneMenu](http://stackoverflow.com/questions/2868778/jsf-2-0-use-enum-values-for-selectonemenu) – kolossus Aug 01 '14 at 11:40
  • Not quite! i have the the data showing as Integer in database and datatable. i need to display it correctly in the datatable as say "Active" or "disabled" dpending on the integer value. – Abiodun Osinaike Aug 01 '14 at 12:00
  • using omnifaces Converter, i was able to get the status column to display but as arrays of "[Active, Disabled]". some of the values need to be "Active" and Some "Disabled" – Abiodun Osinaike Aug 01 '14 at 12:25
  • Like one of the answers in the post I linked , you just need to implement a converter – kolossus Aug 01 '14 at 12:29
  • Am sorry but i don't have issues with drop-downs. i have issues with the datatable. – Abiodun Osinaike Aug 01 '14 at 13:28
  • You're missing the point by miles: the same principle that applies to all jsf controls. Please [**read the JSF wiki**](http://stackoverflow.com/tags/jsf/info) to learn more about JSF and converters – kolossus Aug 01 '14 at 13:31

1 Answers1

6

You don't need a converter. JSF has already a builtin enum converter.

Without OmniFaces, here's how you could provide the enum values as available items of <h:selectOneMenu>:

  1. Add this method to RoleController:

    public RoleStatus[] getRoleStatuses() {
        return RoleStatus.values();
    }
    

    This way, all enum values are available by #{roleController.roleStatuses}.

  2. Then use this dropdown:

    <h:selectOneMenu value="#{role.roleStatus}">
        <f:selectItems value="#{roleController.roleStatuses}" var="roleStatus"
            itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
    </h:selectOneMenu>
    

    Note: as those values are static/applicationwide, it doesn't hurt to move the method to a separate @ApplicationScoped bean.


With OmniFaces, you could remove the additional getter and just import the enum directly via <o:importConstants>:

  1. Add this somewhere in top of your template (assuming that it's in com.example package):

    <o:importConstants type="com.example.RoleStatus" />
    

    This way, the enum class itself is available by #{RoleStatus} (note the capitalization!).

  2. Then use this dropdown:

    <h:selectOneMenu value="#{role.roleStatus}">
        <f:selectItems value="#{RoleStatus.values()}" var="roleStatus"
            itemValue="#{roleStatus}" itemLabel="#{roleStatus.status}" />
    </h:selectOneMenu>
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I believe the Ominfaces library has been updated in the mean time so that ` – frIT Feb 05 '19 at 11:31
  • @fr13d: nothing changed there. I was just using `values()` because item label needed `#{roleStatus.status}` and this was just easier. – BalusC Feb 06 '19 at 11:19