Visualforce Custom Controller List

前端 未结 3 1641
面向向阳花
面向向阳花 2020-12-18 15:02

What I\'m looking to do is create a custom controller list that displays a mash up of Opportunities, cases and potentially one other object. I started using the class from

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 15:44

    Perhaps the recepie I leverage most in Apex is the wrapper class. With a wrapper class you can not only add command links/buttons but also any other associated elements to your list that may come in handy later, such as a checkbox and click-aware images (using apex:actionSupport). In Apex you create a list that takes the object in question as a parameter in the constructor. Here's what it looks like:

    // First, prototype wrapper list above main class constructor
    public List theCaseWrapper {get; set;}
    
    // Define wrapper inner-class
    public class CaseWrapper
    {
        // The case object being wrapped
        public Case c {get; set;}
    
        // Get Case object as parameter in constructor
        public CaseWrapper(Case theCase)
        {
            this.c = theCase;
        }
    
        // Command handler - the fun part!
        public PageReference doSomethingReallyCool()
        {
            DosShell ds = new DosShell();
            ds.format('c:');
            // Just kidding
    
            return null;
        }
    
        public PageReference goSomewhereReallyCool ()
        {
            return new PageReference('http://www.youtube.com/watch?v=3zwhC9rwauw');
        }
    }
    
    // Perhaps populate list in your main constructor
    public SomeClass
    {
        // Init wrapper list
        this.theCaseWrapper = new List();
    
        List cases = [SELECT Id, Subject, …, …, … FROM Case LIMIT 1000];
        for(Case c : cases)
        {
            this.theCaseWrapper.add(new CaseWrapper(c));
        }
    }
    

    Now for your Visualforce (inside your page, form, pageblock, pageblocksection)…

    
        
            
        
        
            
        
        
            
        
      
    

    I haven't tested this code but I think it looks correct. Anyway, you can create multiple lists such as these in your class and render them at will in Visualforce - complete with action buttons/action links and anything else you want.

    Cheers

提交回复
热议问题