You need to sign in to do that
Don't have an account?
Issue with Datatable in Visualforce..
I am trying to perform some processing when the user clicks on a button after performing multiple selections in list box. Once the button is clicked the selections are divided into a batch of 25 elements each and stored in Map and the action poller is enabled.
The poller picks up the records and processes one batch at a time and returns the results which are being stored in a List<CustomType>. The List<CustomType> is bound to a <apex:dataTable> to display the output.
The Problem:
In case of multiple batches each batch is processed and the List<MyCustomType> is populated correctly with elements from all batches. However when the table is finally rendered, values from only the last batch that was processed are displayed within the table.
The VF code is as follows:
<apex:page controller="MyCtl" action="{!getValuesToSelect}">
<apex:form>
<apex:outputPanel id="pnlInput">
<apex:pageBlock>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!createBatches}" value="Display"/>
</apex:pageBlockButtons>
<apex:actionPoller id="poller" action="{!processBatch}" enabled=" {!IsPollerEnabled}" rerender="pnlDisplayAllFieldDetails" interval="5" status="tableStatus"/>
</apex:form>
<apex:outputPanel id="pnlDisplayAllFieldDetails">
<apex:dataTable value="{!MyCustomList}" var="ft" rendered="{!IF(IsFieldTableVisible == True, True, False)}">
<apex:column headerValue="FIELD 1">
<apex:outputText value="{!ft.Field1}"/>
</apex:column>
<apex:column headerValue="FIELD 2">
<apex:outputText value="{!ft.Field2}"/>
</apex:column>
</apex:dataTable>
</apex:outputPanel>
</apex:page>
The Apex Controller code is as follows:
public class MyCtl{
public List<MyCustomType> MyCustomList = new List<MyCustomType>();
public MyCtl()
{}
public class MyCustomType{
public MyCustomType(String pField1, String pField2)
{
Field1 = pField1;
Field2 = pField2;
}
public String Field1{get; set;}
public String Field2{get; set;}
}
public List<MyCustomType> getMyCustomList()
{ return MyCustomList; }
public void processBatches()
{
/* perform some processing */
MyCustomList.add(new MyCustomType(strField1, strField2));
}
public void createBatches()
{
/*
Iterate over the selection
Create batch of 25 elements each of List<String>
Store the list in Map<Integer, List<String>> BatchMap = new Map<Integer, List<String>>
*/
}
}//End
Can someone please suggest what can be possibly wrong in my code !!
Hi Bob,
Thanks for replying. Finally I managed to figure out the issue.
The issue was that I would add the values from IntermediateList to the DisplayList, list that was bound with the datatable on UI using DisplayList.addAll(IntermediateList). Then I would clear the IntermediateList.
Solution:
What happens is that the DisplayList would still hold the reference of IntermediateList and thus would contain no elements when the IntermediateList was cleared i.e. in Apex, a Shallow Copy of the collection is created when we say List.addAll(ListName) . Hence instead of clearing the list everytime, I created a new reference for the intermediate List.
I think the Controller code below would explain it better.
Appreciate all the help, Bob. Cheers !!
All Answers
Usually when this sort of thing happens it means that you are re-using the same list instance rather than creating a new one each time. Thus each iteration overwrites the previous one.
Are you re-initialising a list by doing list.clear()
Thanks for the response Bob..
What I am doing is, getting output for each batch in a intermediate list and adding all its elements to the List<MyCustomType> MyCustomList. And then I clear the intermediate list. Also I have checked that, MyCustomList contains all the elements, but only issue is, it displays the last batch processed, in the datatable.
Does this problem occur because I am rerendering the outputpanel which contains the datatable using the action poller?
Would putting an extra outputPanel within pnlDisplayAllFieldDetails which would then contain the datatable, help?
Please find the detailed code below:
VF Code:
Controller Code:
This might help !!
In the method that is backing the list:
where does DisplaySFDCFieldList get defined/set up? I can't find any references to it in your controller.
Sorry Bob.. my bad...
Its should have been...
good catch !!.. thanks for the reply...
So are you still having problems with the page?
Yes I am still having problems with the page. Is this problem occuring because the output panel containing the dataTable is being rerendered everytime ??
Hi Bob,
Thanks for replying. Finally I managed to figure out the issue.
The issue was that I would add the values from IntermediateList to the DisplayList, list that was bound with the datatable on UI using DisplayList.addAll(IntermediateList). Then I would clear the IntermediateList.
Solution:
What happens is that the DisplayList would still hold the reference of IntermediateList and thus would contain no elements when the IntermediateList was cleared i.e. in Apex, a Shallow Copy of the collection is created when we say List.addAll(ListName) . Hence instead of clearing the list everytime, I created a new reference for the intermediate List.
I think the Controller code below would explain it better.
Appreciate all the help, Bob. Cheers !!