[SalesForce] Filtered related list in Salesforce

How to get a filtered related list in Salesforce? I know that we can get a custom Related List using PageBlock Table in a VF page but i don't know how to get hyperlinks around records in related lists and actions like Edit/Delete like a standard one. Below i have posted screenshots of both related lists..1st is Native Saleforce Related List and 2nd is custom one. How to make custom related list look like the Standard one? Help!!!!

Standard Salesforce Related List which has links to records and actions as well(edit/del)

Custom (filtered) related list using a table in VF page but without links to the records(apart from Account) and no actions.

Thanks

Best Answer

There are many variations to the solution but here is one I've used (you could use CSS better than the nbsp; and the hardcoded styles - I just grabbed this from some existing VF page in my org)

<apex:form>
 ...
<apex:outputPanel id="oppoRelatedList">
<apex:pageBlockTable value="{!givingOpportunities}" var="o">
 <apex:column >
    <apex:facet name="header"><apex:outputText value="Action"/></apex:facet>
    <apex:outputLink title="" value="/{!o.id}/e?retURL=/{!o.id}" 
        style="font-weight: normal; color: rgb(35,111,189); text-decoration: none">Edit
    </apex:outputLink>   
    &nbsp;|&nbsp;
    <a href="javascript:if (window.confirm('Are you sure?')) deleteOppo('{!o.Id}');" 
         style="font-weight: normal; color: rgb(35,111,189); text-decoration: none">Del</a>
 </apex:column>

 <apex:column headerValue="Name">
    <apex:outputLink value="{!URLFOR($Action.Opportunity.View,o.id)}">{!o.name}</apex:outputLink> 
 </apex:column>
 <apex:column value="{!o.otherField1}"/>
 <apex:column value="{!o.otherField2}"/>
</apex:pageBlockTable> 

 <!--  actionFunctions must be within apex:form tabs -->
 apex:actionFunction action="{!deleteOppo}" name="deleteOppo" reRender="oppoRelatedList, pageMsgs" >        
  <apex:param name="oppoid" value="" assignTo="{!selectedOppoId}"/>
 </apex:actionFunction>
...
 </apex:form>

You'll need a controller that

a) returns the list of related Opportunities in a getter for givingOpportunities

b) has a setter for the selectedOppoId to be deleted

c) has an Action Method deleteOppo that deletes the Opportunity that is something like this

    public  ID      selectedOppoId                  {get; set;}

    // ----------------------------------------------------------------------------------------
    // ACTION METHOD    - deleteOppo
    // ----------------------------------------------------------------------------------------
    public PageReference deleteOppo() {
        PageReference resPg;
        if (this.selectedOppoId != null) 
            try {delete new Opportunity(id = this.selectedOppoId);}
            catch (Exception e) {ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,' Delete of Opportunity failed, reason: ',e));
            }
        return resPg;
    }