You need to sign in to do that
Don't have an account?
Roger Pavelle
Errors calling function
I am writing a controller class that will capture the values of multiple checkboxes and rerender a result set based on those values. To do this, I created an internal list of boolean values which correspond to the check boxes on the Apex form. When a checkbox value changes, I call a function in the controller, passing along the corresponding check box number. The function then reverses the value (true becomes false, false becomes true). I then rerender the results based on this value change.
I've tried this multiple ways and am getting errors for reasons I don't understand. Can someone please explain where my logic or understanding is faulty?
VF code (function to change the saved value of the checkbox - chkBoxValues is the list holding the values, all of which are initially false):
public void chkBoxValue(Integer boxNum)
{
chkBoxValues[boxNum] = !chkBoxValues[boxNum];
}
Apex code:
1) <apex:inputCheckbox title="Choice" label="Choice" onchange="{! chkBoxValue(0) }" />
<apex:commandLink reRender="bolete_list"/>
result: unknown function chkBoxValue. Check spelling
2) <apex:inputCheckbox label="Good">
<apex:commandLink action="{! SetChkBoxValue(1) }" reRender="bolete_list"/>
</apex:inputCheckbox>/>
result: The content of elements must consist of well-formed character data or markup
I've tried this multiple ways and am getting errors for reasons I don't understand. Can someone please explain where my logic or understanding is faulty?
VF code (function to change the saved value of the checkbox - chkBoxValues is the list holding the values, all of which are initially false):
public void chkBoxValue(Integer boxNum)
{
chkBoxValues[boxNum] = !chkBoxValues[boxNum];
}
Apex code:
1) <apex:inputCheckbox title="Choice" label="Choice" onchange="{! chkBoxValue(0) }" />
<apex:commandLink reRender="bolete_list"/>
result: unknown function chkBoxValue. Check spelling
2) <apex:inputCheckbox label="Good">
<apex:commandLink action="{! SetChkBoxValue(1) }" reRender="bolete_list"/>
</apex:inputCheckbox>/>
result: The content of elements must consist of well-formed character data or markup
Visualforce page:
Apex Controller:
All Answers
Try using an apex:actionSupport (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionSupport.htm).
It would be something like:
Hi Roger,
Can you provide some more content from your controller class and Apex page. You shouldn't need to prefix the method name with 'set'
<apex:page setup="false" controller="BoleteController">
<apex:form >
<apex:pageBlock title="Bolete List" id="bolete_list">
<apex:inputCheckbox label="Choice" id="choiceCheckbox" >
<apex:actionSupport action="{!setchkBoxValue}" event="onclick" rerender="bolete_list">
<apex:param name="boxNum" value="1"/>
</apex:actionSupport>
</apex:inputCheckbox>
<apex:inputCheckbox label="Good">
<apex:commandLink action="{! SetChkBoxValue(1) }" reRender="bolete_list"/>
</apex:inputCheckbox>/>
<apex:inputCheckbox label="Iffy">
<apex:commandLink action="{! SetChkBoxValue(2) }" reRender="bolete_list"/>
</apex:inputCheckbox>/>
<apex:inputCheckbox label="Avoid">
<apex:commandLink action="{! SetChkBoxValue(3) }" reRender="bolete_list"/>
</apex:inputCheckbox>/>
<!-- bolete search result list -->
<apex:pageBlock title="Search Results">
<apex:pageBlockTable value="{! Boletes }" var="bol">
<apex:column value="{! bol.genus__c }">
<apex:facet name="header">
<apex:commandLink action="{! sortByGenus }" reRender="bolete_list">Genus
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! bol.species__c }">
<apex:facet name="header">
<apex:commandLink action="{! sortBySpecies }" reRender="bolete_list">Species
</apex:commandLink>
</apex:facet>
</apex:column>
<apex:column value="{! bol.Common__c }"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
</apex:page>
Here's the controller class (minus some extraneous stuff like all the possible values and the sorting functions)
public class BoleteController
{
private String sortOrder = 'Genus__c, Species__c ';
private String whereClause = '';
private List<Boolean> chkBoxValues = new List<Boolean>{false,false,false,false,false,false,false,false,false,};
private List<String> whereClauseValues = new List<String>{'edibilityChoice', 'edibilityGood', 'edibilityIffy', 'edibilityAvoid', };
public List<Bolete__c> getBoletes()
{
buildWhereClause();
List<Bolete__c> results = Database.query(
'SELECT Genus__c, Species__c, Common__c ' +
'FROM Bolete__c ' +
whereClause +
'ORDER BY ' + sortOrder
);
return results;
}
public void setChkBoxValue(Integer boxNum)
{
chkBoxValues[boxNum] = !chkBoxValues[boxNum];
}
private void buildWhereClause()
{
Boolean exists = false;
whereClause = '';
//The first four values are for edibility, which are not mutually exclusive, so they are built
// using or instead of and
for (Integer i = 0; i<4; i++)
{
if (chkBoxValues[i])
{
if (exists)
{
whereClause = whereClause + 'or ' + whereClauseValues[i] + ' = true ';
}
else
{
whereClause = 'Where (' + whereClauseValues[i] + ' = true ';
exists = true;
}
}
}
if (exists)
{
whereClause = whereClause + ') ';
}
//All the other values are individual values, so they are built with AND
for (Integer i = 4; i<57; i++)
{
if (chkBoxValues[i])
{
if (exists)
{
whereClause = whereClause + 'and ' + whereClauseValues[i] + ' = true ';
}
else
{
whereClause = 'Where (' + whereClauseValues[i] + ' = true ';
exists = true;
}
}
}
}
}
Visualforce page:
Apex Controller:
BTW, I'm assuming the outputText and associated code are just for debugging purposes, correct?