You need to sign in to do that
Don't have an account?
Soozee
How to pass record id to controller from vf page with checkbox 'onclick'
Hi - I want to dynamically update a record when a user clicks a checkbox on my vf page, but I cannot figure out how to pass the record id of the row that is clicked to the Controller.
Try this:
create a variable in your controller
public String selectedLetterID {get;set;}
Then set it with the param tag.
<apex:inputfield value="{!letter.Suppressed__c}" id="suppressBox" onclick="SupressLetter('{!letter.Id}');return false;"/>
<apex:actionFunction name="SupressLetter" action="{!suppressLetter}" rerender="out">
<apex:param name="firstparam" assignto="{!selectedLetterID}" value="" />
</apex:actionFunction>
Then update your record based on the id that is now assigned to your new variable in your controller method.
All Answers
Can you give us a little more information - it sounds like you are using a table or repeat, is that correct? How are you generating the collection of records the user can choose from?
Sorry for the lack of information, here is more...
I am displaying information in a table. The table is populated by a SOQL query in my controller.
The value of the checkbox is an actual field in the object. Here is where I define the column containing the checkbox in the vf page:
<apex:column >
<apex:facet name="header" >
<!-- this makes the column sortable -->
<apex:outputLink onclick="SortBySuppressed();return false;">Suppressed</apex:outputLink>
</apex:facet>
<!-- this is the checkbox that I want to make work like a button -->
<apex:inputfield value="{!letter.Suppressed__c}" id="suppressBox" onclick="SupressLetter();return false;"/>
</apex:column>
And then SupressLetter() calls:
<apex:actionFunction name="SupressLetter" action="{!suppressLetter}" rerender="out"/>
And suppressLetter is a PageReference in my controller that updates the record selected, but I can't figure out how to pass the record id to the controller from the VF page.
Thanks for your time!
Since I couldn't figure out how to identify the selected row, I just updated all rows and this solved my immediate issue.
Try this:
create a variable in your controller
public String selectedLetterID {get;set;}
Then set it with the param tag.
<apex:inputfield value="{!letter.Suppressed__c}" id="suppressBox" onclick="SupressLetter('{!letter.Id}');return false;"/>
<apex:actionFunction name="SupressLetter" action="{!suppressLetter}" rerender="out">
<apex:param name="firstparam" assignto="{!selectedLetterID}" value="" />
</apex:actionFunction>
Then update your record based on the id that is now assigned to your new variable in your controller method.
Thanks, I'll try this.
Hi - Your solution works great, however...
From the time I click the checkbox, it takes 4 whole seconds for the check in the checkbox to appear.
This will cause users to think that their initial click didn't work, causing them to click several times. In essence, checking and unchecking the checkbox several times and not seeing the immediate result on the screen.
Any suggestions for reducing the lag?
Here is the method in the controller that handles the update:
This is likely to be the way visualforce works. The postback requires serialize/deserialize of the view state. You could try using the view state inspector to see if you can reduce the size.
It looks like you're doing SOQL, a FOR loop through the results, then a DML operation all in your onclick method. Is there anyway you can perform these operations at a different point in your flow? Performance is usually pretty good when you are just setting a parameter and re-rendering a section of the page. Another thought would be to use the ActionStatus tag to let users know you are working on something behind the scenes. 4 seconds is unacceptable.
You should have a list of records that were returned in your table. If the requirement is to update a record based on a checkbox click, then I would do an update on that record that looked something like this.
Letter__c letter = new Letter__c(id=SELECTEDID.....whatever other fields you want updated);
try{
update letter;
}
catch(exception e){
//handle your exception here.
}
If you need to know if a boolean is checked on that record, I would pull that in the original SOQL query for the list that is returned in your table. Put the results in a map that can be accessed using the id they select.
Hi - So, I added the query results to a map and then referenced the map when the checkbox is clicked. I am down to two seconds from four, but this is still noticeably slow.
Here is where I add the query results to the map:
Here is where I call the function from the page:
Any other suggestions? Thank you!
Actually, it's not any faster the new way. I was just counting slower :)
Do you have "Developer Mode" turned on for your user account? That slows down the page performance.
Hi - I do not have 'Developer Mode' turned on.
Hi - I am trying to implement the ActionStatus suggestion to no avail. I can't seem to get the action 'updating...' to appear on the page. Help?
Action Function:
Got this to work with some javascript... The page still takes 4 seconds to return, but at least now the user is aware that the page is doing something.