I have a pageBlockTable that loops through a list of objects. I'm trying add an onRowClick event so I can use the clicked row as data for a javascript function. However when saving in the developer console, I'm getting the error :-

(Unknown property 'ContactStandardController.c')

What am I doing wrong, why is it looking at the controller and not the pageBlockTable var?

<apex:page standardController="Contact" >
    <apex:pageBlock>
        <apex:pageBlockTable value="{!Contact}" var="c" onRowClick="alert('{!c.Name}');">
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
share|improve this question
    
Here the approach is wrong. {!Contact} is a single instance. It should not be used with <dataTable/>. It will result nothing. It should be a list. – Ashwani 8 hours ago
up vote 4 down vote accepted

This is basically a limitation of how the system works; you can't reference the variable until you're already inside the table. If you want to get the information about the row, you can do so through event.target. Here's a little demo I wrote up that alerts a contact's Id when you click anywhere on the row:

<apex:page standardController="Account">
    <script>
    function handle(event) {
        alert(event.target.querySelector('.rowId').dataset.rowId);
    }
    </script>
    <apex:pageBlock>
        <apex:pageBlockTable value="{!Account.Contacts}" var="c" onRowClick="handle(event)">
            <apex:column headerValue="Contact Name">
                <span class="rowId" data-row-id="{!c.Id}" />
                {!c.Name}
            </apex:column>
            <apex:column value="{!c.Email}" />
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

To demo this in your org, remember to add ?id=<some-account-id> to the URL to view this in action. event.target is the row that was clicked.

share|improve this answer
    
All other examples if using onRowClick i could find seem to do exactly what I'm trying for i.e. [link]salesforce.stackexchange.com/questions/134366/… [link]developer.salesforce.com/forums/?id=906F000000094aFIAQ – Cliff B 7 hours ago
    
@CliffB In both those answers, they eventually just attached an onclick handler to each cell in the table; that's not the same as having the merge fields in the onRowClick handler. For what it's worth, I did attempt a version of your own code in my developer org before generating my answer, which I often do just to confirm/reproduce the problem, before independently coming up with the solution I presented. If there were a way to do it, I'm sure that between Adrian, myself, and Ashwani, we would have (probably) found a fix if one were to be had. – sfdcfox 7 hours ago
1  
@CliffB On the other hand, you could use an apex:repeat element, and render your own tr elements with an onclick handler, which would be functionally the same, but it would not be an apex:pageBlockTable; you'd have to do all the styling yourself. – sfdcfox 7 hours ago
    
Fair enough, this will do what i need. Thanks – Cliff B 7 hours ago

The c variable is only in scope between your opening and closing <apex:pageBlockTable> tags. In this case, you should just use alert('{!Contact.Name}').

share|improve this answer
    
This would reference the table not the row I'm clicking – Cliff B 7 hours ago
    
There can only be one row... – Adrian Larson 7 hours ago

Here the approach is wrong. {!Contact} is a single instance. It should not be used with <PageBlockTable/>. It will result nothing. Also, var scope is in only inside the tag not with the tag itself.

Anyways, here is the way to access the column if Contact is a list:

<apex:pageBlockTable value="{!contact}" var="c" onRowClick="alert(this.firstChild.innerHTML);">
            <apex:column >{!c.Name}</apex:column>
            <apex:column >{!c.Id}</apex:column>
</apex:pageBlockTable>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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