function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
abivenkatabivenkat 

display the list's values in the UI - how to achieve it?

 

hi,

 

I am constructing and running a query during the run time in my application based on the user's input. Group By is also used in the query, so i am getting the values in the List<AggregateResult> list and i am converting that list to another list with the use of the wrapper class called List<ListConversion>. Now i am returning this List<ListConversion> List from the controller class and i wanna know how to display the values of the list in the UI using Datatable or pageBlockTable or some other VF components???

 

Note: The point is, while referencing the list values in the pageBlockTable or in DataTable, we should know the Column name to bind it, but, here in this scenario, the column names are dynamic (as shown in the example below). so how to bind the list to the DataTable or pageBlockTable?? 

 

<apex:pageBlockTable columns="2" value="{!ListOfVals}" var="item" id="Table">
<apex:column >
<apex:outputField value="{!item.Name of the first column}" />
<apex:facet name="header">{!Name of the first column}</apex:facet>
 </apex:column>
<apex:column >
<apex:outputField value="{!item.Name of the second column}" />
<apex:facet name="header">{!Name of the second column}</apex:facet>
 </apex:column>
</apex:pageBlockTable> <br />

 

how to bind the dynamic column names.. List is not the ordinary list, it is a list return by the wrapper class called as ListConversion (List<ListConversion>).. Please help me as this is my urgent requirement. Thanks in advance..

 

 

 

Thanks,

abivenkat,

SFDC Learner

aballardaballard

You should be able to do this using dynamic references item[colname]  (see the documentation if you are not familiar with them).  You will need something in your controller that provides the names of the columns.  (Since you constructed the query, I assume something in your code must know what columns you are getting....)

abivenkatabivenkat

 

hi aballard,

 

Thanks for the reply yaar. You are absolutely right about my scenario. I am using the syntax like what you suggest me, like item[colname]. It is working. But, i have specified it using the

 

<apex: column>

<apex: outputField value="{!item[firstColumnName}">

</apex:column>

<apex: column>

<apex: outputField value="{!item[secondColumnName}">

</apex:column>

 

It is throwing some issue like below, 

 

Could not resolve the entity from <apex:outputField> value binding '{!item[firstColumn]}'. outputField can only be used with SObject fields. 

 

How to resolve this issue and which component should i use to bind the value?? need suggestions for achieving this...

 

Thanks,

abivenkat,

SFDC Learner

aballardaballard

What is the type of ListConversion?   OutputField needs to be bound to an sObject.

abivenkatabivenkat

 

hi aballard,

 

sorry for the delayed reply. Its a "ListConversion" Type. 

So i have just changed the <apex:outputField> to <apex:outputText>, now it is giving me different error as given below 

 

Unknown property 'ControllerDynamicLookup.ListConversion.Name'

Error is in expression '{!item[firstColumn]}' in component <apex:outputText> in page dynamiclookup
Error evaluating dynamic reference 'Name'

ControllerDynamicLookup - is name of the Apex Controller Class
ListConversion - is name of the wrapper class
Name - is the field name selected from the contact object.

I have already specified my pageBlockTable in the previous posts. Only thing which differs is the outputText instead of outputField, which is shown below. 

<apex:outputText value="{!item[firstColumn]}" />

how to fix this issue.?? please help

Thanks, 
abivenkat,
SFDC Learner

aballardaballard

I need to know how ListConversion is defined.  

item is going to be an object of type ListConversion, not an sObject. 

 

I suspect you need an expression like item.something[firstColumnName] to retrieve the sobject then select the field out of it....

 

abivenkatabivenkat

 

hi aballard,

i will give my code of thi functionality, can u say wt i have to do to avoid this issue.???

 

 

//Below is my method which calls the ListConversion wrapper class and performs the functionality

public List<ListConversion> tempList = new List<ListConversion>();

public List<AggregateResult> aggLst = new List<AggregateResult>();

 

public void ListPreview() {

aggLst =Database.query('select '+FieldName1+' ,'+FieldName2+' from account group by '+FieldName1+' ,'+FieldName2);

for(AggregateResult ar : aggLst) {
ListConversion listCon = new ListConversion(ar, FieldName1, FieldName2);
tempList.add(listCon);
}

}

 

//Below code is my wrapper class

class ListConversion {
public string field1 { get; set; }
public string field2 { get; set; }
//Constructor for the wrapper class
public ListConversion(AggregateResult arr, string fieldName1, string fieldName2) {
field1 = (string)arr.get(fieldName1);
field2 = (string)arr.get(fieldName2);
system.debug('the field names are '+field1+' ,'+field2);
}
}

 

//Below is the method where the ListConversion List is returned to the pageBlockTable

public List<ListConversion> getListOfVals(){
try {
system.debug('reached the list returning method. size is '+tempList.size());  //am getting this log correctly...
return tempList;
}
catch(Exception ex) {
ApexPages.addMessages(ex);
return null;
}
}

 

is there any way to convert the List<ListConversion> to a sObject List and return to the UI to display in pageBlockTable??? please help...

 

Thanks,

abivenkat,

SFDC Learner

aballardaballard

Ok, with ListConversion as you have written it, you would just access the properties as item.field1 and item.field2 .  Nothin needing the subscripted reference syntax there. 

 

Alternatively you probably don't need the wrapper at all.  You could just access the original sobject list with dynamic references.

 

i.e., in your controller

 

make aggLst public, and provide public variables for accessing the fieldnames. 

 

then something like

 

<apex:pageBlockTable columns="2" value="{!AggLst}" var="item" id="Table">
<apex:column >
<apex:outputField value="{!item[fieldname1]}" />

 

should work.