function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wkuehlerwkuehler 

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.

 

 

sinsiterrulezsinsiterrulez

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.

 

<apex:param name="testuser" assignto="{!variable1}"  value="{!tu.UserName}"/>

Please let me know if it works

 

 

prageethprageeth

Hello 

 

<apex:commandButton action="{!TestParams}" value="TestParam">
    <input type="hidden" name="testuser" value="{!tu.UserName}"/>
</apex:commandButton>

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).

 

<apex:commandLink styleClass="btn" style="padding:5px;text-decoration:none;" action="{!TestParams}" value="TestParam">
    <apex:param assignTo="{!myId}" name="testuser" value="{!tu.id}"/>
</apex:commandLink>

 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.

 

Pradeep_NavatarPradeep_Navatar

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.