[SalesForce] How to i display the result of the dynamic soql query

I have dynamic soql result in my apex code. The fields used for dynamic soql also been selected dynamically. I want to display the result on my VF page with the field name and value in it. My apex dynamic soql result :

(Contact:{Email=agreen@uog.com, AccountId=001bWzAAI, Id=00302yAAG, LastName=Green}, Contact:{Email=spavlova@uog.com, AccountId=0011lbWzAAI, Id=0035QC2oAAG, LastName=Pavlova}, Contact:{Email=lboyle@uog.com, AccountId=000lbWzAAI, Id=005QC2pAAG, LastName=Boyle}, Contact:{Email=asong@uog.com, AccountId=0001lbWzAAI, Id=000015tAAG, LastName=Song})

I want to display this result on my VF page. All the fields and object where condition is generated from dynamic soql query.
my apex dynamic query:

 public void searchResult(){

    //queryBuilder = '';
    string value;
    queryWhereCondition = '';
    system.debug('Wrapper class on search ' + wcls);

    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    Field_Set_Category__c fsc_baseObj = [SELECT Base_object__c, Search_Display__c FROM Field_Set_Category__c WHERE Id = :myid];

    //for(integer i =0; fsc_baseObj.size()){
       // soqlfields= soqlfields + 
    //}

    if(fsc_baseObj.Search_Display__c  != null && fsc_baseObj.Search_Display__c  != '')
        queryBuilder = 'SELECT Id, '+ fsc_baseObj.Search_Display__c +' FROM '+fsc_baseObj.Base_object__c+' Where';
    else
        queryBuilder = 'SELECT Id FROM '+fsc_baseObj.Base_object__c+' Where';

    //Schema.SObjectType objectName = schemaMap.get(fsc_baseObj.Base_object__c);

    Schema.sObjectType parentType = schemaMap.get(fsc_baseObj.Base_object__c);
    //Schema.sObjectType parentType = schemaMap.get('Account'); 
    List<Schema.SObjectField> fieldList = parentType.getDescribe().fields.getMap().values();

    String Referenceobj;

    for(SObjectField fieldLs : fieldList){ 
       system.debug('Looping through field list ' + fieldLs);
       if(string.valueof(fieldLs.getDescribe().getType()) == 'REFERENCE'){

            system.debug('The details about all the fields ' + fieldLs.getDescribe());
            system.debug('To which object is reference ' + (fieldLs.getDescribe()).getReferenceTo() );

            Referenceobj = String.valueof((fieldLs.getDescribe()).getReferenceTo());
            if(Referenceobj.contains(')')){
                Referenceobj = Referenceobj.replace(')', '');                   
            }
            if(Referenceobj.contains('(')){
                Referenceobj = Referenceobj.replace('(', '');        
            }

            querymaker.put(Referenceobj, String.valueof(fieldLs.getDescribe().getRelationshipName()));
       }
    }


    for(wrapperClass wap: wcls){

        system.debug('Index of ' + string.valueof(wap.objectName).indexOf(':', 0));
        system.debug('object name in wrapper class ' + wap.objectName);

        objectName = string.valueof(wap.objectName).substring(0, string.valueof(wap.objectName).indexOf(':', 0));
        system.debug('Name of the object ' + objectName );


        if(querymaker.containsKey(objectName)){
            system.debug('Yes the value conatins ' + querymaker.get(objectName) );
            //system.debug('' + querymaker.get(objectName) );

             fieldNameValue = string.valueof(wap.objectName);

             system.debug('field Name Value ' + fieldNameValue );

             if(fieldNameValue.contains(objectName))
                fieldNameValue = fieldNameValue.replace(objectName,querymaker.get(objectName));
             if(fieldNameValue.contains(':'))
                fieldNameValue = fieldNameValue.replace(':','.');
             if(fieldNameValue.contains('{'))
                fieldNameValue = fieldNameValue.replace('{','');
             if(fieldNameValue.contains('}'))
                fieldNameValue = fieldNameValue.replace('}','');

            if(fieldNameValue.length() != (fieldNameValue.indexOf('.') + 1) ){

                system.debug('Checking is not null ' + fieldNameValue.substring(fieldNameValue.indexOf('=', 0)+1, fieldNameValue.length()));

                if( fieldNameValue.substring(fieldNameValue.indexOf('=', 0)+1, fieldNameValue.length()) != 'null'){

                    fieldName = fieldNameValue.substring(fieldNameValue.indexOf('.', 0)+1, fieldNameValue.indexOf('=', fieldNameValue.indexOf('.', 0)));   
                    system.debug('name of the field ' + fieldName);    

                    Schema.sObjectType objsNameType = schemaMap.get(objectName); 
                    List<Schema.SObjectField> fieldTypList = objsNameType.getDescribe().fields.getMap().values();

                    for(SObjectField fieldLsTyp: fieldTypList){
                        if(string.valueof(fieldLsTyp) == fieldName){

                        if(string.valueof(fieldLsTyp.getDescribe().getType()) == 'ID' || string.valueof(fieldLsTyp.getDescribe().getType()) == 'STRING' ||
                            string.valueof(fieldLsTyp.getDescribe().getType()) == 'PICKLIST' || string.valueof(fieldLsTyp.getDescribe().getType()) == 'PHONE'){

                            value = fieldNameValue.substring(fieldNameValue.indexOf('=', 0)+1, fieldNameValue.length());
                            system.debug('value for search ' + value);

                            fieldNameValue = fieldNameValue.replace(value, '\''+value +'\'');
                            system.debug('Repaced value for search ' + fieldNameValue);

                            if(fieldNameValue.contains('MasterRecord')){
                                fieldNameValue = fieldNameValue.replace('MasterRecord', objectName);
                            }
                            queryWhereCondition = queryWhereCondition + ' ' +fieldNameValue +' AND';
                        }
                        else{
                            if(string.valueof(fieldLsTyp.getDescribe().getType()) == 'INTEGER' ){

                                if(fieldNameValue.contains('MasterRecord')){
                                    fieldNameValue = fieldNameValue.replace('MasterRecord', objectName);
                                }

                                queryWhereCondition = queryWhereCondition + ' ' +fieldNameValue +' AND';
                            }
                        }
                    }
                    }                   
                system.debug('query Builder after adding ' + queryWhereCondition );

            }
            }

        }

    }   
    queryWhereCondition = queryWhereCondition.substring(0, queryWhereCondition.lastIndexOf('AND'));         
    system.debug('query Where Condition ' + queryWhereCondition );

    queryBuilder = queryBuilder +' '+ queryWhereCondition;  
    system.debug('query Builder after adding add fields for SOQL ' + queryBuilder );

    //if(queryBuilder.contains('MasterRecord')){
        //queryBuilder.replaceAll('MasterRecord', objectName);
    //}

    soqlResult = new list<sObject>();
    soqlResult = Database.query(queryBuilder);
    system.debug('Dynamic soql result ' + soqlResult );

Can anyone guide me what are the different ways to display the fields on my VF page.

Best Answer

You need to add a public property to your controller populated with a list of the field names (called fields below) that you want to display (which could be all the values in your fieldList) and then you can use this Visualforce to present the table:

<apex:pageBlockTable var="item" value="{!soqlResult}">
    <apex:repeat var="f" value="{!fields}">
        <apex:column value="{!item[f]}"/>
    </apex:repeat>
</apex:pageBlockTable>