4

I've made another question since I couldn't find an affordable solution to my other post (Inline ajax edit of pageBlockTable columns).

So what I'm looking now, and it's my only problem, is how I can find the ID of the row that I clicked on a table/list. I've made two buttons (Edit and Delete) on the left side of each row. It always opens a popup for creating a new record, but now I need to populate the fields from when I press "Edit", as well as finding which record to update and delete.

Some code (it already lists):

Edit  |  Del

The {!URLFOR($Action.SomeObject__c.Delete, item.id)}" is to be replaced by the call of the popup, it just like this while I can't find a solution.

Best Regards

7

You can pass a javascript function the Id of the record you are working with and then open a VF page with the Id as a URL parameter.

This is assuming you are using a PageBlockTable to iterate over your records and you have a command button in a column in that table. I took some code that I have used in the past as an example

<apex:column headerValue="Edit">
     <apex:commandButton onclick="myFunction('{!var.Id}');return false;" value="Open Popup"/>
</apex:column>

<script>
function myFunction(myId) {
    var url = '/apex/myVFPage?theId=' + myId;
        newWin = window.open(url, '', toolbar=0, location=0, status=0, menubar=0, scrollbars=1, resizable=1, width=700, height=400, left=200, top=200');

    if (window.focus) {
        newWin.focus();
    }    
    return false;
}
</script>

EDIT

If you want to just pass the Id to the controller you can make the button call an action function and pass the Id as a param

<apex:column headerValue="Edit">
     <apex:commandButton onclick="myFunction('{!var.Id}');" value="Open Popup"/>
</apex:column>

<apex:actionfunction name="myActionFunction" action="{!myControllerMethod}">
     <apex:param name="recordId" value=""/>
</apex:actionfunction>

<script>
function myFunction(myId) {
    myActionFunction(myId);
}
</script>

EDIT

In the Controller

public myControllerMethod(){
     Id myId = system.currentPageReference.getParameters.get('recordId');
}
7
  • Thanks for the answer. That was the method I was using in the first place, but for a user-friendly oriented method I want to use that "new page" as a popup modal dialog form, which is already being used but only to insert data at the moment, where I can create a new object without needing any ID. The thing I'm missing is getting that ID into my controller to edit the chosen row. – Rui Rocha Oct 2 '13 at 1:10
  • If you need to pass the Id to the controller, you can use an actionfunction and pass a Apex:param that is the Id of the record – Chris Duncombe Oct 2 '13 at 1:12
  • Added some code to demonstrate – Chris Duncombe Oct 2 '13 at 1:21
  • Thank you very much for the answer. Sorry for asking, but can you confirm me how will !myControllerMethod fetch the parameter? <apex:param name="recordId" value=""/> will have the dynamic value of myId everytime it is invoked right? Can't test at the moment so I'm asking so I can use it when I'm able to. Thanks you once again! – Rui Rocha Oct 2 '13 at 2:03
  • No prob, Updated the code to show how to fetch the value on the controller. Hope that helps – Chris Duncombe Oct 2 '13 at 2:31

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.