I have a VFP that displays three different views from the same table, Case. I want to be able to remember the sort column and direction of each of the three views and using a view state seemed to be the right thing to do but I cannot get it to do anything.
Reading through An_Introduction_to_Visualforce_View_State (https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State" target="_blank) it says that the View State contains "All non-transient data members in the associated controller (either standard or custom) and the controller extensions".
My controller has a number of String variables defined as public, one example is:
public String sortOrderNew {
get { if (sortOrderNew == null) { sortOrderNew = 'CreatedDate'; } return sortOrderNew; }
set;
}
I was expecting this value to be retained across visits the VFP that is linked to this controller but this does not happen. Every time I visit the page these public, non-transient variables are reset.
I did try and include a "public PageReference save()" function in the controller but the examples in the above document indicate that I should use "update" to save the data. Sadly this only works on sObjects, not on simple string values.
I would appreciate any suggestions of information you can provide on this. It shouldn't be this difficult.
[edited to provide more information]
It is a single page with a single controller. The page displays three apex:pageBlockTable tables and each table shows the data from a different SoQL statement. I have set up the VFP that changes the sort order like this:
<apex:column>
<apex:facet name="header">
<apex:commandLink value="Case #" action="{!toggleSortNew}" rerender="results,debug">
<apex:param name="sortField" value="CaseNumber" assignTo="{!sortOrderNew}"/>
<apex:outputPanel rendered="{!BEGINS(sortOrderNew,'CaseNumber')}">
<apex:image value="{!IF(sortDirNew = 'desc','/img/arrowDown.gif','/img/arrowUp.gif')}"/>
</apex:outputPanel>
</apex:commandLink>
</apex:facet>
<apex:outputLink value="/{!case.Id}">{!case.CaseNumber}</apex:outputLink>
</apex:column>
When the column title is clicked it calls !toggleSortNew and sets sortOrderNew to 'CaseNumber'. different columns have the same toggle but set sortOrderNew to the field name of that column.
toggleSortNew looks like this:
public void toggleSortNew() {
// Toggle the direction
sortDirNew = sortDirNew.equals('asc') ? 'desc' : 'asc';
// run the query again
runQuery();
}
sortDirNew is defined in the Apex code as:
public String sortDirNew {
get { if (sortDirNew == null) { sortDirNew = 'asc'; } return sortDirNew; }
set;
}
and sortOrderNew is coded as:
public String sortOrderNew {
get { if (sortOrderNew == null) { sortOrderNew = 'CreatedDate'; } return sortOrderNew; }
set;
}
The runQuery() function simply creates the SoQL statement that retrieves the rows from the Case table, basing the "Order By" statement on the values of "sortOrderNew" and "sortDirNew".
I want to be able to store the values of "sortOrderNew" and "sortDirNew" so that when a user revisits the page their sort options are shown again, rather than having to reselect them.
I hope this more clearly explains what I am after.