Primefaces p:chips, is there any way to start the list of tags with no value?

Asked

Viewed 66 times

0

On my screen, I need the list of tags in the chips component to start empty and when I have values I need them to be set as tags, but when I try to start an empty list of tags, the component starts a blank tag in the field, I need the field to start blank, without any tag...

public String getCanInteresses() {
    return canInteresses;
}

public void setCanInteresses(String canInteresses) {
    this.canInteresses = canInteresses;
}

//REVISAR
public List<String> getCanListInteresses() {
    return Arrays.asList(canInteresses);
}

public void setCanListInteresses(List<String> canInteresses) {
    this.canInteresses = StringUtil.join(canInteresses, ",");
}

XHTML

<p:panelGrid columns="3" layout="grid">
                            <p:panelGrid columns="1" layout="grid">
                                <p:outputLabel value="Interesse" />
                                <p:chips id="tagsInteresse" value="#{curriculoBean.crudObj.canListInteresses}" />
                                <p:commandButton value="Adicionar" icon="#{icon.adicionar}" process="@this :form1:tagsInteresse" update=":form1:tagsInteresse" oncomplete="$(function(){PrimeFaces.focus('form1:tagsInteresse');});" />
                            </p:panelGrid>
                        </p:panelGrid>

The field starts with null and even doing the test if it is null and setting empty, it creates a tag with no value inside

O campo inicia com null e mesmo fazendo o teste se for null e setando vazio, ele cria uma tag sem nenhum valor dentro

I only need the field to start with no tag, only when it already has linked values

1 answer

0


The problem is in the logic of instantiating the list with values inside:

return Arrays.asList(canInteresses); // Se canInteresses for null, a lista será criada com um elemento nulo dentro

I believe it would be better if you had an attribute in your class as below:

private List<String> interesses = new ArrayList<>();

You could load the value through a method to be called whenever you need to check and update the list:

public void carregarInteresses() {
    interesses.clear(); 
    if(canInteresses != null && !canInteresses.isEmpty()) {
        interesses.addAll(Arrays.asList(canInteresses.split(",")));
    }
}

And the moment you persist, or treat String canInteresses you apply the treatment StringUtil.join(interesses, ",") as you’ve been doing.

  • Excellent, thank you very much :)

  • Just one thing, I’d call the upload where? in xhtml?

  • I would call on the bean Managed, on a @PostConstruct, or after loading the data from the screen, or anywhere you want to synchronize the chips interesses with the canInteresses

  • I hadn’t thought about it, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.