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
caterina ferrigno 8caterina ferrigno 8 

Add row under a specific row in a Visualforce Page

Hello!
I have the button "add" (symbol +) on each row of table .When i click on the add button a new row should inserted just below that row.
For example if clicked the button '+' on row 1 then new row should be add below row 1, but in my case i have this row at the end of the table.
This is my apex class:
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
           
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
               
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
        QuoteLineItem qli1=new  QuoteLineItem();    
        List <QuoteLineItem> newqli = new List <QuoteLineItem>();    
        newqli = [SELECT Id, Desrizione__c, N_Ordine_Cliente__c FROM QuoteLineItem];
        qli.add(quotelineitem);       
        newqli.add(quotelineitem);
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >

    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" >
                                                                                                   
    <apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton value="+" action="{!AddRow}" />
                                                                                             
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" />
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

This is the result when i click on button '+' on the first row :
User-added image
I want a new row under the first row!
How can I solve this problem?

Thanks.
Best Answer chosen by caterina ferrigno 8
Nayana KNayana K
There was syntax error in code....I tried in my dev org with simple code and it worked. So I have modfifed your code accordingly . Please try below code :
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
         selectedIndex = 0;      
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >
    <Apex:actionFunction name="addRow" action="{!AddRow}" rerender="pbs" status="eventStatus">
        <Apex:param name ="selectedIndex" value="" assignTo="{!selectedIndex}"/>
    </apex:actionfunction>
	
	<apex:actionStatus id="eventStatus"  >
         <apex:facet name="start">
          <apex:outputPanel >
            <div id="loadingComponent" 
                 style="width:100%; height:100%; position:absolute; top:0; 
                        left:0; background-color: black; opacity: 0.4; z-index: 999;">
                <div style="position:absolute;top:50%;left:50%">
                    <apex:image value="/img/loading32.gif"></apex:image> 
                </div>
            </div>
           </apex:outputPanel>
        </apex:facet>   
    </apex:actionStatus>
	
    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
		<apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton  value="+" onclick="addRow('{!i}');" rerender="dummy"/>
                                                                                            
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column headervalue="Descrizione riga offerta">
					{!roff.Description}
						<apex:variable var="i" value="{!i+1}"/>
					</apex:column>
                    
		</apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

 

All Answers

Nayana KNayana K
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
               
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >

    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
    <apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandLink styleClass="btn" value="+" action="{!AddRow}" rerender="pbs" >
                        <apex:param name="selectedIndex" value="{!i}" assignedTo="{!selectedIndex}"/>                                                                     
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" />
                    <apex:variable var="i" value="{!i+1}"/>
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>


Not sure how helpull it is, but you can give a try.
caterina ferrigno 8caterina ferrigno 8
Hi Nayana ,
When i add the following code i have a visualforce error :

"Attempt to de-reference a null object
L'errore è nell'espressione "{!AddRow}" nel componente page creanuovafattura: Class.CreaNewFatturaCtrl.AddRow: line 38, column 1"

This is line 38 column 1 in the code : " qli.add(selectedIndex+1, quotelineitem);  "

What can i do?
Thanks a lot
 
Nayana KNayana K
Ok can you turn on debug log and check what the value you are getting for :
system.debug('====selectedIndex===='+selectedIndex);

It seems selectedIndex is returning null.
Nayana KNayana K
<apex:commandLink styleClass="btn" value="+" action="{!AddRow}" rerender="pbs" >
                        <apex:param name="selectedIndex" value="{!i}" assignTo="{!selectedIndex}"/>                                                                     
                    </apex:column> 
Nayana KNayana K
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
         selectedIndex = 0;      
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >
    <Apex:actionFunction name="addRow" action="{!AddRow}" rerender="pbs">
        <Apex:param name ="selectedIndex" value="" assignTo="{!selectedIndex}"/>
    </apex:actionfunction>
    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
    <apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton  value="+" onclick="addRow('{!i}');"/>
                                                                                            
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" />
                    <apex:variable var="i" value="{!i+1}"/>
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>


if above code dint work just change one line and try again :
<apex:commandButton  value="+" onclick="addRow('{!i}');"  rerender="dummy"/>
caterina ferrigno 8caterina ferrigno 8
Ok Nayana , now there is another problem: whatever it is the row in which I click the add button '+' , the new row is added always at the second row.
For example, if i click '+' on the first row, the new row is on the second row. But also if i click '+' on the second, third row etc, the new row is always on the second row.
Nayana KNayana K
Turn on debug log and click on + button on 2rd row and check what value is coming in debug for selectedIndex...is it still 0? Check for 4th row - still 0? Then it means value is not assigning properly on click of commandbutton. If this is the case, let's try to find root cause one by one.

Modify the action function to keep some alerts to check if value is set in page level properly or not...
 
<Apex:actionFunction name="addRow" action="{!AddRow}" onsubmit="alert('{!i}');" oncomplete="alert('{!i}');"  rerender="pbs">
        <Apex:param name ="selectedIndex" value="" assignTo="{!selectedIndex}"/>
    </apex:actionfunction>

