1

I have created a table with edit, create and delete options of record. But I have a issue in deleting operation. When am trying to delete the last row of table, all the rows are deleted except the first row. Please provide the solution ?? how can I delete the particular selected record ??

Apex class

public class addRowRemoveRow{

    public List<Account> allAccs {get; set;}
    public string SelectedAccountId {get; set;}
    public static Integer toDelIdent {get; set;}


    public addRowRemoveRow(){
        allAccs = new List<Account>();
        Account acc1 = new Account();
        allAccs.add(acc1);
    }

    public void addRowMethod(){
        Account acc1 = new Account();
        allAccs.add(acc1);
    }

    public void removeRowMethod(){
        integer count = allAccs.size();
        system.debug('size----->'+count);
        allAccs.remove(count--);
    }

    public void saveMethod(){
        insert allAccs;
        clearMethod();
    }



    public void clearMethod(){
        integer finalCount = allAccs.size();
        allAccs.clear();
        for(integer i=0;i<finalCount;i++){
            Account acc1 = new Account();
            allAccs.add(acc1);

        }

    }


}

VF page

<apex:page controller="addRowRemoveRow">
  <apex:form >
      <apex:pageBlock id="anyName">
          <apex:dataTable value="{!allAccs}" var="Accvariable" cellpadding="4" border="1">
          <apex:column headerValue="Action">
              <apex:outputLink >Edit</apex:outputLink>&nbsp;|&nbsp;
             <apex:outputLink >Del</apex:outputLink>

              </apex:column>
              <apex:column headerValue="Model">
                  <apex:inputtext value="{!Accvariable.Name}"/>
              </apex:column>
              <apex:column headerValue="Version">
                  <apex:inputtext value="{!Accvariable.Phone}"/>
              </apex:column>
              <apex:column headerValue="Discount">
                  <apex:inputtext value="{!Accvariable.website}"/>
              </apex:column>
         </apex:dataTable>
          <apex:pageBlockButtons location="TOP">
          <apex:commandButton value="addRow" action="{!addRowMethod}" rerender="anyName" />
         <!-- <apex:commandButton value="removeRow" action="{!removeRowMethod}"/>-->

              <apex:commandButton value="save" action="{!saveMethod}"/>
<!--          <apex:commandButton value="Clear" action="{!clearMethod}" reRender="anyName" />-->
          </apex:pageBlockButtons>
      </apex:pageBlock>

  </apex:form>
</apex:page>
5
  • I don't see it currently in your visualforce page, but is it safe to assume that you are encountering this problem when using the addRowRemoveRow() method?
    – martin
    Commented Aug 3, 2015 at 5:32
  • Agree with Martin. This could also be a rerender issue. Could you please update the code snippet at the time you had the issue?
    – Ranga
    Commented Aug 3, 2015 at 5:41
  • In removeRowMethod() am encountering a problem. how can I change that method to delete a particular row which am trying to delete ?
    – prabu
    Commented Aug 3, 2015 at 5:52
  • <apex:pageBlock id="anyName"><apex:dataTable value="{!allAccs}" var="Accvariable" cellpadding="4" border="1"> <apex:column headerValue="Action"> <apex:outputLink >Edit</apex:outputLink>&nbsp;|&nbsp; <apex:outputLink >Del</apex:outputLink> </apex:column><apex:column headerValue="Model"> <apex:inputtext value="{!Accvariable.Name}"/></apex:column> .. .</apex:dataTable><apex:pageBlockButtons location="TOP"> <apex:commandButton value="addRow" action="{!addRowMethod}"rerender="anyName" /><apex:commandButton value="save" action="{!saveMethod}"/></apex:pageBlockButtons> </apex:pageBlock>
    – prabu
    Commented Aug 3, 2015 at 6:07
  • code is too long..i shorted it
    – prabu
    Commented Aug 3, 2015 at 6:07

1 Answer 1

1

For delete the selected row you can use wrapper class. It is more better approach. There is a sample given in this link you can check here.

if you want to delete permanently on edit click then you can use map in controller, pass record id using param in the controller and then delete the record using DML. You can take help from here

Hope it will solve your problem.

5
  • without selecting the checkbox by clicking the "edit" output link of the row I want to delete the record.
    – prabu
    Commented Aug 3, 2015 at 6:57
  • @prabu permanent delete the record or just want to remove from the list. Commented Aug 3, 2015 at 7:14
  • In both case you can use map, pass selected record id using param in controller and then remove the row from map or delete the record using id. Commented Aug 3, 2015 at 7:16
  • want to delete the record permanently..!!
    – prabu
    Commented Aug 4, 2015 at 3:55
  • then you can use map , pass record id using param and then delete the record using DML. You can take help from here Commented Aug 4, 2015 at 5:04

You must log in to answer this question.

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