1
votes

I am not sure if I am making a silly mistake, but this has not been working for me on a very simple case: In my tabview / tabs when I switch from one tab to other I want to save the data that's entered in the form. The Submit button works fine on each tab, but the tabchange event shows all my form data to be null. I have tried process with @all, @form, @this, nothing seems to work. Same with update option.

Here's the XHTML:

    <h:form id="form">
    <p:growl id="msgs" showDetail="true" keepAlive="5000"  />
    <p:tabView id="tView" dynamic="true" cache="false" >
        <p:ajax event="tabChange" listener="#{deleteMe.onTabChange}" update=":form:msgs" />
        <p:tab title="MyTitle I" id="tab1">
            <p:fieldset legend="Personal Information" toggleable="false" >
                <h:panelGrid columns="2" cellpadding="10"> 
                    Value 1: <p:inputText value="#{deleteMe.thingOne}"></p:inputText>
                    Value 2: <p:inputText value="#{deleteMe.thingTwo}"></p:inputText>
                    <p:commandButton value="Submit" id="ajax" update=":form:msgs" action="#{deleteMe.onSubmit}" />
                </h:panelGrid>
            </p:fieldset>
        </p:tab>
        <p:tab title="MyTitle II" id="tab2">
            <p:fieldset legend="Social Information" toggleable="false">
                <h:panelGrid columns="2" cellpadding="10">
                    <p:inputText value="#{deleteMe.thingThree}"></p:inputText>
                    <p:commandButton value="Submit" id="ajax2" update=":form:msgs" action="#{deleteMe.onSubmit}" />
                </h:panelGrid>
          </p:fieldset>  
        </p:tab>
    </p:tabView>
</h:form>   

And here's my backing bean:

`

import javax.annotation.PostConstruct;
import javax.inject.Named;

import org.primefaces.event.TabChangeEvent;
import org.springframework.context.annotation.Scope;


@Named(value = "deleteMe")
@Scope("view")
public class DeleteBean implements Serializable {
    private static final long serialVersionUID = 47L;

    private String thingOne;
    private String thingTwo;
    private String thingThree;

    @PostConstruct
    public void initialize() {

    }   

    public void onTabChange(TabChangeEvent event) {
        System.out.println("Saving draft, current index: "+ event.getTab().getTitle());
        System.out.println(thingOne);
        System.out.println(thingTwo);
        saveDraft();
    }

    public void onSubmit() {
        System.out.println(thingOne);
        System.out.println(thingTwo);
        System.out.println(thingThree);
        saveDraft();
    }

    public void saveDraft() {
        System.out.println("This is thing 1 value: "+thingOne);
        System.out.println("This is thing 2 value: "+thingTwo);
        System.out.println("This is thing 2 value: "+thingThree);
    }


    public String getThingOne() {
        return thingOne;
    }

    public void setThingOne(String thingOne) {
        this.thingOne = thingOne;
    }

    public String getThingTwo() {
        return thingTwo;
    }

    public void setThingTwo(String thingTwo) {
        this.thingTwo = thingTwo;
    }

    public String getThingThree() {
        return thingThree;
    }

    public void setThingThree(String thingThree) {
        this.thingThree = thingThree;
    }
}

`

On tabchange event, it triggers the method, but the values are all null.

SystemOut     O This is thing 1 value: null
SystemOut     O This is thing 2 value: null
SystemOut     O This is thing 2 value: null

Can you please point out if I am making some silly mistake? Thank you.

3

3 Answers

0
votes

The correct answer is to use skipChildren="false" on your p:ajax commands.

https://github.com/primefaces/primefaces/issues/2525

 <p:ajax event="tabChange" 
         skipChildren="false"
         listener="#{deleteMe.onTabChange}" 
         update=":form:msgs" />
0
votes

I have the same problem. On changing the tab without submitting, data will be lost because of not updating the input text field. You can add ajax for Input text field.

   <p:inputText id="thingOneId" value="#{deleteMe.thingOne}">
      <p:ajax update="thingOneId"/>
   </p:inputText>

skipchildren="true"

works for not loosing the data, but it won't update the value from inputfields.

-2
votes

Saw a workaround for it but I am not sure why it is working this way:

I had to add the remote command with a tabchange listener on tabView.

<p:remoteCommand name="onTabChangeProcess" process="tView, @this"/> 
    <p:tabView activeIndex="#{deleteMe.activeIndex}" id="tView" onTabChange="onTabChangeProcess()">
            <p:ajax event="tabChange" listener="#{deleteMe.onTabChange}" update="tView" process="tView"/>

This made the form to work as expected.