Question

hi every one I have this scenario :

i am using primefaces data table p:datatable and it receives a list from managed bean... please look for this sample : and pay attention to the header "status"

<p:datatable styleClass="table1" id="listTable" var="t" value="{testBean.elements}">
    <p:column headerText="Request No.">
       <h:outputText value="#{t.part_request_no}"></h:outputText>
    </p:column>
    <p:column headerText="Request No.">
       <h:outputText value="#{t.part_request_date}"></h:outputText>
    </p:column>
    <!--please pay attention here -->
    <p:column headerText="status">
        <h:outputText value="#{t.part_status}"></h:outputText>
    </p:column>
</p:datatable>

the result looks like : this

Request No.  | Request Date | Status |
    1        |   2013-02-02 |   1    |
    2        |   2013-01-01 |   2    |
    3        |  2013-4-23   |   5    |

my Question is : representing 1, 2, 5 is a shame. so i prefer to view statuses like

  • 1 : New
  • 2 : Rejected
  • 5 : Canceled.

the result i require to be like this

Request No.  | Request Date | Status   |
    1        |   2013-02-02 |   New    |
    2        |   2013-01-01 | Rejected |
    3        |   2013-4-23  | Canceled |

any ideas ???? thank you ....

Was it helpful?

Solution

There are several ways depending on how and where you'd like to maintain the mapping between status codes and associated descriptions.

  1. Hardcoded in view.

    <h:outputText value="New" rendered="#{t.part_status == 1}" />
    <h:outputText value="Rejected" rendered="#{t.part_status == 2}" />
    <h:outputText value="Cancelled" rendered="#{t.part_status == 5}" />
    

  2. Hardcoded in a map.

    @ManagedBean
    @ApplicationScoped
    public class Data {
    
        private static final Map<Long, String> STATUSES = createStatuses();
    
        private static Map<Long, String> createStatuses() {
            Map<Long, String> statuses = new HashMap<Long, String>();
            statuses.put(1L, "New");
            statuses.put(2L, "Rejected");
            statuses.put(5L, "Cancelled");
            return Collections.unmodifiableMap(statuses);
        }
    
        public Map<Long, String> getStatuses() {
            return STATUSES;
        }
    
    }
    

    with

    <h:outputText value="#{data.statuses[t.part_status]}" />
    

  3. Definied in a loclaized resource bundle file.

    status.1 = New
    status.2 = Rejected
    status.5 = Cancelled
    

    with

    <f:loadBundle basename="com.example.i18n.text" var="text" />
    ...
    <c:set var="statusKey" value="status.#{t.part_status}" />
    <h:outputText value="#{text[statusKey]}" />
    

  4. Definied in an enum.

    public enum Status {
    
        New(1), Rejected(2), Cancelled(5);
    
        private int code;
    
        private Status(int code) {
            this.code = code;
        }
    
        public static Status of(int code) {
            for (Status status : values()) {
                if (status.code == code) {
                    return status;
                }
            }
            throw new IllegalArgumentException();
        }
    
        public int getCode() { 
            return code;
        }
    
    }
    

    and during populating the model

    t.setPart_status(Status.of(statusCode));
    

    and then just in the view

    <h:outputText value="#{t.part_status}" />
    

A combination can also. E.g. i18n label in enum.


Unrelated to the concrete problem, I'd work on your Java naming conventions. part_status is not a valid property name. It should be partStatus.

OTHER TIPS

Why don't you just simply create a method in you ManagedBean to convert it to the label you want?

<p:column headerText="status">
    <h:outputText value="#{testBean.getStatusLabel(t.part_status)}"/>
</p:column>

And on the ManagedBean:

public String getStatusLabel(int code){
    // Do your thing here...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top