You need to sign in to do that
Don't have an account?
Roger Pavelle
How do I pass the checked/not checked value from a checkbox to a controller function?
My data has 50+ filterable fields that are boolean values. I am trying to create a function that will filter the data based on whether one of the fields is selected. My idea is that, in the controller function, I will dynamically build a SOQL WHERE clause based on which fields have been selected. Basically, when a box is checked, it will rebuild the WHERE clause to include the corresponding field. If the box is then unchecked, the field will be removed from the WHERE clause.
What I'm not sure about is how to pass the TRUE/FALSE value from the checkbox to the function that will do the building.
In the controller module, I'm planning on having two arrays, one with all the possible where clause strings and another with corresponding boolean values that will determine if the string should be added (the whereArray variable referenced below].
public void buildWhere(Integer int, Boolean value)
{
whereArray[int] = value;
}
So, on the Apex page, how do I go about passing the true/false value to the function?
Thanks.
What I'm not sure about is how to pass the TRUE/FALSE value from the checkbox to the function that will do the building.
In the controller module, I'm planning on having two arrays, one with all the possible where clause strings and another with corresponding boolean values that will determine if the string should be added (the whereArray variable referenced below].
public void buildWhere(Integer int, Boolean value)
{
whereArray[int] = value;
}
So, on the Apex page, how do I go about passing the true/false value to the function?
Thanks.
Here's the final code:
Apex:
<apex:inputCheckbox label="Found in S.W. Pennsylvania" id="regionFoundSWPAChk" >
<apex:actionSupport action="{!setChkBoxValue}" event="onchange" rerender="bolete_list">
<apex:param name="boxNum" value="49"/>
</apex:actionSupport>
</apex:inputCheckbox>
VF:
private List<Boolean> chkBoxValues = new List<Boolean>{false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false};
public void setChkBoxValue()
{
integer boxNum = integer.valueOf(system.currentPageReference().getParameters().get('boxNum'));
boolean before = chkBoxValues[boxNum];
chkBoxValues[boxNum] = !chkBoxValues[boxNum];
System.debug('boxNum:' + boxNum + ' before:' + before);
System.debug(currentChkBoxValues);
}
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 (Edibility__c = ' + whereClauseValues[i] + ')';
}
else
{
whereClause = 'Where ((Edibility__c = ' + whereClauseValues[i] + ')';
exists = true;
}
}
}
if (exists)
{
whereClause = whereClause + ') ';
}
//All the other values are individual values, so they are built with AND
for (Integer i = 4; i<whereClauseValues.size(); i++)
{
if (chkBoxValues[i])
{
if (exists)
{
whereClause = whereClause + 'and ' + whereClauseValues[i] + ' = true ';
}
else
{
whereClause = 'Where ' + whereClauseValues[i] + ' = true ';
exists = true;
}
}
}
}
All Answers
Please check below post. I hope that will help you.
https://developer.salesforce.com/page/Wrapper_Class
Thanks
Amit Chaudhary
1 quick question. Do you have this boolean fields within any object?
Thanks,
Kapil
Example:
No boxes checked - 250 records returned
Box 1 checked - only those records where that box value is true returned - 100 records returned
Box 2 checked - only records where Box 1 & 2 are true returned - 20 records returned.
Box 5 checked - only records where Box 1 & 2 & 5 are true returned - 0 records returned
Box 2 unchecked - only records where Box 1 & 5 are true returned - 40 records returned
Box 8 checked - records where Box 1, 5, 8 iare true returned - 3 records returned.
At this point, it is easy to figure out which record I want to look at and can click on it to show a detail screen.
What I am unable to figure out is how to pass the checked/unchecked value to my controller class (presumably in the onChange method)
Amit Chaudhary 8 - I am not sure if a wrapper class will work in this scenario because the checkboxes are not in the table of returned results. If I am mistaken, please help me understand how to do this.
KapilC - Yes, my custom object has all the boolean fields that are going to be associated with the checkboxes. My plan is to dynamically build an SOQL WHERE clause using those boolean values by passing to a build function a code saying which field should be included (see my original question for how I'm planning this).
You can use "<apex:selectCheckboxes>" and <apex:actionSupport>
Please look at following controllr and VF
VF
if its resolved your issue,please mark as best answer so it will help to other who will serve same problem.
Thanks!
Just so I understand this better in the future, how is the checked value being passed from the VF code to the controller? Is it the rstatus variable? Is it possible to use this variable inside the action statement. In other words, would it work if line 14 read:
14 <apex:actionSupport event="onchange" action="{!getFilteredRecords(rstatus)}" reRender="records" />
Also, I don't see see anything in the suggested code that would remove a filter if the box is unchecked (which is why I want need to pass the value).
Here's the final code:
Apex:
<apex:inputCheckbox label="Found in S.W. Pennsylvania" id="regionFoundSWPAChk" >
<apex:actionSupport action="{!setChkBoxValue}" event="onchange" rerender="bolete_list">
<apex:param name="boxNum" value="49"/>
</apex:actionSupport>
</apex:inputCheckbox>
VF:
private List<Boolean> chkBoxValues = new List<Boolean>{false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,false,false,false,false,false,false,false,false};
public void setChkBoxValue()
{
integer boxNum = integer.valueOf(system.currentPageReference().getParameters().get('boxNum'));
boolean before = chkBoxValues[boxNum];
chkBoxValues[boxNum] = !chkBoxValues[boxNum];
System.debug('boxNum:' + boxNum + ' before:' + before);
System.debug(currentChkBoxValues);
}
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 (Edibility__c = ' + whereClauseValues[i] + ')';
}
else
{
whereClause = 'Where ((Edibility__c = ' + whereClauseValues[i] + ')';
exists = true;
}
}
}
if (exists)
{
whereClause = whereClause + ') ';
}
//All the other values are individual values, so they are built with AND
for (Integer i = 4; i<whereClauseValues.size(); i++)
{
if (chkBoxValues[i])
{
if (exists)
{
whereClause = whereClause + 'and ' + whereClauseValues[i] + ' = true ';
}
else
{
whereClause = 'Where ' + whereClauseValues[i] + ' = true ';
exists = true;
}
}
}
}