Wednesday, March 13, 2013

Displaying records with Command Link using Custom Controller

Ex:1

Visualforce Page:

<apex:page controller="reccls" >
      <apex:Form >
          <apex:Pageblock >
              <apex:pageBlockTable value="{!accs}" var="a">
                  <apex:column >
                      <Apex:commandLink value="{!a.name}" action="{!redirect}">
                          <apex:param value="{!a.id}" assignTo="{!pid}" name="p1"/>
                      </apex:CommandLink>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:Pageblock>
      </apex:Form>
</apex:page>

Apex Class:

public with sharing class reccls {

    public PageReference redirect() {
        System.debug('---------->'+pid);
        pagereference ref = new pagereference('/'+pid);
        return ref;
                                                      }

    public string pid {get; set;}
    public List<Contact> accs { get; set; }
    
    public reccls(){    
        accs = new List<Contact>();
        accs =[Select id, name from Contact];
                         }    
                                                }

Ex:2

Visualforce Page:


<apex:page controller="reccls" >
      <apex:Form >
          <apex:Pageblock >
              <apex:pageBlockTable value="{!accs}" var="a">
                  <apex:column Style="width:50px" headerValue="Action" >
                      <apex:commandLink value="Edit" action="{!editrec}" style="color: #015BA7;font-weight: normal;">
                       <apex:param value="{!a.id}" assignTo="{!pid}" name="e"/>
                      </apex:commandLInk>
                       &nbsp;|&nbsp;
                      <apex:commandLink value="Del" action="{!delrec}" style="color: #015BA7;font-weight: normal;">
                       <apex:param value="{!a.id}" assignTo="{!pid}" name="d"/>
                      </apex:commandLInk>
                  </apex:Column>
                  <apex:column headerValue="Contact" >
                      <Apex:commandLink value="{!a.name}" styleclass="actionLink" action="{!redirect}">
                          <apex:param value="{!a.id}" assignTo="{!pid}" name="p1"/>
                      </apex:CommandLink>
                  </apex:column>
              </apex:pageBlockTable>
          </apex:Pageblock>
      </apex:Form>
</apex:page>

Apex Class:

public with sharing class reccls {

    public PageReference delrec() {
       System.debug('---------->'+pid);
       Contact c =[Select id from Contact where id=:pid];
       delete c;
       
       pagereference ref= new pagereference('/apex/param');
       ref.setredirect(true);
       return ref;

                                                   }


    public PageReference editrec() {
        System.debug('---------->'+pid);
        pagereference ref = new pagereference('/'+pid+'/e');
        return ref;
                                                    }


    public PageReference redirect() {
        System.debug('---------->'+pid);
        pagereference ref = new pagereference('/'+pid);
        return ref;
                                                      }

    public string pid {get; set;}
    public List<Contact> accs { get; set; }
   
    public reccls(){    
        accs = new List<Contact>();
        accs =[Select id, name from Contact];
                         }   
                                                    }

No comments:

Post a Comment