+ Start a Discussion
Chidanand MChidanand M 

How to set the row index of the dynamically created ROW ( add another row just below the selected add button))

Hai folks,

I have a table with 7 rows.
The last column of each row has action buttons [add] and [delete]
Now when i click on add button of the first row, new row should get created below it. Now with the following code, new row is getting created at the end of all 7 row's. i.e. it is getting created as 8th row.
Any workaround??
 
Visualforce Page

<apex:page standardController="Account" extensions="addAttendee" sidebar="false">
 <apex:form >
 <apex:pageBlock title="Accounts" id="pb">

 <apex:pageMessages />
 <apex:variable var="rowNumber" value="{!0}"/>
 <apex:pageblockSection columns="1">
 
 <apex:pageBlockTable title="Contacts" var="acc" value="{!attendeeList}"> 
 
 <apex:column headerValue="First Name" >
 <apex:inputField value="{!acc.FirstName}"/>
  <apex:param value="{!rowNumber+1}" /> 
 </apex:column> 
 <apex:column headerValue="Last Name" >
 <apex:inputField value="{!acc.LastName}"/>
 </apex:column> 
 <apex:column headerValue="Phone" >
 <apex:inputField value="{!acc.Phone}"/>
 </apex:column> 
 <apex:column headerValue="Email" >
 <apex:inputField value="{!acc.Email}"/>
 </apex:column> 
 <apex:column headerValue="Action" >
 <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb">
 <apex:commandButton action="{!addRow}" value="Add Attendee" reRender="pb"/>
 <apex:param name="rowIndex" value="{!rowNumber}"/>
 </apex:commandButton>
 <apex:variable var="rowNumber" value="{!rowNumber+1}"/>
 </apex:column> 
   
 </apex:pageBlockTable>
 
 <apex:commandButton action="{!addRow}" value="Add Attendee" reRender="pb"/>
 </apex:pageblockSection>
 <apex:pageBlockButtons >
 <apex:commandButton value="Save" action="{!ave}" />
 <apex:commandButton value="Cancel" action="{!cancel}"/>
 </apex:pageBlockButtons>
 
 </apex:pageBlock>
 
 </apex:form> 
 </apex:page>
Apex Code

public class addAttendee {
public Account accounts;
public Contact del;
public String currentRecordId {get;set;}
public List<Contact> addattendeeList {get;set;}
public List<Contact> delattendeeList {get;set;}
public List<Contact> attendeeList {get;set;}
public Integer totalCount {get;set;}
public Integer rowIndex {get;set;}
public List<Contact> delAttendees {get; set;} 
 
 
 public addAttendee(ApexPages.StandardController controller) {
 accounts = (Account)controller.getRecord();
 currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
 //attendeeList = [Select id, firstName, LastName, Email, Phone from Contact where AccountId =: accounts.Id];
 attendeeList = [Select id, firstName, LastName, Email, Phone from Contact where AccountId =: currentRecordId];
 totalCount = attendeeList.size();
 delattendeeList = new List<Contact>();
 delattendees = new List<Contact>();
 attendeeList.add(new Contact(AccountId = accounts.Id));
 }
 
 
 
 public void addRow(){
 //addattendeeList = new List<Contact>();
 attendeeList.add(new Contact(AccountId = accounts.Id));
 }
 
 public PageReference ave(){
 
 upsert attendeeList;
 delete delattendeeList;
 return (new ApexPages.StandardController(accounts)).view();
 } 
 
 public void deleteRow(){
 
 rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
 System.debug('rowbe deleted ' + rowIndex );
 System.debug('rowm to be deleted '+attendeeList[rowIndex]);
 del = attendeeList.remove(rowIndex);
 delattendeeList.add(del);
 
 }
 }



 
Best Answer chosen by Chidanand M
Pankaj_GanwaniPankaj_Ganwani
Hi Chidu,

You can add one row by passing rowindex from page to contoller and use below mentioned code:

public void addRow()
{
  if(rowindex+1 < attendeeList.size())
     attendeeList[rowindex + 1] = new Contact(AccountId = accounts.Id);
  else
      attendeeList.add(new Conact(AccountId = accounts.Id));
  }

All Answers

Prady01Prady01
Hi Chidu, You can achieve this using wrapper class inside apex. In the wrapper class you can add a new entity to the wrapper list this is adding a new row or remove the entity from the wrapper list this is deleting the row.

Thanks
Prady
Chidanand MChidanand M
hi bro..

My code already does the action of add and delete. My requirement is,
When i click on the add button of the first row, Now row should be added in the secode row.
That is new row should have the index value 1.
Pankaj_GanwaniPankaj_Ganwani
Hi Chidu,

You can add one row by passing rowindex from page to contoller and use below mentioned code:

public void addRow()
{
  if(rowindex+1 < attendeeList.size())
     attendeeList[rowindex + 1] = new Contact(AccountId = accounts.Id);
  else
      attendeeList.add(new Conact(AccountId = accounts.Id));
  }
This was selected as the best answer
Chidanand MChidanand M
@Pankaj

When i add the following code, it is giving me the NULL Pointer Exception

EXCEPTION
System.NullPointerException: Argument cannot be null.
Error is in expression '{!addRow}' in component <apex:commandButton> in page addrowsdynamicallynew: Class.workestimationDetailsNew.addRow: line 55, column 1
Class.workestimationDetailsNew.addRow: line 55, column 1
 
public void addRow(){
 //addattendeeList = new List<Contact>();
// attendeeList.add(new Contact(AccountId = '00128000002rnbM'));
rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));
if(rowindex+1 < attendeeList.size())
     attendeeList[rowindex + 1] = new Contact(AccountId = '00128000002rnbM');
  else
      attendeeList.add(new Contact(AccountId = '00128000002rnbM'));

 }




 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you place assignTo attribute at line no.29 in vf page:
<apex:param name="rowIndex" value="{!rowNumber}" assignTo="{!rowIndex}"/>

Then use this value in function.
Chidanand MChidanand M
Well done!! Thanks. @pankaj