13

I'd like to be able to assign each row in a table an Id so I can easily find the row(s) later using JavaScript. I see where I can set the id of the table but I'd like to also assign an id to each row with a unique value that will make the row I want easy to find.

5 Answers 5

6
  <apex:page standardController="Account">
     <apex:variable value="{!1}" var="rowNum"/>

 <table>
    <apex:repeat value="{!Account.contacts}" var="item">
      <tr id="hello{!rowNum}"><td>{!item.Name}</td></tr>
     <apex:variable var="rowNum" value="{!rowNum + 1}"/>
   </apex:repeat>
  </table>
</apex:page>

Another solution using apex variable

enter image description here

1
  • I know I could do it this way. I was hoping I didn't have to rewrite the page that someone else had written. Sadly, it looks like a rewrite is in order. Commented Oct 18, 2013 at 20:44
15

If you want to use HTML5 you can take advantage of the custom attributes prefixed with 'data-'. I use this in tables where I want to grab rows and know the SF Id for javascript remoting.

See the sample below from a table I use and then perform actions on the rows using JS remoting

<apex:outputPanel id="topAccounts">         
    <div  id="topAccounts" class="myTable">                 
        <table >
            <tr>
                <td>Name</td>
                <td>Reason</td>
                <td>Executive Sponsor</td>
            </tr>
            <apex:outputPanel rendered="{!noAccounts}">
            <tr>
                <td>No Accounts Listed</td>
                <td>N/A</td>
                <td>N/A</td>
            </tr>
            </apex:outputPanel>
            <apex:repeat value="{!topAccounts}" var="a">
                <tr data-SFid="{!a.Id}">
                    <td><div class="removeButton account" style="float:left;position:absolute;"><apex:image style="width:16px; height:16px;" url="{!URLFOR($Resource.VF_Images, 'delete-32.png')}"/></div>{!a.Name}</td>
                    <td><div class="output account">{!a.Territory_Plan_Top_Account_Reason__c}</div><div class="inlineEdit"><apex:inputField value="{!a.Territory_Plan_Top_Account_Reason__c}"/></div></td>
                    <td><div class="output account">{!a.Territory_Plan_Top_Account_Executive__c}</div><div class="inlineEdit"><apex:inputField value="{!a.Territory_Plan_Top_Account_Executive__c}"/></div></td>
                </tr>
            </apex:repeat>
        </table>                    
    </div>
</apex:outputPanel>

The important snippet being the inside the repeat tag

<apex:repeat value="{!topAccounts}" var="a">
    <tr data-SFid="{!a.Id}">

This is assigning the SF id to a custom attribute of the . This will allow us to get the element using javascript later on, and to know the SF id of the object within the

EDIT

Effectively each row in the table has a unique identifier, and as a bonus it carries the SF Id of the object being presented in that row.

I have some jQuery on a button that resides in each row, that goes and gets the Salesforce ID for that row and passes it into a function that has my JS remoting call in it

j$('.removeButton.account').click(function(){
     console.log(j$(this).closest('tr').attr('data-SFid'));
     removeAccount(j$(this).closest('tr').attr('data-SFid'));
});

If you just want to get the <tr> element in JavaScript, then do:

var tr = $("tr").find("[data-SFid='" + id + "']");
7
  • Answer would be awesome if you showed the snippet of jQuery to retrieve a row based on its ID...
    – metadaddy
    Commented Oct 18, 2013 at 16:08
  • My case is a bit different. I don't grab a row based on the ID, I actually have a button each row 'RemoveButton' and I have jQuery that retrieves the data-SFid of the closest <tr> Commented Oct 18, 2013 at 16:11
  • Love the use of the new HTML5 custom attributes. I have to remember that I'm not forced to hack-around the Visualforce id mangling to use jQuery.
    – greenstork
    Commented Oct 18, 2013 at 16:13
  • No worries, @Dunc44 - I just added the magic line of code :-)
    – metadaddy
    Commented Oct 18, 2013 at 16:14
  • @meta, looks like we were updating it at the same time Commented Oct 18, 2013 at 16:16
3

When you assign an id to a row in a repeat tag, the id value is a concatenated string of the thePageId:theRepeatId:theRowId. There's quite a good blog post about how this works, with references to the Salesforce documentation:

http://watro.tumblr.com/post/9615587212/getting-ids-with-jquery-on-apex-tags

4
  • Sorry greenstork. I know how to GET the id. What I need to do is SET the ID of the <tr> tags under the <table> tag created when you use the pageBlockTable apex tag in a VisualForce page. Commented Oct 18, 2013 at 15:11
  • The example I posted uses jQuery to select the id. Are you asking how to use jQuery to get by an id and then set it to something?
    – greenstork
    Commented Oct 18, 2013 at 15:22
  • If you're repeating over a collection of objects, what would you want the id to be? What defines its uniqueness?
    – greenstork
    Commented Oct 18, 2013 at 15:24
  • What the id is, isn't all that important. What I want to do is set the id of the <tr> element but I do not see a way to do that. I know I could change and use the <apex:repeat> tag and do my own <tr> tags but I was hoping to not rewrite a whole page that has been in service for a few years. Commented Oct 18, 2013 at 15:42
1

As @GreenStork mentioned there is a deterministic way to identify the id of row in a page block table, but you're not able to directly set the ids of the rows.

If you're having trouble finding a particular row based on a different attribute then the row number you might consider inserting a bonus element into one of the page block table columns with an id you define that can then be traversed up to the parent <tr>.

Another option would be to take a look at the rowclasses attribute of the pageblock table which allows you to supply a comma-separated list of CSS classes that get applied in a repeating order and which can be used to find a particular row.

Beyond that we'd need to know more about your use case and why @GreenStork's approach isn't easy enough for you to provide better suggestions.

6
  • The problem with using rowClass is that it must be a static value. It cannot contain variables so that I can set the id or some other attribute to a value based on data in the row. What I'd like to do is something like in a table of users, have the <tr id="{!usr.Id}"> kind of thing. Commented Oct 18, 2013 at 16:08
  • In that case you'll want to either build the table manually using apex:repeat as @mohith is suggesting, alternatively as I suggested just output a dummy span or something with the user id as the id, search for that and traverse up, unfortunately if you're looking to directly set the ids of the <tr>s with a page block table you've hit a wall Commented Oct 18, 2013 at 19:15
  • I've been trying to hack things with javascript but it looks like a full page rewrite with the apex:repeat tag will be required. I was hoping I didn't have to but I guess there's no choice. At least it all pays the same. :( Commented Oct 18, 2013 at 20:43
  • Bummer, I've been able to get by in the past with something like this. One of the columns has the record id then just traverse up to the tr, i.e. j$("[id='hiddenspan#" + userId + "']").parents("tr"). Best of luck (do upvote if this answer has been helpful) Commented Oct 18, 2013 at 20:45
  • The problem is setting the id of the tr tag. You can't set the id for a rendered tr tag from the apex:pageBlockTable iterator. That is the whole problem. If I could do that, I'd be done and on to the next item to work on. Commented Oct 18, 2013 at 21:25
1

OK so I resolved my problem by rewriting the page. There isn't a way I can control the ID of a tag from the apex:pageBlockTable tag so I built the table with standard and tags. I then used the apex:repeat tag for the iteration through the records. I thought this would be my only option but I was hoping there would be a way to not to have to rewrite the page. Now that I can set the id of the record, it is very easy to find the record using javascript.

Thanks for the suggestions though. This is still a learning process.

You must log in to answer this question.

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