0

I have multiple rows in the pageBlockTable and I want the commandButton to work for each row seperately. Right now it only executes the first row, does anyone have any ideas how I can do this?

apex page

<apex:page standardController="Account" extensions="getItems">    
<apex:outputPanel layout="block" style="overflow:auto;width:500;height:180px">
    <apex:form onsubmit="openWindowPopup(); return false;" styleClass="formId">
        <apex:pageblock id="table">

            <script>
            function openWindowPopup(){
                var comments = document.getElementsByClassName("commentsId")[0].value;
                var passwordValue = document.getElementsByClassName("passwordId")[0].value;

                }

                window.open('https://www.local.com?passwd='+passwordValue+'&comment='+comments);
                }
            </script>
            <apex:pageblocktable id="pageBlockId" value="{!AssetLines}" var="lines" >     
            <apex:column >
                <apex:commandbutton value="Generate" reRender="table" />       
            </apex:column>
                <apex:column headerValue="Password">
                    <apex:inputField value="{!AssetItems.Password__c}" styleClass="passwordId"  />
                </apex:column>
                <apex:column headerValue="Comments">
                        <apex:inputField value="{!AssetItems.Comments__c}" styleClass="commentsId"/>
                 </apex:column>                  
            </apex:pageblocktable>
        </apex:pageblock>
    </apex:form>
</apex:outputPanel>

  • please share class code, what exactly you want to achieve by clicking on the button. – Nebula May 28 '15 at 11:35
  • the commandbutton runs the javascript code, which passes the input into a url query string and opens a new window that uses the query string as input for a form in the new window that was opened. – Micheal McGowan May 28 '15 at 11:39
  • did you try my code?@MichealMcGowan – snehakem May 28 '15 at 12:48
  • @MichealMcGowan Let me know if it works? :) – snehakem May 28 '15 at 12:57
  • I tried it and i got an error when i press the commandbutton and got an error "unexpected Identifier". – Micheal McGowan May 28 '15 at 13:25
3

The previous openWindowPopup function was always taking password value of first record because of below line,

var passwordValue = document.getElementsByClassName("passwordId")[0].value;

Hence i updated that function to have a parameter as password field id, Using which you can access the clicked record password value. Also now the function is called onclick on command button instead of form submit where it is easier to pass the password field id.

Try this code,

<apex:page standardController="Account" extensions="getItems">    
<apex:outputPanel layout="block" style="overflow:auto;width:500;height:180px">
    <apex:form  styleClass="formId">
        <apex:pageblock id="table">

            <script>
            function openWindowPopup(pid,cid){
                var comments = document.getElementById(cid).value;
                var passwordValue = document.getElementById(pid).value;
                window.open('https://www.local.com?passwd='+passwordValue+'&comment='+comments);
            }
            </script>


     <apex:pageblocktable id="pageBlockId" value="{!AssetLines}" var="lines" >     
                <apex:column >
                    <apex:commandbutton value="Generate" reRender="table" onclick="openWindowPopup('{!$Component.myPassId}','{!$Component.myCommentId}')"/>       
                </apex:column>
                <apex:column headerValue="Password">
                        <apex:inputField value="{!AssetItems.Password__c}" styleClass="passwordId" id="myPassId"  />
                 </apex:column>   
                 <apex:column headerValue="Comments">
                        <apex:inputField value="{!AssetItems.Comments__c}" styleClass="commentsId" id="myCommentId"/>
                 </apex:column>                                    
                </apex:pageblocktable>
            </apex:pageblock>
        </apex:form>
    </apex:outputPanel>
  • In order to help the asked, maybe you might want to add some info as to what the error was and how you solved it? – Guy Clairbois May 28 '15 at 12:32
  • Thank you for your answer. Your answer may work for one column and one input but in my code there are multiple columns of input that are used in the query string, I left them out to reduce the length of code in my question. Do you have a solution to the edited question? Thanks. – Micheal McGowan May 28 '15 at 12:50
  • Ya i do have. I will update the same. But you are supposed to accept answer if worked in first place for your question :). – snehakem May 28 '15 at 12:52
0

Try this code:

/*****script*****/
function openWindowPopup(elm){

var passwordValue = elm.parentNode.nextSibling.childNodes[0].value;

window.open('https://www.local.com?passwd='+passwordValue);
}

/*****Vf page*****/
<apex:pageblocktable id="pageBlockId" value="{!AssetLines}" var="lines" >     
<apex:column >
    <input type="button" value="Generate" onclick="openWindowPopup(this);" class="btn"/>       
</apex:column>
    <apex:column headerValue="Password">
        <apex:inputField value="{!AssetItems.Password__c}" styleClass="passwordId"  />
    </apex:column>                                     
</apex:pageblocktable>

You have to call a method on click of button and not on submit of form. Remove onsubmit event from form.

Let me know if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.