When you click 1st row i has to be 0, on click of second row it has to be 1, on click of 3rd it has to be 2 and so on.

To investigate more :
 
<apex:pageblocktable value="{!qli}" var="roff">
    <Apex:column value ="{!i}"/>
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton  value="+" onclick="addRow('{!i}');"/>
                                                                                            
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" />
                    <apex:variable var="i" value="{!i+1}"/>
    </apex:pageblocktable>


As you can see, I have added one more column to display i value for debugging purpose...so each row should contain ith value increasing by 1 starting from 0.

Let me know what you are getting
 
caterina ferrigno 8caterina ferrigno 8
Sorry Nayana but 'onsubmit' attribute is non supported  in <Apex:actionFunction>.
Nayana KNayana K
Remove onsubmit and keep only oncomplete...check the outcome....have u checked debug log with old code which iIhave given?
caterina ferrigno 8caterina ferrigno 8
If i kepp only 'oncomplete' i have this error :
Error: unknown property 'Fattura__cStandardController.i'

Anyway, i have checked debug log with old code and i have this :
USER_DEBUG [24]|DEBUG|selectedIndex0.
 
Nayana KNayana K
Oh.....then remove oncomplete also....
 
<apex:commandButton  value="+" onclick="alert('{!i}');addRow('{!i}');"/>

modify commandbutton and keep thos modifications of pageblock table and let me know the outcome of alert when u ckick each row
caterina ferrigno 8caterina ferrigno 8
Hi Nayana! I have done the modifications and the outcome of alert for each row is this :
c.cs83.visual.force.come says :
Nayana KNayana K
According to http://www.infallibletechie.com/2012/11/how-to-increment-apexvariable.html post , we have to include apex:variable inside apex:column inorder to increment it....

So try below code :
#1 :
 
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
               
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >

    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
    <apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandLink styleClass="btn" value="+" action="{!AddRow}" rerender="pbs" >
                        <apex:param name="selectedIndex" value="{!i}" assignTo="{!selectedIndex}"/>                                                                     
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" >
						<apex:variable var="i" value="{!i+1}"/>
					</apex:column>
                    
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

#2 if above dint work :
 
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
         selectedIndex = 0;      
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >
    <Apex:actionFunction name="addRow" action="{!AddRow}" rerender="pbs">
        <Apex:param name ="selectedIndex" value="" assignTo="{!selectedIndex}"/>
    </apex:actionfunction>
    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
    <apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton  value="+" onclick="addRow('{!i}');"/>
                                                                                            
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta">
						<apex:variable var="i" value="{!i+1}"/>
					</apex:column>
                    
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

if #2 din't work, then just modify #2's one line and try again :
<apex:commandButton  value="+" onclick="addRow('{!i}');"  rerender="dummy"/>

 
caterina ferrigno 8caterina ferrigno 8
I'm sorry Nayana , but the problem is still there :(
Thanks anyway!!
Nayana KNayana K
Can you tell me one thing?  After keeping below code what the first column in the table is showing? Is i is in increasing order from 0?
 
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
               
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >

    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
    <apex:pageblocktable value="{!qli}" var="roff">
                     <apex:column value="{!i}">
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandLink styleClass="btn" value="+" action="{!AddRow}" rerender="pbs" >
                        <apex:param name="selectedIndex" value="{!i}" assignTo="{!selectedIndex}"/>                                                                     
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column value="{!roff.Description}" headervalue="Descrizione riga offerta" >
						<apex:variable var="i" value="{!i+1}"/>
					</apex:column>
                    
    </apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

 
Nayana KNayana K
Meanwhile il try to do same thing with simple code in my dev org..
Nayana KNayana K
There was syntax error in code....I tried in my dev org with simple code and it worked. So I have modfifed your code accordingly . Please try below code :
public class CreaNewFatturaCtrl { 

      public Fattura__c fattura {get;set;}    
      private String idQuote;     
      public Quote prev {get;set;}   
      public List <QuoteLineItem> qli {get;set;}
      public Integer selectedIndex   {get;set;}
      Public QuoteLineItem quotelineitem{get;set;}
          
                                                             
     public CreaNewFatturaCTRL(ApexPages.StandardController stdcontroller) {
         selectedIndex = 0;      
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');
        this.fattura=(Fattura__c) stdController.getRecord();
        fattura.preventivo__c = idQuote;
               
        qli = [SELECT Id, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_da_fatturare__c , Totale__c, Description FROM QuoteLineItem 
                WHERE Quote.Id = :idQuote]; 
     
        
        }
        
        
        
      public ApexPages.PageReference AddRow () {
                          
        QuoteLineItem quotelineitem = new quotelineitem();
          
         
        system.debug('====selectedIndex===='+selectedIndex);
        if(selectedIndex == qli.size()-1)
        {
            qli.add(quotelineitem);     
        }
        else
        {
            qli.add(selectedIndex+1, quotelineitem);     
        }
          
        
        return null; 
     }
         
                       
    public ApexPages.PageReference saveRecord() {
     insert fattura;
     return new PageReference('/'+fattura.id);
    }
    
