Primefaces datatable formatting numbers

1

How can I format a column (zip) of the datatable with the mask " 00000-000 "?

Example:

Value: 90560120 - Output:     

asked by anonymous 21.04.2015 / 17:55

4 answers

1

If you want a input then @Anderson's answer is more appropriate. If you only want a OutputText or OutputLabel I recommend creating your own convert, it would have a better semantics, a clearer understanding of what is being done and is easily reused on other screens.

Your facelets would look like:

<h:outputText value="#{seuBean.cep}">
    <f:converter converterId="CEPConverter" />
</h:outputText>

And the convert would look like:

@FacesConverter("CEPConverter")
public class CEPConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String[] parts = value.split("-\.");
        return Integer.parseInt(join(parts));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        String valueAsString = value.toString();
        return formatCEP(valueAsString.substring(0, 2), valueAsString.substring(2, 5), valueAsString.substring(5));
    }

    private String formatCEP(String part1, String part2, String part3) {
        return part1.concat(".").concat(part2).concat("-").concat(part3);
    }

    private String join(String[] parts) {
        StringBuilder sb = new StringBuilder();

        for(String part : parts) {
            sb.append(part);
        }

        return sb.toString();
    }
}

This is a very simple implementation, you can even improve / customize it according to your understanding.

    
21.04.2015 / 20:39
1

I'm still looking for a more simple form, but I ended up solving it this way:

dataTable :

<h:outputText value="#{bean.format('##.###-###', item.cep, true)}" style="float:left"/>

Bean :

public String format(String pattern, Object value, boolean suppressZero) {
    if (!suppressZero || Double.parseDouble(value.toString()) != 0) {
        MaskFormatter mask;
        try {
            mask = new MaskFormatter(pattern);
            mask.setValueContainsLiteralCharacters(false);
            return mask.valueToString(value);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    } else {
        return "";
    }
}
    
21.04.2015 / 20:13
1

I believe that creating a converter that saves only the numbers is the simplest way to resolve this issue.

package br.com.t2tecnologia;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("app.numbersConverter")
public class AppNumbersConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null) {
            return null;
        }

        return value.replaceAll("[^0-9+]", "");
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object object) {
        return object.toString();
    }

}

No .xhtml:

<p:inputMask mask="99.999-999" id="enderecoCepText" value="#{e.cep}" size="10"
             converter="app.numbersConverter"
             required="true" requiredMessage="CEP não informado."/>

Use this to convert to various other data, such as CPF, CNPJ, etc., to store only the numbers in the database column.

    
26.07.2016 / 22:41
0

The most appropriate to use in your case would be the inputMask component of Primefaces. In your case the tag would look like this: <p:inputMask mask="99.999-9999"/>

You can find more by talking about the tag here .

    
21.04.2015 / 19:53