0

How to reset/order of the index after user deletes the row?

In the screen shot, I have 4 rows 1,2,3,4 and I deleted row #3 and when I try to click on Add More Row then instead of getting row number 3 I got the row number 4

enter image description here Visualforce page:

<apex:outputPanel id="accountHead">
  <apex:variable value="{!0}" var="rowNum"/>  
   <apex:pageBlockSection columns="1" title="Adding Multiple Accounts" id="thePbs" collapsible="False"> 

     <apex:pageBlockTable value="{!waAccList}" var="eachRecord"> 

      <apex:column headerValue="Action">
        <apex:commandLink value="Remove" style="color:red" action="{!removeRowFromAccList}" rendered="{!rowNum > 0}" rerender="accountHead" immediate="true" >
             <apex:param value="{!rowNum}" name="rowToRemove" assignTo="{!rowToRemove}"/>
         </apex:commandLink>
         <apex:variable var="rowNum" value="{!rowNum + 1}"/>
      </apex:column>

        <apex:column headerValue="Row" rendered="true" headerClass="colHeadr">
            <div class="colHeadr"><apex:outputText value="{!eachRecord.index +1}"/>   </div>               
        </apex:column>                

      <apex:column headerValue="Account Name">
           <apex:inputText value="{!eachRecord.record.selectedDay}"   required="false"/>
       </apex:column> 

    </apex:pageBlockTable>
   </apex:pageBlockSection>
   <apex:commandButton value="Add More" action="{!addNewRowToAccList}" rerender="accountHead" Status="status"   />

  </apex:outputPanel>

Controller:

public Integer rowToRemove {get;set;}

public void removeRowFromAccList()
    {
        waAccList = removeRowToAccountList(rowToRemove, waAccList);    
    }

    public void addNewRowToAccList()
    {
        waAccList = addNewRowToAccList(waAccList);
        system.debug('waAccList >>>' + waAccList );
    } 

public List<WrapperpaAccountList> addNewRowToAccList(List<WrapperpaAccountList> waAccObjList)
    {
        WrapperpaAccountList newRecord = new WrapperpaAccountList();
        mycustomClass newAccountRecord = new mycustomClass();        
        newRecord.record = newAccountRecord;
        newRecord.index = waAccObjList.size(); //<<<<<reset order??
        waAccObjList.add(newRecord); 
        return waAccObjList;
    }
  • the general solution to this issue is the delete action method needs to run through all wrapper class instances using a for loop and tell the wrapper class to change the index value based on the for loop iteration value. Each wrapper class instance can not recalculate its own index because each instance doesn't know anything about the list overall – cropredy Jun 7 '16 at 17:24
  • look at this post by jeff, he has explained an alternative fix to handle using counter variable in a wrapper class:blog.jeffdouglas.com/2010/04/02/… – Rao Jun 7 '16 at 17:27
1

If you just want to display row number then you can easily do this

 <apex:column headerValue="Row" rendered="true" headerClass="colHeadr">
       <div class="colHeadr"><apex:outputText value="{!rowNum}"/>   </div>               
  </apex:column> 

because in the index you are storing list size which is 3 but when you increment it it will give you 4 again. If you want to use controller variable then you need to refresh your complete list and then populate it again.

  • thats correct and i'm aware of that i'm storing the list size but how would you deal with delete action if i go with your route? so i'm deleting the row based on the row number – Nick Kahn Jun 7 '16 at 17:14
  • @NickKahn yes you are . But if you use my solution still it will work for you. Just give it a try. second point is you are giving list size to index number which is wrong and will not work in bigger picture. – Tushar Sharma Jun 7 '16 at 17:16
  • let me try and tweak my code so i should be removing this line then newRecord.index = waAccObjList.size(); correct? – Nick Kahn Jun 7 '16 at 17:17
  • @NickKahn Yes comment this and then test. – Tushar Sharma Jun 7 '16 at 17:19
  • okay I have tested and I see there is a issue with that approach, so i have added 1,2,3,4,5 rows and I delete row 3 but instead of deleting row 3 its deleting the row 5, meaning that its deleting the last row all the time – Nick Kahn Jun 7 '16 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.