2012-08-01 61 views
0

我有一个动态选项列表字段,其中包含组织中所有顶点类名称。页面上还有一个“显示”按钮。现在如果用户从这个选项列表中选择一个值并点击显示按钮,那么该类的顶点代码应该显示在下面。 请建议我如何在我的VF页面中实现它。单击“显示”按钮时显示顶点类代码

谢谢!点击该按钮时

public class SomeController { 

    private List<ApexClass> allApexClasses; 
    public String selectedClass {public get; public set;} 
    public String apexCodeOutput {public get; private set;} 

    public SomeController() { 
     // only select classes that aren't part of a managed package, since you won't be able to view the body 
     allApexClasses = [select id, name, body from ApexClass where lengthwithoutcomments <> -1 order by name asc]; 
    } 

    public List<SelectOption> getClassList() { 
     List<SelectOption> opts = new List<SelectOption> opts; 
     for (ApexClass ac : allApexClasses) 
      opts.add(new SelectOption(ac.Id, ac.Name)); 
     return opts; 
    } 

    public PageReference show() { 
     if (selectedClass != null) { 
      Id classId = (Id) selectedClass; 
      for (ApexClass ac : allApexClasses) { 
       if (classId == ac.Id) { 
        apexCodeOutput = ac.body; 
        break; 
       } 
      } 
     } 
     return null; 
    } 
} 

,然后在VF页,只需重新呈现输出代码:

<apex:form > 
<apex:selectList value="{!selectedClass}" size="5"> 
<apex:selectOptions value="{!ClassList}" ></apex:selectOptions> 
</apex:selectList> 
<apex:pageBlock > 
<apex:commandButton action="{!show}" value="Show" id="Button"/> 
<apex:pageBlockSection title="My Current Class"> 

回答

1

你可以查询ApexClass对象的body领域,你在找什么。您需要在代码周围使用<pre>标签来保持间距,以便代码可读。

<apex:form> 
    <apex:selectList value="{!selectedClass}" size="5"> 
     <apex:selectOptions value="{!ClassList}" ></apex:selectOptions> 
    </apex:selectList> 
    <apex:pageBlock > 
     <apex:commandButton action="{!show}" value="Show" rerender="apexoutput" id="Button"/> 
     <apex:pageBlockSection title="My Current Class"> 
      <apex:outputPanel id="apexoutput"> 
       <pre>{!apexcodeoutput}</pre> 
      </apex:outputPanel> 
     </apex:pageBlockSection> 
    </apex:pageBlock> 
</apex:form>