0

I am learning JSF and have an understanding issue the way valueChangeListener works. I am trying to use the it inside the dataTable. The objective of using valueChangeListener here is- I want to track the changes user performs in the Title and First Name column.

Please find below code :

<p:dataTable var="tempVar"
                        value="#{tempView.tempVO}">
                        <p:column>
                            <h:outputLabel value="Academic Title:" />
                            <p:inputText value="#{tempVar.title}"
                                style="margin-left:10px;margin-top:20px;width:140px;height:25px"
                                valueChangeListener="#{tempView.titleChangeListener}">
                                <f:attribute name="TITLE" value="TITLE" />
                            </p:inputText>

                            <br />


                            <h:outputLabel value="First Name:" />
                            <p:inputText value="#{tempVar.firstName}"
                                style="margin-left:35px;margin-top:20px;width:140px;height:25px"
                                valueChangeListener="#{tempView.firstNameChangeListener}">
                                <f:attribute name="FIRST_NAME"
                                    value="FIRST_NAME" />
                            </p:inputText>
                 <p:column>
    </p:dataTable>

Bean Code

@PostConstruct
    public void init() {

        try {
            tempVO = tempService
                    .fetchDataFromDatabase(tmpDataBean
                            .tempId());

            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void titleChangeListener(ValueChangeEvent event) {

        String title = (String) ((UIInput) event.getSource()).getAttributes()
                .get("TITLE");

        System.out.println(title);

    }

    public void firstNameChangeListener(ValueChangeEvent event) {

        String firstName = (String) ((UIInput) event.getSource())
                .getAttributes().get("FIRST_NAME");
        System.out.println(firstName);
    }

The issues I am facing is- valueChangeListener is called for all the database columns. For instance if I am changing only Title, it is calling first name valueChangeListener also. The answer provided by @BaluC on this links says that it should be called only when value changed. [When to use valueChangeListener or f:ajax listener?

[1]: When to use valueChangeListener or f:ajax listener? Could someone please help to understand the way valueChangeListener works, Am I using it in the wrong way? Thank you.

Community
  • 1
  • 1
Damon
  • 64
  • 1
  • 1
  • 10

2 Answers2

0

The valueChangeListener will be invoked when value has changed and the form submits.

Do you want to edit a specified record respectively multiple lines in your datatable? Why do it by yourself instead of using the Primefaces edit mode?

http://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml

Hope that helps!

  • Thank you for the answer. As I have asked in the question, It is not working in the expected way. It is calling the all other valueChangeListener too even there column value has not been changed. I can not use the edit dataTable functionality since I also need the old value for audit purpose. – Damon Jul 18 '16 at 14:17
  • So the listener method got called for every record in the datatable, independet of the specified value got changed or not? You can receive both values by CellEditEvent, the old and the new one. I am pretty sure you could do the same by RowEditEvent. – chaeschuechli Jul 18 '16 at 14:22
  • So the listener method got called for every record in the datatable, independet of the specified value got changed or not?- Yes ,it got called for every record. I have to look in detail about the CellEditEvent an RowEditEvent. But just for the knowledge perspective, I would like to understand the root cause of the issue I am facing in implementing valueChangeListener. here, every record means in my case column "title" and "First Name" so I am changing value of title and submitting the form. It is calling both- titleChangeListener as well as firstNameChangeListener. – Damon Jul 18 '16 at 14:29
0

Finally able to solve the issue. valueChangeListener is working the way it is expected, the issue was with the data in the list. if data is null for some of the columns and if you submit the form, null got converted into empty string internally and valueChangeListener consider it as a change,hence method got called.

Damon
  • 64
  • 1
  • 1
  • 10