• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One ajax listener on multiple components in panel (primefaces)

 
Rancher
Posts: 383
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I have a primefaces form containing several panels. More specifically (if it helps), in the form there are several panelGrid panels, with several panelGroup sub-panels in each panelGrid. Inside those panelGroup sub-panels are different fields (i.e. inputText, selectOneMenu, etc...). A user can make a change to any one of those fields in the sub-panel, but not necessarily the first field in the sub-panel. So, I need to "listen" for changes on any one of those fields, to determine if that particular sub-panel has activity going on.

Currently, I have a listener only on the first field (presuming that users will enter a value starting with that field, in most instances). But I'd like to be able to have it so a change event is fired should the user change any of the other fields first. What I am trying to say in a round-about way is that I have no control over which field the user changes first, but there is validation that happens when changes are made. The current method of having a listener on the first field appears to work, but as described, there is no guarantee that that will happen every time.

My apologies for not including code...here is the basic structure of the form:



My question: Is it risky (or does it even make sense) to have an ajax tag calling the same listener bean, inserted on every field within the sub-panel? I am going to do more searching on this, but thought I would also throw this out there, to get a perspective from others.

I should also note I am fairly new to how the ajax event process works (an on-going learning experience currently). I am consulting some online resources on ajax, but I know this website is an excellent (and trusted) resource.

I am using Eclipse and Primefaces 5.0, with JDK 1.7, running on a JBoss EAP 6.2 server.

Any information would be greatly appreciated.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't call a bean. You call a method. The method is a member of a bean. In this case, it's the onControlPerson1Change() method of foreignParty bean.

You can very definitely share a bean between multiple AJAX callers and you can also share a single method between multiple callers. The Ajax Event that's passed as a parameter to the listener will indicate which control fired the method.

What you are planning sounds a lot like something I often do. In which case, my bean would have a "dirty data" flag and the listener method would set that flag true. So I wouldn't really care which control fired the event, since if even a single value changed, it would mean that the form was "dirty".

A non-AJAX variation of this approach would be to attach a valueChangeListener to each field of interest. Since this is a non-AJAX event, the listener would fire when the command (commandButton or commandLink) for the form was triggered, not when the field changed. Since valueChangeListeners only fire if the value is different when the command triggers, any cases where the user changed something and then changed it back before submitting would not invoke their respective listeners.

I've also been known to go to the other extreme and use Javascript to highlight changed controls (for example, alter the background color of a changed textbox).
 
Randy Maddocks
Rancher
Posts: 383
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tim,

Thanks for the clarification on "bean" and "method.  Duly noted.  :thumbup:

I like the "dirty flag" approach. I will likely try that. Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic