You need to sign in to do that
Don't have an account?

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 :

I want a new row under the first row!
How can I solve this problem?
Thanks.
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 :
I want a new row under the first row!
How can I solve this problem?
Thanks.
All Answers
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.
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
system.debug('====selectedIndex===='+selectedIndex);
It seems selectedIndex is returning null.
<apex:param name="selectedIndex" value="{!i}" assignTo="{!selectedIndex}"/>
</apex:column>
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"/>
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.
Modify the action function to keep some alerts to check if value is set in page level properly or not...
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 :
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
Error: unknown property 'Fattura__cStandardController.i'
Anyway, i have checked debug log with old code and i have this :
USER_DEBUG [24]|DEBUG|selectedIndex0.
modify commandbutton and keep thos modifications of pageblock table and let me know the outcome of alert when u ckick each row
c.cs83.visual.force.come says :
0
So try below code :
#1 :
#2 if above dint work :
if #2 din't work, then just modify #2's one line and try again :
Thanks anyway!!
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?