0

I have a view (.xhtml) and that view contains a dataTable. Inside that dataTable there is one column named "Type" that correspond to the attribute "CTYPE". CTYPE stores three types of values: 'AB', 'BC' and 'CD'. I then have an Enum that translates those values into more adequate strings. Ex: 'AB' -> 'My String test 1', etc..., where 'AB' is the key and 'My String test 1' is the correspondent description (value). The column of the dataTable mentioned above ("Type") shows the description of every key, i.e., instead of 'AB' it shows 'My String test 1', and so on. That column is a sorteableColumn and has a sortKey='CTYPE' that sorts the column by Type. The problem is I want to sort the column by description and not by key. So, I want to sort by 'My String test 1', 'My String test 2'... instead of 'AB', 'BC'... In order to achieve that, my sortKey would have to have access to the Enum and translate the key into the correspondent description. However, I'm not managing to do it.

My .xhtml:

 <p:column>
   <f:facet name="header">
     <ccomponent:sortableColumn model="#{typeSearchM}"
                                controller="#{typeSearchC}"
                                label="#{msg['labels.types.type_method']}"
                                sortKey="CTYPE" />
   </f:facet>
   <h:outputText value="#{msg[typeDTO.getTypesMethod()]}" />
 </p:column>

My TypeDTO.java:

public String getTypesMethod() {
    return DestinationTypeDomain.getDescByKey(typeData.getCType());
}

where DestinationTypeDomain is the enum that translates the key to the description. What I'd like to that is change my sortKey to something like:

sortKey="#{DestinationExpeditionDomain.getDescByKey(typeDTO.destinationDesc)}"
Rita
  • 1,233
  • 2
  • 14
  • 23

1 Answers1

0

I usually let an @ApplicationScoped managed bean do that kind of duties. Forget about hardcoding the description directly in the enumeration, it will make things more complicated if one day you want to add multilanguage support. Just use the original enum:

@ManagedBean
@ApplicationScoped
public class UtilsBean{

    public String typeDescription(Type type){
        switch (type){
            case AB: return "My String test 1";
            //Add every single case
        }
    }

}

Then you only have to use it where you want, it'll be available for every view from your application:

<p:column sortBy="#{utilsBean.typeDescription(type)}">
    #{utilsBean.typeDescription(type)}
</p:column>
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • I did as above. Your answer made sense. When the page loads everything is normal (as before). However, when I try to sort the column the table becomes empty and this error shows up on the console: "" Any ideas why? – Rita Sep 08 '14 at 10:09
  • I think this might occur because of the parameter. – Rita Sep 08 '14 at 10:18
  • You need EL 2.2 to be available for using parameters in EL expressions. Have a look at [this](http://stackoverflow.com/a/3284328/1199132). – Aritz Sep 08 '14 at 10:45
  • Thanks but this is no good for me. This sorting is done client-side and I want to sort server-side. – Rita Sep 10 '14 at 14:47
  • @Rita could you tell me where have you read that's sorted client side? According to the docs: `Defining sortBy attribute enables ajax based sorting on that particular column`: http://www.primefaces.org/showcase/ui/data/datatable/sort.xhtml That's server side. – Aritz Sep 10 '14 at 14:52
  • Maybe I've read sort docs instead but sortBy is sorting by paginator and not the whole datatable, as wished. – Rita Sep 10 '14 at 14:56
  • @Rita, it sorts the whole datatable, unless you're using lazy loading or similars, which is not told in your question. As said in documentation, an ajax call is made, the entire model gets sorted at server side and the table gets rendered again. – Aritz Sep 10 '14 at 15:04
  • But that's not it. The whole list should be ordered by the description (in the DB) but the ajax response should only retrieve the first N-itens otherwise the server gets too overloaded. But since the description is not an attribute in the DB I don't think this is possible. – Rita Sep 10 '14 at 15:54