1

I want a value from one input field to be copied into all pageBlock fields. I could insert records with the value, but then user wouldn't be able to change individual values in the pageBlock.

Current code is:

      <apex:form >  
<apex:inputtext value="{!newQuoteValue}" required="true">
           <apex:actionSupport event="onchange" rerender="newQuotesBlock"/>
        </apex:inputText>     
        <apex:pageblock id="newQuotesBlock" >
          <apex:pageblockTable value="{!wrapperObj}" var="Rec">
              <apex:column headerValue="£/year"  >
              <apex:inputtext value="{!Rec.Standing}" required="true"/>
             </apex:column>

Apex:

Public List<WrapperClassEx> WrapperList {get;set;}
public long newQuoteValue {get;set;}
Public Class WrapperClassEx{
     Public WrapperClassEx(sobject accRec, string meterNameMethodVar, string meterAQ, string meterIDs, long massStanding ){
        elecObj = accRec;
        meterName = meterNameMethodVar;
        AQ=meterAQ;
        meterID=meterIDs;
        standing=massStanding;
     }
   }
Public List<WrapperClassEx> getwrapperObj(){
List<electricity_meter__c> MeterList = [Select id,name,AQ__c,meter_details__c from electricity_meter__c where (Linked_Monitor_Line__c =: lineId)]; 
      WrapperList = New List<WrapperClassEx>();
      for(electricity_meter__c met: MeterList){
        String meterNameInner = String.valueOf( met.get('Name') );
        String meterAQ = String.valueOf( met.get('AQ__c') );
        String meterID = String.valueOf( met.get('meter_details__c') );  
        long massStanding = newQuoteValue;
        WrapperList.add(New WrapperClassEx(met,meterNameInner,meterAQ,meterID,massStanding )); 
      } 
      return WrapperList;
   }

I am guessing that getwrapperObj is not being updated from onChange event?

2 Answers 2

3

Perhaps this is easiest done using Javascript (specifically using the jQuery library) instead of a Controller Action Method.

To do this, let's first create a Javascript function called copyInput. This function takes in the HTML-element calling the method as its input (ex. copyInput(this);). Then it copies the input's value to the input of all inputs with the class inputToCopyTo. This is done via

<apex:includeScript value="https://http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"/>
<!-- Note:  You should probably download and save the jQuery version you use as
            Static Resource and then use {!URLFOR($Resource.jQueryResourceName)}
            instead of the url   --> 
<script>
    var j$=jQuery.noConflict();

    function copyInput(thar){
        var inputValue=j$(thar).val();
        // set the inputValue to all inputs with the class "inputToCopyTo"
        j$('.inputToCopyTo').val(inputValue);
    }
</script>

Then, let's trigger the function on the onkeyup event on the master-input. (Try using the onchange event too to see the difference. I generally prefer the feel onkeyup, but that's just me.)

Finally, let's add the class inputToCopyTo to the pabeBlockTable's inputs so those inputs get whoever is using the copyInput function we made.

Putting this all together:

<apex:form >  
    <apex:inputtext value="{!newQuoteValue}" required="true" onkeyup="copyInput(this);"> <!-- Changed by OP as this was bugging -->
    </apex:inputText>     
    <apex:pageBlock id="newQuotesBlock" >
        <apex:pageBlockTable value="{!wrapperObj}" var="Rec">
            <apex:column headerValue="£/year"  >
                <apex:inputtext value="{!Rec.Standing}" required="true" styleClass="inputToCopyTo"/>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

<apex:includeScript value="https://http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"/>
<!-- Note:  You should probably download and save the jQuery version you use as
            Static Resource and then use {!URLFOR($Resource.jQueryResourceName)}
            instead of the url   -->
<script>
    var j$=jQuery.noConflict();

    function copyInput(thar){
        var inputValue=j$(thar).val();
        // set the inputValue to all inputs with the class "inputToCopyTo"
        j$('.inputToCopyTo').val(inputValue);
    }
</script>
2
  • Thanks, it worked. However I changed from onkeydown to onkeyup, as it was only changing after second key press. Any ideas for picklists?
    – dzh
    Feb 28, 2014 at 11:58
  • 1
    Picklists worked using the same way as inputfields.
    – dzh
    Feb 28, 2014 at 15:05
1

Perhaps, you have missed providing an action to action:support it should be something like:

<apex:actionSupport event="onchange" action="{!someControllerMethod}" rerender="newQuotesBlock"/>

Reference: here

2
  • Good point, although using getwrapperObj() method wouldn't update them. I assume I need to write a new method that updates wrapperObj ?
    – dzh
    Feb 27, 2014 at 17:55
  • @dzh yes, a new method !
    – Ashwani
    Feb 27, 2014 at 18:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .