+ Start a Discussion
Debd89Debd89 

How to Represent a Dynamic query in a PageBlockTable or a Grid in Visualforce

I was preparing this page where I need to Show All fields of Account object and some selected fields from that list Should only should be queried from the Account object. All is resolved till now that I face one major hurdle. Now I need to represnt this Dynamically Queried Account List in a table or Grid in the page itself. The problem lies in the fact that the queried columns names are not known to me but in a " Set<String> rightvalues " so the <apex:pageBlockTable value="{!accList}" var=" dispAcc"> <apex:column value="{!dispAcc.?}> .....How do I resolve this?

 

my code is as given under this following

 

The Controller :

public with sharing class GridRep {

public list<Account> accList = new list<Account>();
public list<Account> getAccList()
{
return accList;
}
// public list<Account> accList{get;set;}
public list<String> availableFields{get;set;}
public list<String> selectedFields {get;set;}
public Set<String> leftvalues = new Set<String>();
public Set<String> rightvalues=new Set<String>();
public String strQuery;

public GridRep(){
availableFields = new List<String>();
selectedFields = new List<String>();

}
public List<String> getFieldList(Map<String, Schema.SObjectField> fieldMap)
{
//Make a comma-separated list of the fields
List<String> fieldList =new List<String>();
for(String fieldName:fieldMap.keySet())
{
Schema.DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe();
fieldList.add(fieldDescribe.getName());

}
System.debug(fieldList);
return fieldList;
}


public PageReference refreshPage()
{
availableFields = new List<String>();
Map<String, Schema.SObjectField> fieldMap=new Map<String, Schema.SObjectField>();
fieldMap = Schema.SObjectType.Account.fields.getMap();
List<String> fieldNames=getFieldList(fieldMap);
fieldNames.sort();
for (String fldName:fieldNames)
{
availableFields.add(fldName);
leftvalues.add(fldName);
}

return null;
}

public List<SelectOption> getunSelectedFields()
{
List<SelectOption> optionFields = new List<SelectOption>();
List<string> tempList = new List<String>();
tempList.addAll(leftvalues);
tempList.sort();
for(string s : tempList)
optionFields.add(new SelectOption(s,s));
return optionFields;
}
public PageReference selectclick()
{
selectedFields.clear();
for(String s : availableFields)
{
leftvalues.remove(s);
rightvalues.add(s);
}
return null;
}
public List<SelectOption> getSelectedValues()
{
List<SelectOption> optionsFields2 = new List<SelectOption>();
List<string> tempList2 = new List<String>();
tempList2.addAll(rightvalues);
tempList2.sort();
for(String s : tempList2)
optionsFields2.add(new SelectOption(s,s));
return optionsFields2;
}

public PageReference unselectclick()
{

for(String s : selectedFields)
{
rightvalues.remove(s);
leftvalues.add(s);

}
return null;
}

public pagereference find()
{
strQuery='Select ';
for(String temp:rightvalues)
{
strQuery+=temp+',';
}
strQuery=strQuery.removeEnd(',');
strQuery+=' from Account';

accList=new list<Account>();
try
{
accList=Database.query(strQuery);
}catch(Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error in Database Fetch');
ApexPages.addMessage(errormsg);
}

return null;
}


}

 

and the page as is :

 

<apex:page controller="GridRep" sidebar="false" >
<apex:form id="fm1">
<apex:sectionHeader title="Grid Like Representation"/>
<apex:commandButton action="{!refreshPage}" value="Populate Available"/>
<apex:pageBlock >
<apex:pageBlockButtons location="top">
<apex:commandButton value="Find" action="{!find}" id="button1"/>

</apex:pageBlockButtons>
<apex:panelGrid columns="3" id="abcd">
<apex:selectList value="{!availableFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!unSelectedFields}" />
</apex:selectList>
<apex:panelGroup >
<apex:commandButton value="Transfer Right" action="{!selectclick}" reRender="abcd"/>
<apex:commandButton value="Transfer Right" action="{!unselectclick}" reRender="abcd"/>
</apex:panelGroup>
<apex:selectList value="{!selectedFields}" multiselect="true" style="height:80px;width:150px" >
<apex:selectOptions value="{!SelectedValues}" />
</apex:selectList>
</apex:panelGrid>
<apex:pageBlockSection >
<!--   Here Should Be the code to Display Dynamic queried list accList -->

</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Jake GmerekJake Gmerek

You have to use dynamic Visualforce for that.  Here is an example from something similar that I did with related lists, but the concept is the same in that I needed to set the number and type of columns dynamically:

 

http://jakemuse.blogspot.com/2012/03/visualforce-custom-related-list.html