function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chidanand MChidanand M 

Display List based on the row index

Hi friends,

Is it possible to display a list based on the rowIndex.
Soql query.
sandeep sankhlasandeep sankhla
Hi chidu,

What exactly you want to do here ? what is your req?Yes it is possible..

Thanks,
Sandeep
Anupam RastogiAnupam Rastogi
Hi Chidu,

While querying using SOQL you can use 'Order By Id' Asc or Desc as per your requirement.

This will give you sorted records based on the row index.

Thanks
AR
Chidanand MChidanand M
Could u plz tell me how to do it. Very urgent.
I am creating a rows dynamically in vf page at the specified index.
Whenever i save and refresh the browser, the newly created row at the specified index is getting misplaced to other positin.
So, i wanna a display the list based on the rowIndex.

Here is my code. You can run on your system directly as it contains only standard objects and fields.
Plz get me the solution.
 
vf

<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:param value="{!rowNumber+1}" />
 </apex:column> 
 
 <apex:column headerValue="Phone" >
 <apex:inputField value="{!acc.Phone}"/>
 <apex:param value="{!rowNumber+1}" />
 </apex:column> 
 
 <apex:column headerValue="Email" >
 <apex:inputField value="{!acc.Email}"/>
 <apex:param value="{!rowNumber+1}" />
 </apex:column> 
 <apex:column headerValue="Action" >
 <apex:commandButton value="Delete" action="{!deleteRow}" reRender="pb">
 <apex:param name="rowIndex" value="{!rowNumber}"/>
 </apex:commandButton>
 <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>
Controller

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  ='00128000002s5b7';
 //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>();
 
 totalCount = attendeeList.size();
 
 system.debug(totalCount);
 
 if(totalCount==0){
 
 attendeeList.add(new Contact(AccountId = currentRecordId,LastName='Chidanand'));
 attendeeList.add(new Contact(AccountId = currentRecordId,LastName='Chetan'));
 attendeeList.add(new Contact(AccountId = currentRecordId,LastName='Robert'));
  
 }
 }
 
 
 
 public void addRow(){

rowIndex = Integer.valueOf(ApexPages.currentPage().getParameters().get('rowIndex'));

//attendeeList.add(rowIndex+1,new Contact(AccountId = '00128000002rnbM'));
if(rowindex+1 < attendeeList.size()){
      attendeeList.add(rowindex+1,new Contact(AccountId = currentRecordId));
     //attendeeList[rowindex + 1] = new Contact(AccountId = '00128000002rnbM');
     }
  else{
      attendeeList.add(new Contact(AccountId = currentRecordId));
      }


 }
 
 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);
 
 }
 }



 
Anupam RastogiAnupam Rastogi
Hi Chidu,

Below pasted is the modified code.

Some points that I observed in your code which were incorrect - 

1. <apex:param> is not supported within <apex:column>. Moreover there was no need to increment rowNumber after every column. It needs to be increased only once for a row.

2. There is no need to have the 'Add Attendee' button with all the rows. Only one button at the end is sufficient.

3. I would suggest that after saving the records you should navigate the user to the Account detail page instead of keeping on the same view.

I hope this helps.

Thanks
AR

If you found the reply useful then please mark it as best answer.


VF 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="No." style="width:20px; text-align:center;" headerClass="centertext">
                        <apex:outputText value="{0}" style="text-align:center;"> 
                            <apex:param value="{!rowNumber+1}" /> 
                        </apex:outputText>
                    </apex:column> 
                    <apex:column headerValue="First Name" >
                        <apex:inputField value="{!acc.FirstName}"/>
                    </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: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>


Controller Extension
public class addAttendee {
    public Account accounts;
    public Contact del;
    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();
        attendeeList = [Select id, firstName, LastName, Email, Phone from Contact where AccountId =: accounts.Id];
        totalCount = attendeeList.size();
        
        delattendeeList = new List<Contact>();
        delattendees = new List<Contact>();
    }
    
    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);
        
    }
}