Switch from integer to String in data table in primefaces

0

In the database the periods that is the same as the month is saved in numbers but I want to convert them to String so that the end user in the table can see the names of the months, the same case happens with the column made that only has 2 options 1 or 0 but I want to change with pending or done

Investigating I came across the FacesConverter entry and create a class as follows.

@FacesConverter(value = "periodoConverter")
public class PeriodoConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        return string;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object value) {
        int periodo = 1;
        String periodo1 = "";
        if (value != null) {
            switch (periodo) {
                case 1:
                    periodo1 = "Enero";
                    break;
                case 2:
                    periodo1 = "Febrero";
                    break;
                case 3:
                    periodo1 = "Marzo";
                    break;
                case 4:
                    periodo1 = "Abril";
                    break;
                case 5:
                    periodo1 = "Mayo";
                    break;
                case 6:
                    periodo1 = "Junio";
                    break;
                case 7:
                    periodo1 = "Julio";
                    break;
                case 8:
                    periodo1 = "Agosto";
                    break;
                case 9:
                    periodo1 = "Septiembre";
                    break;
                case 10:
                    periodo1 = "Octubre";
                    break;
                case 11:
                    periodo1 = "Noviembre";
                    break;
                case 12:
                    periodo1 = "Diciembre";
                    break;
            }
        }
        return periodo1;
    }

}

and on the front:

<p:column headerText="Periodo" >
   <h:outputLabel value="#{reg.periodoDatos}" >
      <f:converter converterId="periodoConverter" />
   </h:outputLabel>
</p:column>

but I do not know why it does not work and it appears blank, any help I would thank you from my heart a thousand thanks.

    
asked by Ricardo Betancourh Bolivar 27.08.2018 в 08:08
source

1 answer

0

I think you could use this form, maybe it is not the most optimal but it could be useful for you:

<p:column headerText="Periodo" >
    <h:outputText value="#{reg.periodoDatos eq 1 ? 'ENERO' : ''}" rendered="#{reg.periodoDatos eq 1}" />
    <h:outputText value="#{reg.periodoDatos eq 2 ? 'FEBRERO' : ''}" rendered="#{reg.periodoDatos eq 2}" />
    <h:outputText value="#{reg.periodoDatos eq 3 ? 'MARZO' : ''}" rendered="#{reg.periodoDatos eq 3}" />
    <h:outputText value="#{reg.periodoDatos eq 4 ? 'ABRIL' : ''}" rendered="#{reg.periodoDatos eq 4}" />
  ............
</p:column>

In the same way you would do with the other column:

<h:outputText value="#{reg.estado eq 0 ? 'PENDIENTE' : ''}" rendered="#{reg.periodoDatos eq 0}" />
<h:outputText value="#{reg.periodoDatos eq 1 ? 'REALIZADO' : ''}" rendered="#{reg.periodoDatos eq 1}" />

The use of the converter is for when you send the data in a form, if it is necessary to carry out a conversion between what is shown on the screen and what is stored in the database (according to my experience).

    
answered by 31.08.2018 в 17:49