salesforce apex + vf page 动态显示object 和 动态加载显示的列

1. salesforce apex class:

public with sharing class YUT_DynamicTableController {
//List displayed on UI  
    public List<selectoption> supportedObject {get; set;}  
      
    //Selected Object  
    public String SelectedObject {get; set;}  
      
    //Global describe  
    Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();  
    Set<String> objectKeys = gd.keySet();  
      
    //Field Select List  
    public List<SelectOption> fieldLableAPI {get; set;}  
      
    //Selected fields to be displayed in table  
    public List<String> SelectedFields {get; set;}  
      
    //List to maintain dynamic query result  
    public List<sObject> ObjectList {get; set;}  
      
      
    //Constructor  
    public YUT_DynamicTableController()  
    {  
        //Initialize  
        supportedObject = new List<selectoption>() ;  
        SelectedObject = '' ;  
        fieldLableAPI = new List<SelectOption>() ;  
        SelectedFields = new List<String>() ;  
        ObjectList = new List<sObject>() ;  
          
        //Get only reference to objects  
        for(Schema.SObjectType item : ProcessInstance.TargetObjectId.getDescribe().getReferenceTo())  
        {  
            //Excluding custom setting objects  
            if(!item.getDescribe().CustomSetting)  
            {  
                //Adding to list  
                supportedObject.add(new SelectOption(item.getDescribe().getLocalName().toLowerCase() , item.getDescribe().getLabel() ));  
            }  
        }  
          
    }  
      
    //Get fields of selected object  
    public void ObjectFields()  
    {  
        if(SelectedObject != '--None--')  
        {  
        	fieldLableAPI.clear();
            //Creating sObject for dynamic selected object  
            Schema.SObjectType systemObjectType = gd.get(SelectedObject);  
            //Fetching field results  
            Schema.DescribeSObjectResult r = systemObjectType.getDescribe();  
                  
            Map<String, Schema.SObjectField> M = r.fields.getMap();  
            //Creating picklist of fields  
            for(Schema.SObjectField fieldAPI : M.values())  
            {  
                fieldLableAPI.add(new SelectOption(fieldAPI.getDescribe().getName() , fieldAPI.getDescribe().getLabel())) ;  
            }  
        }  
    }  
      
    public void ShowTable()  
    {  
        //Creating dynamic query with selected field  
        String myQuery = 'Select Id ' ;  
          
        for(String field : SelectedFields)  
        {  
            if(field.toLowerCase() != 'id' && field.toLowerCase() != '--none--')  
            myQuery += ','+ field + ' ' ;  
        }  
          
        //Limit is 100 for now you can change it according to need  
        myQuery += ' from ' + SelectedObject + ' LIMIT 100' ;  
          
        //Executing the query and fetching results  
        ObjectList = Database.query(myQuery) ;  
    }  
}

2. VF Page:

<apex:page controller="YUT_DynamicTableController">  
<apex:pageBlock >  
    <apex:form >  
        <apex:actionFunction name="ObjectFileds" action="{!ObjectFields}"/>  
          
        <apex:commandButton value="Show Table" action="{!ShowTable}"/>  
          
        <apex:pageBlockSection >  
            <apex:pageBlockSectionItem >  
                <apex:outputLabel value="Select Object"/>  
                <apex:selectList multiselect="false" size="1" value="{!SelectedObject}" οnchange="ObjectFileds();">  
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>  
                    <apex:selectoptions value="{!supportedObject}" />  
                </apex:selectlist>  
            </apex:pageBlockSectionItem>  
              
            <apex:pageBlockSectionItem >  
                <apex:outputLabel value="Select Field"/>  
                <apex:selectList multiselect="true" size="5" value="{!SelectedFields}">  
                    <apex:selectOption itemLabel="--None--" itemValue="--None--"/>  
                    <apex:selectoptions value="{!fieldLableAPI}" />  
                </apex:selectlist>  
            </apex:pageBlockSectionItem>  
              
            <apex:pageBlockTable rendered="{!IF(ObjectList.size > 0 , true , false)}" value="{!ObjectList}" var="rec">  
                <apex:column value="{!rec.Id}" rendered="{!IF(SelectedFields.size == 0 , true, false)}"/>  
                <apex:repeat value="{!SelectedFields}" var="FieldLable">  
                    <apex:column value="{!rec[FieldLable]}" rendered="{!IF(FieldLable != '--None--' , true, false)}"/>  
                </apex:repeat>  
            </apex:pageBlockTable>  
              
            <apex:outputPanel rendered="{!IF(ObjectList.size < 1 , true , false)}">  
                <apex:pageMessage severity="ERROR" summary="No records to display"/>  
            </apex:outputPanel>  
              
        </apex:pageBlockSection>  
          
    </apex:form>  
</apex:pageBlock>  
</apex:page>

3.显示效果:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值