function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ssfdcssfdc 

Setting Apex:param after runtime from user input

I need to set a param once a user has changed the value of an inputText field so it will be sent once the user clicks the commandButton, I can't figure this one out and am reultant to scrap my design for JS remoting,

Use: Case

A list of differect products, a user can select a quantity then hit Add, the product Id and quanitity should be passed, the Id is set correctly as this is set at run time but how to set soemthing and bind it to command button once the page is loaded?

    <apex:repeat value="{!prodList}" var="p">
      {!p.Name}

      <apex:inputText value="{!quant}"/>
      <apex:commandButton action="{!addItem}" value="Add">
       <apex:param name="id" assignTo="{!prodId}" value="{!p.Id}"/>
      </apex:commandButton>

    </apex:repeat>

bob_buzzardbob_buzzard
There has been a bug in parameters nested in command buttons for a few years now (in fact its been so long I suspect its now considered a feature), in that if you don't specify a rerender attribute on the command button, the param value is discarded and the controller property is null.

There's a detailed discussion on this at:

http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/

ssfdcssfdc
Thanks Bob for the reply but I'm aware of this bug, My param that gets set at run time works fine but setting a param from a user input is where I get stuck, after runtime, I actually just redesigned this using JS remoting but I really wanted to figure this on out using Apex only.
bob_buzzardbob_buzzard
Ah okay, I think I understand.  The problem here is that every element in your list has an input field bound to the 'quant' controller property, so when you submit the form they all write their value into that property.  As there are no guarantees of ordering in a postback, all you can say is that the last one to write its value will win.

There's a couple of ways around this:

(1) Put an actionregion around each repeated element - this means that only the quant value inside the same action region as the command button will be sent to the controller
(2) Change your code to use wrapper classes, which contain the product sobject and the quantity - that way each repeated element will write its quant value back to its own wrapper class.
ssfdcssfdc
Thanks Bob! I'll defiantly look at these two approaches, what I'd did in the mean time was to give each input an ID, ex I'd="in{!p.Id}" also changed the command button to a standard HTML button and added an onClick passing p.Id, I then used some jQuery to pick up the input field and JS remoting.
bob_buzzardbob_buzzard
I use remoting a lot these days.  Its definitely faster than using regular VF postbacks, but the stateless nature of the requests can make things a little more tricky if you are managing large amounts of data.