3

In the following example, I modify a datatable using p:ajax then use remoteCommand to update the table. That works. However, I would also like to update growl msgs in the event of an error or success. That doesn't work.

I see from the examples if a button calls an action, then calls remoteCommand, the growl msgs update. However, that won't give me what I need from the ajax cellEdit in a datatable.

How can I update the growl messages from a p:ajax command.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
>

<h:head/>

<h:form id="formId">

    <p:growl id="msgs" showDetail="true" />
    <h:body>
        <h:panelGrid>
            <!-- Updates wordsId table but not msgs -->
            <p:remoteCommand name="onCellEditRemote" update="wordsId msgs"/>

            <p:dataTable id="wordsId" var="word" value="#{remoteMessageBean.words}" editable="true" editMode="cell">
                <!-- does not update msgs -->
                <p:ajax event="cellEdit" listener="#{remoteMessageBean.modifyWordOnCellEdit}"  oncomplete="onCellEditRemote()" update="formId:msgs"/>

                <p:column headerText="Modify" >
                    <p:outputLabel value="#{word}" />
                </p:column>
                <p:column headerText="Modify" >
                    <p:cellEditor>
                        <f:facet name="output"><h:outputText value=""/></f:facet>
                        <f:facet name="input">
                            <p:inputText id="modelInput" value="#{remoteMessageBean.modifyWord}"/>
                        </f:facet>
                    </p:cellEditor>
                </p:column>
            </p:dataTable>
        </h:panelGrid>

        <p:remoteCommand name="rc" update="msgs" />
        <p:commandButton type="button" action="#{remoteMessageBean.buttonAction}" value="Doesnt Work" icon="ui-icon-refresh" update="msgs" />

        <p:remoteCommand name="rc2" update="msgs" action="#{remoteMessageBean.buttonAction}" />
        <p:commandButton type="button" onclick="rc2()" value="Works" icon="ui-icon-refresh" />


    </h:body>
</h:form>
</html>



@ManagedBean
@SessionScoped
public class RemoteMessageBean {
    private static Logger logger = Logger.getLogger(RemoteMessageBean.class);
    private String modifyWord;
    private List<String> words;

    public RemoteMessageBean() {
        words = new ArrayList<>();
        words.add("word1");
        words.add("word2");
        words.add("word3");
    }

    public void modifyWordOnCellEdit(CellEditEvent event) {
        logger.debug(event);
        getWords().add(getModifyWord());
        logger.debug("");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "adding " + getModifyWord(), null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
        setModifyWord(null);
    }

    public void buttonAction() {
        logger.debug("");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "buttonAction", null);
        FacesContext.getCurrentInstance().addMessage(null, msg);
      }

    public String getModifyWord() {
        return modifyWord;
    }

    public void setModifyWord(String modifyWord) {
        this.modifyWord = modifyWord;
    }

    public List<String> getWords() {
        return words;
    }

}
user6627139
  • 406
  • 6
  • 16
  • Your `onCellEditRemote` remote command does not call anything, so most likely therefor won't update anything. Check network traffic – Kukeltje Jul 23 '16 at 07:47

2 Answers2

1

I worked around the problem by creating and saving message in modifyWordOnCellEdit then calling displayMessageAction from remoteCommand to display it. Not exactly what I was hoping for but it works.

Jsf Page

 <p:remoteCommand name="onCellEditRemote" update="wordsId" action="#{gameBean.displayMessageAction}"/>

Controller

   public void displayMessageAction() {
        if (getMessage() != null) {
            FacesContext.getCurrentInstance().addMessage("growl", getMessage());
            setMessage(null);
        }
    }

    public void modifyWordOnCellEdit(CellEditEvent event) {
    ...
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, exception.getMessage(), null);
        setMessage(msg);
    }
Mehdi Bouzidi
  • 1,937
  • 3
  • 15
  • 31
user6627139
  • 406
  • 6
  • 16
-2

Its will work add partialsubmit in p:ajax

Guru Bala
  • 39
  • 1
  • 7