+ Start a Discussion
BrianWKBrianWK 

Best Practice for re-rendering and calculating field values. Best or Preferred method

Okay, so here's my scenario.

 

I have a custom object called "Contract Product." I've estentially built a "shopping cart" page where users can select a Product via a drop down, enter some Dollar and dates and "Add to Cart." This way they can add multiple products into the cart and they all get "saved" to the server in bulk (vs one at a time). 

 

Part of the aspect of this cart is a number of fields. As specific fields get modified the page updates other fields with new values based on some calculations. Right now all my calculations are in a separate Class called by my custom controller. This is nice in case I have to update the math I can modify the methods. Plus I can use this class for other pages doing the same calculation.

 

Here's the simple example. We have three fields:

  • Unit Quantity
  • Unit Price
  • Total Amount Paid

When Unit Quantity is changed, the page re-renders the Total Amount Paid with the new value (Unit Quantity * Unit Price).

When Unit Price is changed, the page re-renders the Total Amount Paid with the new value (Unit Quantity * Unit Price).

When Total Amount Paid is changed, the page re-renders the Unit Price (Total Amount Paid / Unit Price)

 

 

So far what I've done is have each field in an Outputpanel. Each Inputfield also has an ActionSupport component that "onblur" calls a Custom Controll method (Calculate) and re-renders the other fields. I also have a Status on the Action Support so a "waiting" message displays when the Support method is working.

 

This system "works." I've noticed a few challenges and frustrations:

 

  1.  "On Blur" requires the user to leave the field. Sometimes the actionsupport is called sometimes it isn't
  2. This is slow: Each time the Action Support is used it causes a "slow down" in the end-user process. Sometimes the response is really fast -- sometimes it takes 5-10 seconds for the rerender to occur.
  3. Not every re-render gets re-rendered consistently. There are more fields than just the 3 in this sample being used. Sometimes what will happen is that two fields will get updated and re-rendered and 2 other fields won't -- until the next change happens. I know the value is being updated in the controller - what's shown on the page to the end user appears to be either the "wrong" value (it's from a prior edit) or blank.

 

So what's the best way to handle this type of scenario? I've seen people use javascript, jquery, mixture of either with apex. Is there a best practice? What would provide the fastest user experience and the most accuracy?