3

When i click on any row using onrowclick function in apex:pageBlockTable. I am able to select the row using javascript functionality but i am not able to get the selected row data to row data in alert box.

Can any one help me How to get selected row data in controller?

Please find the vfpage and controller

<apex:page controller="petwizardcontroller" sidebar="false">
<apex:form title="">
    Household Name: <apex:inputText value="{!ID_c}" />
    <apex:commandButton value="Search" action="{!doSearch}"/>

<apex:pageBlock >
    <apex:pageblockTable value="{!lstQuery}" var="eachRecord">        
        <apex:column value="{!eachRecord.Household__c}"/>
        <apex:column value="{!eachRecord['Name']}"/>
        <apex:column value="{!eachRecord.Phone__c}"/>
        <apex:column value="{!eachRecord.of_Childrens__c}"/>
        <apex:column value="{!eachRecord.of_Exiting_Pets__c}"/>
        <apex:column value="{!eachRecord.Address__c}"/>
        <apex:selectList value="{!eachRecord.Phone__c}">
        <apex:selectOptions value="{!eachRecord.of_Childrens__c}">
            </apex:selectOptions>
        </apex:selectList>
    </apex:pageblockTable>
    <apex:commandButton value="None of These" reRender="PCSHouseTable"/>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller

public class petwizardcontroller {
public string ID_c{get;set;}
public List<PCS_Household__c> lstQuery{get;set;}

public petwizardcontroller(){
    lstQuery=new List<PCS_Household__c>();
}

public Void doSearch(){

    string Query='';
    Query='SELECT Name,Household__c,Phone__c,of_Childrens__c,of_Exiting_Pets__c,Address__c FROM PCS_Household__c where Household__c LIKE \'%'+ID_c+'%\'';
    system.debug('==Query=='+Query);

    lstQuery=Database.query(Query);
    system.debug('==lstQuery=='+lstQuery);

}


}
3
  • 2
    You missed your code?
    – highfive
    Commented Apr 28, 2016 at 6:58
  • updated my code.
    – user31063
    Commented Apr 28, 2016 at 7:00
  • but how to get the row selected value in text fields thank you.
    – user31063
    Commented Apr 28, 2016 at 10:34

1 Answer 1

1

By passing this to the JavaScript, the JavaScript has a reference to the DOM element that was clicked on:

<apex:page standardController="Contact" recordSetVar="cs">
<apex:pageBlock>
<apex:pageBlockTable onRowClick="handler(this);" value="{!cs}" var="c">
    <apex:column value="{!c.Name}"/>
    <apex:column value="{!c.Birthdate}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<script>
function handler(tr) {
    alert(tr.tagName);
}
</script>
</apex:page>

and can then use raw DOM processing or jQuery to generate the required output or other logic.

The above answer is to the original question which did not include the part about passing the data to the controller. A way to do that is to call the JavaScript generated by an apex:actionFunction from the handler which in turn calls a controller method passing say the ID of the clicked row.

You must log in to answer this question.