You need to sign in to do that
Don't have an account?

Send ID to controller for single row of dataTable
Having some issues figuring this one out. This is what i need:
- Display a table, reach row displaying id, username, and a command button
- When the command button is clicked, take the ID of the record on that row and send it to a method on the controller where it will be output using a system.debug.
Visualforce
<apex:page controller="vftest"> <apex:form > <apex:pageBlock > <apex:dataTable value="{!TestUsers}" var="tu"> <apex:column headerValue="Id"> <apex:inputField value="{!tu.Id}"/> </apex:column> <apex:column headerValue="UserName"> <apex:inputField value="{!tu.UserName}"/> </apex:column> <apex:column> <apex:commandButton action="{!TestParams}" value="TestParam"> <apex:param name="testuser" value="{!tu.UserName}"/> </apex:commandButton> </apex:column> </apex:dataTable> </apex:pageBlock> </apex:form> </apex:page>
Class
public class vftest { string selected_user; public List<User> getTestUsers() { List<User> testuserlist = [select id, username from user where user_type__c = 'Test']; return testuserlist; } public void TestParams() { Id id1 = ApexPages.currentPage().getParameters().get('testuser'); system.debug('-----------------tp-id1-------------------' + id1); Id id2 = System.currentPageReference().getParameters().get('testuser'); system.debug('-----------------tp-id2-------------------' + id2); } }
Any help would be appreciated.
Hi,
You can modify your existing code (param tag)
Define a string 'variable1' & assign the value using assignto attribute in param & access it in your action method for the button.
Please let me know if it works
Hello wkuehler;
As I think it is a SalesForce bug that <apex:param> does not work with apex:commandButton. But in their docs they have said that it is working.
There are alternative ways that you can overcome this issue. One way is using a HTML input hidden tag instead of your apex:param tag as below.
Another way is to use a commandLink instead of the commandButton.
However if you want to show the link as a button you can change the style class of the commandLink as below(not recommended).
However the style class name 'btn' is from Salesforce style sheet. So if Salesforce changed the name of the style class you would face unexpected issues.
Find below a sample refrence code :
VF CODE:
<apex:page controller="tagAdminDetailController" standardStylesheets="false">
<apex:stylesheet value="{!($Resource.AccountStyle)}"/>
<apex:pageBlock title="Tag detail information">
<apex:form >
<apex:pageBlockTable value="{!tagchild}" var="c">
<apex:column headerValue="Action" >
<apex:commandLink value="Delete" action="{!DelRecord}">
<apex:param name="recidval" value="{!c.idval}" assignTo="{!strID}" />
</apex:commandLink>
</apex:column>
<apex:column headerValue="address" value="{!c.taddress}" styleClass="centertext">
</apex:column>
<apex:column headerValue="name" value="{!c.tname}" />
</apex:pageBlockTable>
</apex:form>
</apex:pageBlock>
</apex:page>
Controller code:
public class tagAdminDetailController {
Public List<Employee__c> tags {get;set;}
Public List<tagChild> tagchild {get;set;}
Public string strID{get;set;}
public tagAdminDetailController()
{
tags = [Select Name, Id,Address__c From Employee__c];
createlist(tags);
}
public void DelRecord()
{
strID = Apexpages.currentpage().getParameters().get('recidval');
System.Debug('The ID =>> ' + strID);
Employee__c tagdel = [Select Name, Id From Employee__c where id =: strID ];
delete tagdel;
}
public void createlist( List<Employee__c> tags2)
{
tagchild = new List<tagChild>();
for ( Employee__c tg : tags2)
{
tagchild tc = new tagchild();
tc.taddress= tg.Address__c;
tc.tname=tg.name;
tc.idval=tg.id;
tagchild .add(tc);
}
}
public class tagChild
{
Public String taddress{get; set;}
Public String tname{get; set;}
Public String idval{get; set;}
}
}
Hope this helps.