4

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>
1
  • 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
    Jan 19, 2017 at 4:22

3 Answers 3

4

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.

4
  • 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
    Jan 19, 2017 at 5:10
  • @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
    Jan 19, 2017 at 5:24
  • 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
    Jan 19, 2017 at 5:28
  • Fair enough, this will do what i need. Thanks
    – Cliff B
    Jan 19, 2017 at 6:01
2

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}').

2
  • This would reference the table not the row I'm clicking
    – Cliff B
    Jan 19, 2017 at 5:09
  • There can only be one row...
    – Adrian Larson
    Jan 19, 2017 at 5:09
0

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>

You must log in to answer this question.

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