You need to sign in to do that
Don't have an account?
Onchange of Checkbox value, it is not getting changed in database
Here hidden_field__c is a checkbox.
When in the VFP if user changes the checkbox to true in the database it still shows as false and vice-versa
Can someone please point out what's missing in my code.?
This is my simple code
<pre>
public class dataTableCon {
List<Account> accounts;
public List<Account> getAccounts() {
if(accounts == null) accounts = [select name, owner.name,hidden_field__c from account limit 10];
return accounts;
}
}
</pre>
<pre>
<apex:page controller="dataTableCon" id="page">
<apex:form >
<apex:pageBlock id="theBlock">
<apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column >
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" rerender="theBlock"/>
</apex:inputCheckbox>
</apex:column>
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}" >
</apex:column>
<apex:column >
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.owner.name}" >
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>
</pre>
When in the VFP if user changes the checkbox to true in the database it still shows as false and vice-versa
Can someone please point out what's missing in my code.?
This is my simple code
<pre>
public class dataTableCon {
List<Account> accounts;
public List<Account> getAccounts() {
if(accounts == null) accounts = [select name, owner.name,hidden_field__c from account limit 10];
return accounts;
}
}
</pre>
<pre>
<apex:page controller="dataTableCon" id="page">
<apex:form >
<apex:pageBlock id="theBlock">
<apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column >
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" rerender="theBlock"/>
</apex:inputCheckbox>
</apex:column>
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}" >
</apex:column>
<apex:column >
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.owner.name}" >
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>
</pre>
<apex:column rendered="{!!account.hidden_field__c}">
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" action="{!saveAccount}" rerender="theBlock">
<apex:param name="accountId" value="{!account.Id}"/>
</apex:actionSupport>
</apex:inputCheckbox>
</apex:column>
public Id accountId { get; set; }
public void saveAccount()
{
Map<Id,Account> map_Accounts = new Map<Id,Account>( accounts );
update map_Accounts.get( accountId );
}
</pre>
All Answers
You need to add a save method in you controller to save your changes and a button in your vf page to call that save method.
in controller:
public pageReference save(){
try{
insert accounts;
}catch(Exception e){
System.debug(e.getMessage());
}
return null;
}
in vfPage within Page Block:
<apex:pageBlockBottons>
<apex:commandButton action="{!save}" value="Save" id="theButton"/>
</apex:PageBlockButtons>
-Glyn
Actually I don't want this happening on the click of save button.
When the checkbox is clicked, I want to pass the page parameters to the controller, Capture the row Id and check if the checkbox is set to true or false.
If it is true then not show it in the datatable. I am not deleting it from the data base, just not showing it on the data table to a certain group of people.
Any suggestions.
Thanks.!!
The VF code in the next post hides the columns of the dataTable if the record has the Hidden_Field__c checkbox checked. Let me know if this does what you want.
Glyn Anderson
Sr Developer | System Analyst | ClosedWon | closedwon.com
Certified Developer | Certified Advanced Administrator
Blog: GlynATheApexGuy.blogspot.com
Twitter: @GlynAtClosedWon
<apex:page controller="dataTableCon" id="page">
<apex:form >
<apex:pageBlock id="theBlock">
<apex:dataTable value="{!accounts}" var="account" id="theTable" rowClasses="odd,even" styleClass="tableClass">
<apex:column rendered="{!!account.hidden_field__c}">
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" rerender="theBlock"/>
</apex:inputCheckbox>
</apex:column>
<apex:column rendered="{!!account.hidden_field__c}">
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!account.name}" >
</apex:column>
<apex:column rendered="{!!account.hidden_field__c}">
<apex:facet name="header">Owner</apex:facet>
<apex:outputText value="{!account.owner.name}" >
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form>
</apex:page>
</pre>
No i tried rendering before, this hides the row from the DataTable, but in the database, the checkbox doesn't get updated.
eg: if i make the hidden_field__c 'true' for row1 in the datatable, in the database the hidden_field__c must get updated to true and vice-versa.
Basically i am trying to do is dynamically making a record private or public.
I have created a VFP that displays a datatable of related custom_comments__c. since each parent record would have many comments, it's not a good user experience to go into individual chidl record to make it as private/public.The hidden_field__c checkbox is accssible only to certain users who can make the child record as public/private. If it is marked as private, these ceratin users can view the record but other user's cannot view it.
I have these certain users in a permission set.
Let me know if this helps.
-Glyn
<apex:column rendered="{!!account.hidden_field__c}">
<apex:facet name="header">Private</apex:facet>
<apex:inputCheckbox value="{!account.hidden_field__c}" >
<apex:actionSupport event="onchange" action="{!saveAccount}" rerender="theBlock">
<apex:param name="accountId" value="{!account.Id}"/>
</apex:actionSupport>
</apex:inputCheckbox>
</apex:column>
public Id accountId { get; set; }
public void saveAccount()
{
Map<Id,Account> map_Accounts = new Map<Id,Account>( accounts );
update map_Accounts.get( accountId );
}
</pre>
i added the assignTo in the param tag. Without the assignTo i was getting the null pointer exception.
<apex:param name="accountId" value="{!account.Id}" assignTo="{!accountId}"/>
-Glyn