    public ApexPages.PageReference cancelRecord() {
     return new PageReference('/'+idQuote);
    }
}

And this is my VF page:

<apex:page StandardController="Fattura__c" extensions="CreaNewFatturaCtrl" showHeader="false">
<apex:pageBlock title="Nuova fattura">

    

<apex:form >
    <Apex:actionFunction name="addRow" action="{!AddRow}" rerender="pbs" status="eventStatus">
        <Apex:param name ="selectedIndex" value="" assignTo="{!selectedIndex}"/>
    </apex:actionfunction>
	
	<apex:actionStatus id="eventStatus"  >
         <apex:facet name="start">
          <apex:outputPanel >
            <div id="loadingComponent" 
                 style="width:100%; height:100%; position:absolute; top:0; 
                        left:0; background-color: black; opacity: 0.4; z-index: 999;">
                <div style="position:absolute;top:50%;left:50%">
                    <apex:image value="/img/loading32.gif"></apex:image> 
                </div>
            </div>
           </apex:outputPanel>
        </apex:facet>   
    </apex:actionStatus>
	
    <apex:pageBlock >  

    <apex:pageBlockButtons location="Both">
                <apex:commandButton value="Salva" action="{!saveRecord}"/>
                <apex:commandButton value="Annulla" action="{!cancelRecord}"/>                  
    </apex:pageBlockButtons>
     
  
                 
    <apex:pageBlockSection title="Informazioni" collapsible="FALSE">
  
                <apex:inputField value="{!Fattura__c.Numero_fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Nome_Fattura__c}"/>
                <apex:inputField value="{!Fattura__c.Preventivo__c}" />
                <apex:inputField value="{!Fattura__c.Motivo_stato__c}"/>  
                <apex:inputField value="{!Fattura__c.Banca__c }"/>
                <apex:inputField value="{!Fattura__c.ID_Fattura_SFDC__c}"/>
                <apex:inputField value="{!Fattura__c.Cliente_di_fatturazione__c }"/>
                                                                                                               
                </apex:pageblockSection>
                    
   <apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >
   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }"                          
              </apex:pageblockSection> 
      
    <apex:pageBlockSection title="Amministrazione" collapsible="FALSE" >
    
                <apex:inputField value="{!Fattura__c.Data_emissione__c}"/>
                <apex:inputField value="{!Fattura__c.Data_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Data_scadenza__c}"/>                                   
                <apex:inputField value="{!Fattura__c.Scadenza__c}"/>
                <apex:inputField value="{!Fattura__c.Scadenza_pagam__c}"/>
                <apex:inputField value="{!Fattura__c.Data_annull__c}"/>
                <apex:inputField value="{!Fattura__c.Data_revisione__c}"/>
          </apex:pageblockSection>
 
  <apex:pageBlockSection columns="1"  title="Righe offerta" collapsible="FALSE" id="pbs">
     <apex:variable var="i" value="{!0}"/>                                                                                             
		<apex:pageblocktable value="{!qli}" var="roff">
    
                    <apex:column headervalue="Aggiungi">
                         
                    <apex:commandButton  value="+" onclick="addRow('{!i}');" rerender="dummy"/>
                                                                                            
                    </apex:column> 
                                 
                    
                    <apex:column value="{!roff.ListPrice}" headervalue="Prezzo di listino" />
                    <apex:column value="{!roff.UnitPrice}" headervalue="Prezzo di vendita" />
                    <apex:column value="{!roff.Quantity}" headervalue="Quantità" />
                    <apex:column value="{!roff.Importo_IVA__c}" headervalue="Importo IVA" />
                    <apex:column value="{!roff.Totale__c}" headervalue="Totale" />
                    <apex:column value="{!roff.Importo_ancora_da_fatturare__c}" headervalue="Importo ancora da fatturare" />
                    <apex:column headervalue="Descrizione riga offerta">
					{!roff.Description}
						<apex:variable var="i" value="{!i+1}"/>
					</apex:column>
                    
		</apex:pageblocktable>               
    </apex:pageblockSection>  
                    
   </apex:pageBlock>                                              
  </apex:form>                                                         
 </apex:pageBlock>
</apex:page>

 
This was selected as the best answer
caterina ferrigno 8caterina ferrigno 8
Great Nayana, finally it works!!! Thanks a lot for your patience!!
I have another (i hope littlle) question:
I wish in this new row  input fields that are different from fields of the row above, that is i want in this new row  some custom fields : Descrizione__c and  N_ fattura__c.
is there a simple way to do this?
 
Nayana KNayana K
I have never tried like this, so I am not much sure. Also, I think this is difficult. I can see your pageBlocktable contains 8 columns. So dynamically how you want to add? On which column position (among 8 cols) fields should get added? there are so many confusions and I don't have any solution as of now.
caterina ferrigno 8caterina ferrigno 8
Ok ok, don't worry and thank you very much!