+ Start a Discussion
Lorenz CortezLorenz Cortez 

Incorrect counter for deleting row

Hello Everyone,

I have this VF page that can add new rows of fields and delete a row of fields
User-added image

My problem is when I delete the first record or the top most record and then delete the second record it deletes the last row of records. After I deleted the first record it deletes the wrong rows of fields and then when I delete the first record then click the 'Add Skill' button it displays the wrong information in the row. Please Help me out. Thank you very much. My code is down below.

My VF page:

<apex:page standardController="Employee_Skills__c" extensions="AddContacts1" sidebar="false">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <apex:form >
    <apex:pageBlock title="Skill" id="pb">
      <apex:pageMessages />      
      <apex:variable var="rowNumber" value="{!0}"/>
        <apex:pageBlockTable id="thetable" title="Employee_Skills__c" var="empski" value="{!empskilist}">
          <apex:column headerValue="No." style="width:20px; text-align:center;" headerClass="centertext">
            <apex:outputText value="{0}" style="text-align:center;">
              <apex:param value="{!rowNumber+1}" />
          <apex:column headerValue="Skill Name" >
            <apex:inputField value="{!empski.Skill_ID__c}"/>
          <apex:column headerValue="Proficiency" >
            <apex:inputField value="{!empski.Proficiency__c}"/>
          <apex:column headerValue="Skill Type" >
            <apex:inputField value="{!empski.Skill_Type__c}"/>
          <apex:column headerValue="Years of Expirience" >
            <apex:inputField value="{!empski.Year_of_Experience__c }"/>
          <apex:column headerValue="Action" >
            <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb" immediate="true">
              <apex:param name="rowIndex" value="{!rowNumber}"/>
            <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
           <div align="center" style="margin-top:10px;">  
              <apex:commandButton action="{!addRow}" value="Add Skill" reRender="pb" immediate="true" />   
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!save}" />
        <apex:commandButton value="Cancel" action="{!cancel}"/>

My Controller:


public class AddContacts1 {
        public Employee_Skills__c empskill;
        public Employee_Skills__c del;
        public List < Employee_Skills__c > addEmpSkiList {get;set;}
        public List < Employee_Skills__c > delEmpSkiList {get;set;}
        public List < Employee_Skills__c > empskilist{get;set;}
        public Integer totalCount {get;set;}
        public Integer rowIndex {get;set;}
        public List < Employee_Skills__c > delEmpSki {get;set;}

        public AddContacts1(ApexPages.StandardController controller) {
                empskill= (Employee_Skills__c) controller.getRecord();
                empskilist= [SELECT Id,Skill_ID__r.Name,Employee_ID__c,Name,Proficiency__c,Skill_Type__c,Year_of_Experience__c FROM Employee_Skills__c WHERE Employee_ID__c =: empskill.Employee_ID__c];
                totalCount = empskilist.size();
                delEmpSkiList = new List < Employee_Skills__c > ();
                delEmpSki = new List < Employee_Skills__c > ();

        public void addRow() {
                addEmpSkiList = new List < Employee_Skills__c > ();
                empskilist.add(new Employee_Skills__c (Id = empskill.Id,Employee_ID__c = empskill.Employee_ID__c));

        public PageReference save() {
                upsert empskilist;
                delete delEmpSkiList ;
              return null;
        public void deleteRow() {
                rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
                del = empskilist.remove(rowIndex);
               // if(del.Id != NULL)
               // {
              //  }

Thank you very much.
Flint LockwoodFlint Lockwood
This is because you are not readjusting the index of the list. After you delete the first item, the second item becomes "index 0". Hence, when you try to delete the second item, which would've been index 1, you are in fact deleting the third item. 
Flint LockwoodFlint Lockwood
I would try rowIndex =Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex')) - 1; 1 being the number of items you have deleted. So you deleted two items, it would be -2.
Lorenz CortezLorenz Cortez

Hi @Flint Lockwood,

I try to put the -1 on rowIndex =Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex')) and It doesnt work and I have this error when I delete the first record.

System.ListException: List index out of bounds: -1
Error is in expression '{!deleteRow}' in component <apex:commandButton> in page testsample: Class.AddContacts1.deleteRow: line 32, column 1

Do you have another solution to this? Thank you very much


Flint LockwoodFlint Lockwood
Assuming that the Skill Name is unique, you can send the Skill Name as your parameter instead. That will be a lot easier than readjusting the index. 

so instead of 
<apex:param name="rowIndex" value="{!rowNumber}"/>

<apex:param name="skillname" value="{!empski.Skill_ID__c}"/>

In your controller

String skillname = String.valueOf(ApexPages.currentPage().getParameters().get('skillname'));

for(Employee_Skills__c es: empskill){
if(es.Skill_Id__c == skillname){

//perform your dml