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
MeerMeer 

Adding row in table?

How can I achieve this thing in VfPage without refreshing the page

 

<HTML>
<HEAD>
<TITLE> Add/Remove dynamic rows in HTML table </TITLE>
<SCRIPT language="javascript">

function addRow(tableID)
{

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
cell1.innerHTML = rowCount + 1;
}

</SCRIPT>
</HEAD>
<BODY>

<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<TABLE id="dataTable" width="70px" border="1">
<TR>
<TD> 1 </TD>
</TR>
</TABLE>

</BODY>
</HTML>

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I've written a blog post in the past that dynamically adds and removes entries from a list - its aimed at new records but should be straightforward to repurpose:

 

http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html

All Answers

bob_buzzardbob_buzzard

I've written a blog post in the past that dynamically adds and removes entries from a list - its aimed at new records but should be straightforward to repurpose:

 

http://bobbuzzard.blogspot.co.uk/2011/07/managing-list-of-new-records-in.html

This was selected as the best answer
MeerMeer

Hi Bob, 

 

It was an awesome blog, it did help me out.. But I am facing some issue if you can kindly give a look. I am not able to save the Parent record (i.e Fin_Journal__c in my case)

Fin_Journal__c and Line__c are two custom objects and have Master-Detail Relationship

 

************Controller************

 

public class Line_Manager
{
public List<LineWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;

public Line_Manager ()
{
wrappers=new List<LineWrapper>();
wrappers.add(new LineWrapper(nextIdent++));
}

public Line_Manager (ApexPages.StandardController controller)
{
wrappers=new List<LineWrapper>();
wrappers.add(new LineWrapper(nextIdent++));
}

public void delWrapper()
{
Integer toDelPos=-1;
for (Integer idx=0; idx<wrappers.size(); idx++)
{
if (wrappers[idx].ident==toDelIdent)
{
toDelPos=idx;
}
}

if (-1!=toDelPos)
{
wrappers.remove(toDelPos);
}
}

public void addRows()
{
for (Integer idx=0; idx<addCount; idx++)
{
wrappers.add(new LineWrapper(nextIdent++));
}
}

public PageReference save()
{
List<Line__c> lines = new List<Line__c>();
for (LineWrapper wrap : wrappers)
{
lines.add(wrap.line);
}

insert lines;
}

public class LineWrapper
{
public Line__c line {get; private set;}
public Integer ident {get; private set;}

public LineWrapper(Integer inIdent)
{
ident=inIdent;
line =new Line__c(Journal__c = ApexPages.currentPage().getParameters().get('id'));
}
}

}

 

 

************VFPage************

<apex:page standardController="Fin_Journal__c" extensions="Line_Manager">
<script src="../../soap/ajax/24.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
function GetDescription()
{
var queryresult = sforce.connection.query("SELECT Description__c FROM Code_Combination__c WHERE Name = '" + document.getElementById('{!$Component.MyForm:acc}').value + "'", queryresult);
var records = queryresult.getArray('records');
document.getElementById('{!$Component.MyForm:desc}').value = records [0].Description__c;
}

</script>

<apex:form id="MyForm">

<table border="0" cellspacing = "0" cellpadding= "2">
<tr><td></td></tr><tr><td></td></tr><tr>
<td><apex:image url="some url here" width="35" height="40"/></td>

<td>
<apex:outputText ><font size="1" color="#606060" face="Arial"><b>New Journal</b></font></apex:outputText><br/>
<apex:outputText ><font size="4" color="Black" face="Arial">Journal Edit</font></apex:outputText>
</td>
</tr><tr><td></td></tr><tr><td></td></tr>
</table>

<center>
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Save & New"/>
<apex:commandButton value="Cancel"/>
</center>

<table><tr><td>&nbsp;</td></tr></table>

<apex:pageBlock mode="edit">
<apex:pageblockSection columns="1" Title="Journal Details">
<apex:inputfield value="{!Fin_Journal__c.Period__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Journal_Date__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Card__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Description__c}" style="width:270px"/>
<apex:outputLabel ></apex:outputLabel>
<apex:outputLabel ></apex:outputLabel>
</apex:pageblockSection>
</apex:pageBlock>

<apex:pageBlock mode="edit">
<apex:pageblockSection Title="Journal Settings">
<apex:inputfield value="{!Fin_Journal__c.Journal_Name__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:inputfield value="{!Fin_Journal__c.Batch__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Source__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Currency__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Status__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Category__c}"/>
<apex:inputfield value="{!Fin_Journal__c.Currency__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:inputfield value="{!Fin_Journal__c.Include_Tax__c}"/>
<apex:outputLabel ></apex:outputLabel>
<apex:outputLabel ></apex:outputLabel>
</apex:pageblockSection>
</apex:pageBlock>

</apex:form >

<apex:form >
<apex:pageBlock mode="edit">
<apex:pageBlockSection title="Lines" columns="1">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
<apex:column headerValue="Sr. #">
<apex:outputText value="{!wrapper.ident}"/>
</apex:column>
<apex:column headerValue="Account Code" style="width:30%">
<apex:inputField value="{!wrapper.line.Code_Combination__c}" style="width:90%"/>
</apex:column>
<apex:column headerValue="Description" style="width:30%">
<apex:inputField value="{!wrapper.line.Description__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Job" style="width:20%">
<apex:inputField value="{!wrapper.line.Job__c}" style="width:87%"/>
</apex:column>
<apex:column headerValue="Debit" style="width:6%">
<apex:inputField value="{!wrapper.line.Debit__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Credit" style="width:6%">
<apex:inputField value="{!wrapper.line.Credit__c}" style="width:99%"/>
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="New Line" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>
</apex:commandButton>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>



bob_buzzardbob_buzzard

In your save method, you are only writing the line items back to the database.  I'd suggest you should first delegate to the encapsulated standard controller's save, which will write the parent record, and then carry out your specific line item saving.

MeerMeer

Thanks a lot Bob. You Rock!!

 

Cheers,

 

Meer