1

row not delete when click on delete hyperlink what i'm doing wrong.

apex class

public class DeleteEditController{
public List<Stud__c> contacts{get;set;}
public String conId{get;set;}

public DeleteEditController(){
    contacts = new List<Stud__c>([Select Id, Name__c, Email_Address__c from Stud__c]);

}

public PageReference remove(){
    integer index = 0;
    for(Stud__c con : contacts){
        if(con.Id == conId){
            contacts.remove(index);
            break;
        }
        index ++ ;
    }
    return null;
  }
}

VFPAge

<apex:page controller="DeleteEditController">
<apex:form >
        <apex:actionFunction name="Remove" action="{!remove}" rendered="rm">
       <apex:param name="arg" value="" assignTo="{!conId}"/>
       </apex:actionFunction>
         <apex:pageBlock id="rm">
        <apex:pageblockTable value="{!contacts}" var="co">
          <apex:column headerValue="Delete Record">
          <a href="#" onClick="remove('{!co.Id}');">Delete</a></apex:column>
         <apex:column value="{!co.Name__c}"/>
         <apex:column value="{!co.Email_Address__c}"/> 
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:form>

1
  • does control goes to your method remove? Commented Mar 23, 2017 at 12:53

2 Answers 2

1

There are couple of things which are not handled correctly.

  1. Your actionFunction should have reRender attribute. You have used reRendered.

  2. Create List<Stud__c> using getter instead of defining it in Constructor.

  3. You are just removing item from list and not deleting it from back-end.

Your code should be:

<apex:actionFunction name="remove" action="{!remove}" reRender="dummy2" >
    <apex:param name="arg" value="" assignTo="{!conId}"/>
</apex:actionFunction>

<apex:outputPanel id="dummy2">
    <apex:pageBlock id="rm">
        <apex:pageblockTable value="{!contacts}" var="co">
            <apex:column headerValue="Delete Record">
            <apex:commandLink value="Delete" onClick="remove('{!co.Id}');" /></apex:column>
            <apex:column value="{!co.Name__c}"/>
            <apex:column value="{!co.Email_Address__c}"/> 
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:outputPanel>

And your Controller should be:

public List<Stud__c> contacts { get {
    return new List<Stud__c>([Select Id,Name__c, Email_Address__c from Stud__c]);
}set;}

public String conId{get;set;}
public void remove(){
    integer index = 0;

    Stud__c c = null;
    for(Stud__c con : contacts){
        if(con.Id == conId){
            c = contacts.get(index);
            contacts.remove(index);
            break;
        }
        index ++ ;
    }
    if (c!=null)
        delete c;
   // return null;
}
1

Try changing this piece of code

    <apex:pageblockTable value="{!contacts}" var="co">
          <apex:column headerValue="Delete Record">
          <a href="#" onClick="remove('{!co.Id}');">Delete</a></apex:column>
         <apex:column value="{!co.Name__c}"/>
         <apex:column value="{!co.Email_Address__c}"/> 
        </apex:pageblockTable>
    </apex:pageBlock>

into:

   <apex:pageblockTable value="{!contacts}" var="co">
          <apex:column headerValue="Delete Record">
          <a href="#" onClick="remove('{!co.Id}'); return false;">Delete</a></apex:column>
         <apex:column value="{!co.Name__c}"/>
         <apex:column value="{!co.Email_Address__c}"/> 
        </apex:pageblockTable>
    </apex:pageBlock>

There is a conflict in actionfunction event and command button action.

1
  • still it won't work cause OP has not used reRender attribute in actionFunction. Commented Mar 23, 2017 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .