如何通过命令按钮将字段值传递给apex类?

时间:2016-10-18 03:28:43

标签: salesforce visualforce apex

我需要将id字段值传递给apex类,以使用命令按钮删除该行。

类别:

public class search_delete
{
public string id {get;set;}
public list<account> acc{get;set;}
public search_delete()
{
    acc = new list<account>();        
    acc = [SELECT id,name,phone,industry from account];

}
public void delete_record()
{

acc = [SELECT name,phone,industry from account where id = :id];

delete acc;
}
}

VP:

<apex:page controller="search_delete" >
 <apex:form >
     <apex:pageBlock >
         <apex:pageBlockTable value="{!acc}" var="a">
         <apex:column value="{!a.Name}"/>
         <apex:column value="{!a.Phone}"/>
         <apex:column value="{!a.Industry}"/>         
         <apex:column >
             <apex:commandButton value="Delete" Action="{!delete_record}">
                 <apex:param name="accId" value="{!a.id}"/>
             </apex:commandButton>
         </apex:column>
         </apex:pageBlockTable>
     </apex:pageBlock>     
 </apex:form> 
</apex:page>

现在我需要通过点击删除按钮从vp页面将id值传递给apex类。 My Output

1 个答案:

答案 0 :(得分:1)

使用apex命令按钮的assignto属性。

<apex:page controller="search_delete" >
<apex:form >
 <apex:pageBlock >
     <apex:pageBlockTable value="{!acc}" var="a">
     <apex:column value="{!a.Name}"/>
     <apex:column value="{!a.Phone}"/>
     <apex:column value="{!a.Industry}"/>         
     <apex:column >
         <apex:commandButton value="Delete" Action="{!delete_record}">
            <apex:param name="accId" value="{!a.id}" assignto="{!idchosen}/>
         </apex:commandButton>
     </apex:column>
     </apex:pageBlockTable>
 </apex:pageBlock>     
</apex:form> 

public class search_delete
{
public string id {get;set;}
public string idchosen {get;set;}
public list<account> acc{get;set;}
public search_delete()
{
  acc = new list<account>();        
 acc = [SELECT id,name,phone,industry from account];

}
public void delete_record()
{

acc = [SELECT name,phone,industry from account where id = :idchosen];

delete acc;
}
}