473,595 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with adding dynamic rows to table

I am working on some JavaScript that dynamically adds rows to a table
in response to a button click. A new row does appear on the screen
when the button is clicked. However, that table to which a row has
been added is itself contained within an outer table (to handle the
desired screen layout). That outer table does not properly grow to
contain the new size of the table to which the row was added. (More
specifically, I have sporadically seen the outer table grow correctly,
but then most of the time it does not grow as desired.) Is there
something that needs to be done in the code that will make the right
thing happen? (This phenomena appears in Netscape 7.1 -- things work
correctly under Internet Explorer 6.0)
A second anomoly concerns a button that appears in the newly added
row. An "onClick" event is associated with that dynamically added
button. The newly added button works correctly under Netscape 7.1 but
fails to work under Internet Explorer 6.0. (It is like Explorer will
not honor an event that is set in place by code that is dynamically
created after the page is loaded.) Shown below is an excerpt of the
code that is attempting to set up the desired button. Is there
something apecial that must be done in the code to get Explorer to
handle this properly (I have tried spelling things as both "onclick"
and also "onClick")?

tbldata = document.create Element("TD");
item = document.create Element("BUTTON ");
v = 'doaction("Edit ",' + n +')';
item.setAttribu te("name","Edit ");
item.setAttribu te("onclick",v) ;
textitem = document.create TextNode("Edit" );
item.appendChil d(textitem);
tbldata.appendC hild(item);

Any suggestions would be appreciated.

Dave Eland
da******@oru.ed u
Jul 23 '05 #1
3 3148
> That outer table does not properly grow to
contain the new size of the table to which the row was added. (More
specifically, I have sporadically seen the outer table grow correctly,
but then most of the time it does not grow as desired.) Is there
something that needs to be done in the code that will make the right
thing happen? (This phenomena appears in Netscape 7.1 -- things work
correctly under Internet Explorer 6.0)
I don't know why it does that, do you can an example page so I can look
at it?

A second anomoly concerns a button that appears in the newly added
row. An "onClick" event is associated with that dynamically added
button. The newly added button works correctly under Netscape 7.1 but
fails to work under Internet Explorer 6.0. (It is like Explorer will
not honor an event that is set in place by code that is dynamically
created after the page is loaded.) Shown below is an excerpt of the
code that is attempting to set up the desired button. Is there
something apecial that must be done in the code to get Explorer to
handle this properly (I have tried spelling things as both "onclick"
and also "onClick")?


You probably need to create a callback function.

function createCallBack( jsCall) {
return eval("function( ) { "+jsCall+"; }");
}

item.onclick = createCallBack( v);

Assigning things to onclick by setting attributes is not exactly right,
because onclick is an event, and not an attribute. Therefor you must
assign it like an event. Even better would be to use addEventListene r,
but I don't know how well that is supported in IE.

Good luck,
Vincent

Jul 23 '05 #2
Shown below is the sample prototype code I am working on. In addition
to the problems already mentioned, there are also inconsistencies in
toggling the visibiliy of the "Insert", "Update", and "Delete"
buttons. (Things work correctly in Netscape 7.1 and not in Internet
Explorer 6.0. Note that the visibility settings on the SIDSelect and
PIDSelect selection items work correctly in BOTH browsers.)

Thanks for your help!

Dave Eland
da******@oru.ed u

<html>
<head>
<script LANGUAGE="JavaS cript">
var needsubmit,edit num;

function LTrim(strValue)
{
var LTRIMrgExp = /^\s */;
return strValue.replac e(LTRIMrgExp,'' );
}
function RTrim(strValue)
{
var RTRIMrgExp = /\s *$/;
return strValue.replac e(RTRIMrgExp,'' );
}
function Trim(strValue)
{
return LTrim(RTrim(str Value));
}
function setstatus(item, value)
{
document.getEle mentById(item). setAttribute("S TATUS",value);
}
function getstatus(item)
{
return(document .getElementById (item).getAttri bute("STATUS")) ;
}
function setvisible(item )
{
var v;

if (getstatus(item ) == "Show") v = "visible"; else v = "hidden";
document.getEle mentById(item). style.visibilit y = v;
}
function setallvisible()
{
setvisible("SID Select");
setvisible("PID Select");
setvisible("Ins ert");
setvisible("Upd ate");
setvisible("Del ete");
}
function hadchange(item)
{
setstatus("Inse rt","Show"); /**/
setstatus("Upda te","Show"); /**/
setstatus("Dele te","Show"); /**/
setallvisible()
}
function addrow()
// add new blank row at end of table
{
var n,v,tbl,tblbody ,tblrow,tbldata ,item,textitem, rowlist;

tbl = document.getEle mentById("tbl2" );
tblbody = tbl.getElements ByTagName("tbod y").item(0);
rowlist = tbl.getElements ByTagName("tr") ;
n = rowlist.length;

tblrow = document.create Element("TR");

tbldata = document.create Element("TD");
item = document.create Element("INPUT" );
item.setAttribu te("type","HIDD EN");
item.setAttribu te("name","PID" );
item.setAttribu te("value"," ");
tbldata.appendC hild(item);

item = document.create Element("INPUT" );
item.setAttribu te("type","TEXT ");
item.setAttribu te("name","PNAM E");
item.setAttribu te("value"," ");
item.setAttribu te("size",15);
item.setAttribu te("readonly",t rue);
tbldata.appendC hild(item);

tblrow.appendCh ild(tbldata);

tbldata = document.create Element("TD");
item = document.create Element("BUTTON ");
v = 'doaction("Edit ",' + n +')';
item.setAttribu te("name","Edit ");
item.setAttribu te("onclick",v) ;
textitem = document.create TextNode("Edit" );
item.appendChil d(textitem);
tbldata.appendC hild(item);

tblrow.appendCh ild(tbldata);

tblbody.appendC hild(tblrow);
}
function delrow(rownum)
// delete row if not at end of list
{
var i,v,tbl,tblbody ,rowlist,button list;

tbl = document.getEle mentById("tbl2" );
tblbody = tbl.getElements ByTagName("tbod y").item(0);
rowlist = tbl.getElements ByTagName("tr") ;
if (rownum < rowlist.length - 1)
{
tblbody.removeC hild(rowlist[rownum]);
buttonlist = document.getEle mentsByName("Ed it");
for (i = 0; i < buttonlist.leng th; i++)
{
v = 'doaction("Edit ",' + i + ')';
buttonlist[i].setAttribute(" onClick",v);
}
}
}
function doaction(curact ion,num)
{
var i;
var selecteditem,pi d,pname,pidlist ,pnamelist;

document.thisFo rm.ACTION.value = curaction;
if (curaction == "SetScreen" )
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "First")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Prev")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Next")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Last")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Jump")
{
var SIDSelect;

SIDSelect = document.getEle mentById("SIDSe lect");
if (SIDSelect.styl e.visibility == "hidden")
setstatus("SIDS elect","Show");
else
setstatus("SIDS elect","Hide");
SIDSelect.selec tedIndex = -1;
setallvisible() ;
needsubmit = false;
}
if (curaction == "Edit")
{
var PIDSelect;

editnum = num;
PIDSelect = document.getEle mentById("PIDSe lect");
if (PIDSelect.styl e.visibility == "hidden")
setstatus("PIDS elect","Show");
else
setstatus("PIDS elect","Hide");
needsubmit = false;
PIDSelect.selec tedIndex = -1;
setallvisible() ;
}
if (curaction == "SelectSID" )
{
if (document.thisF orm.SIDSelect.v alue == "Cancel")
{
needsubmit = false;
}
else
{
needsubmit = true;
document.thisFo rm.submit();
}
setstatus("SIDS elect","Hide");
setallvisible() ;
}
if (curaction == "SelectPID" )
{
if (document.thisF orm.PIDSelect.v alue != "Cancel")
{
selecteditem =
Trim(document.g etElementById(" PIDSelect").val ue);
i = selecteditem.in dexOf(" ");
pid = Trim(selectedit em.substring(0, i));
pname = Trim(selectedit em.substring(i) );
pidlist = document.getEle mentsByName("PI D");
pidlist[editnum].value = pid;
pnamelist = document.getEle mentsByName("PN AME");
pnamelist[editnum].value = pname;
if (pid == "" && editnum < pidlist.length-1)
{
delrow(editnum) ;
}
if (pid != "" && editnum == pidlist.length-1)
{
addrow();
}
setstatus("Upda te","Show");
}
needsubmit = false;
setstatus("PIDS elect","Hide");
setallvisible() ;
}
if (curaction == "Insert")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Update")
{
needsubmit = true;
document.thisFo rm.submit();
}
if (curaction == "Delete")
{
needsubmit = true;
document.thisFo rm.submit();
}
}
function wantsubmit()
{
return(needsubm it);
}
function initialize()
{
editnum = 0;
needsubmit = true;
setstatus("Inse rt","Hide");
setstatus("Upda te","Hide");
setstatus("Dele te","Hide");
setallvisible() ;
}
</script>
<title>Supplier s info</title>
</head>
<body onLoad='initial ize()'>
<FORM NAME=thisForm ACTION="showpar m.cgi" METHOD="POST"
onSubmit='retur n wantsubmit()'>
<TABLE ID="tbl0" BORDER="1">
<TR>
<TD>
<SELECT NAME="SCREEN" onChange='doact ion("SetScreen" ,0)'>
<OPTION SELECTED> Suppliers
<OPTION> Parts
</SELECT>
<INPUT TYPE="HIDDEN" NAME="ACTION" VALUE="UNKNOWN" >
<BR><BR>
<BUTTON ID="First" STATUS="Show"
onClick='doacti on("First",0)'> First</BUTTON>
<BUTTON ID="Prev" STATUS="Show"
onClick='doacti on("Prev",0)'>P rev</BUTTON>
<BUTTON ID="Next" STATUS="Show"
onClick='doacti on("Next",0)'>N ext</BUTTON>
<BUTTON ID="Last" STATUS="Show"
onClick='doacti on("Last",0)'>L ast</BUTTON>
<BUTTON ID="Jump" STATUS="Show"
onClick='doacti on("Jump",0)'>J ump</BUTTON>
<BR><BR> Supplier
<INPUT TYPE="HIDDEN" NAME="SID" VALUE="1">
<BR>
<TABLE ID="tbl1" BORDER="1">
<TR>
<TD>SNAME</TD>
<TD>
<INPUT TYPE="TEXT" NAME="SNAME" VALUE="Jones" SIZE=15
onChange='hadch ange()'>
</TD>
</TR>

<TR>
<TD>CITY</TD>
<TD>
<INPUT TYPE="TEXT" NAME="CITY" VALUE="Tulsa" SIZE=15
onChange='hadch ange()'>
</TD>
</TR>
</TABLE>
<BR> Parts
<BR>
<TABLE ID="tbl2" BORDER="1">
<TR>
<TD>
<INPUT TYPE="HIDDEN" NAME="PID" VALUE="1">
<INPUT TYPE="TEXT" NAME="PNAME" VALUE="Bolt" SIZE=15 READONLY=true>
</TD>
<TD>
<BUTTON NAME="Edit" onClick='doacti on("Edit",0)'>E dit</BUTTON>
</TD>
</TR>

<TR>
<TD>
<INPUT TYPE="HIDDEN" NAME="PID" VALUE="3">
<INPUT TYPE="TEXT" NAME="PNAME" VALUE="Washer" SIZE=15
READONLY=true>
</TD>
<TD>
<BUTTON NAME="Edit" onClick='doacti on("Edit",1)'>E dit</BUTTON>
</TD>
</TR>

<TR>
<TD>
<INPUT TYPE="HIDDEN" NAME="PID" VALUE="4">
<INPUT TYPE="TEXT" NAME="PNAME" VALUE="Screw" SIZE=15
READONLY=true>
</TD>
<TD>
<BUTTON NAME="Edit" onClick='doacti on("Edit",2)'>E dit</BUTTON>
</TD>
</TR>

<TR>
<TD>
<INPUT TYPE="HIDDEN" NAME="PID" VALUE=" ">
<INPUT TYPE="TEXT" NAME="PNAME" VALUE=" " SIZE=15 READONLY=true>
</TD>
<TD>
<BUTTON NAME="Edit" onClick='doacti on("Edit",3)'>E dit</BUTTON>
</TD>
</TR>
</TABLE>
<BR>
<BUTTON ID="Insert" STATUS="Hide"
onClick='doacti on("Insert",0)' >Insert</BUTTON>
<BUTTON ID="Update" STATUS="Hide"
onClick='doacti on("Update",0)' >Update</BUTTON>
<BUTTON ID="Delete" STATUS="Hide"
onClick='doacti on("Delete",0)' >Delete</BUTTON>
</TD>

<TD>
<SELECT ID="SIDSelect" NAME="SIDSelect " SIZE=20 STATUS="Hide"
onChange='doact ion("SelectSID" ,0)'>
<OPTION VALUE="Cancel"> Cancel</OPTION>
<OPTION VALUE="1 Jones">Jones</OPTION>
<OPTION VALUE="2 Parker">Parker</OPTION>
<OPTION VALUE="3 Baker">Baker</OPTION>
</SELECT>
<SELECT ID="PIDSelect" NAME="PIDSelect " SIZE=20 STATUS="Hide"
onChange='doact ion("SelectPID" ,0)'>
<OPTION VALUE="Cancel"> Cancel</OPTION>
<OPTION VALUE=" "> </OPTION>
<OPTION VALUE="1 Bolt">Bolt</OPTION>
<OPTION VALUE="2 Nut">Nut</OPTION>
<OPTION VALUE="3 Washer">Washer</OPTION>
<OPTION VALUE="4 Screw">Screw</OPTION>
</SELECT>
</TD>
</TR>
</TABLE>
</FORM>
<script language="javas cript" src="showdom2.j s"></script>
</body>
</html>
On Mon, 07 Jun 2004 11:45:47 +0200, Vincent van Beveren
<vi*****@provid ent.remove.this .nl> wrote:
That outer table does not properly grow to
contain the new size of the table to which the row was added. (More
specifically, I have sporadically seen the outer table grow correctly,
but then most of the time it does not grow as desired.) Is there
something that needs to be done in the code that will make the right
thing happen? (This phenomena appears in Netscape 7.1 -- things work
correctly under Internet Explorer 6.0)


I don't know why it does that, do you can an example page so I can look
at it?

A second anomoly concerns a button that appears in the newly added
row. An "onClick" event is associated with that dynamically added
button. The newly added button works correctly under Netscape 7.1 but
fails to work under Internet Explorer 6.0. (It is like Explorer will
not honor an event that is set in place by code that is dynamically
created after the page is loaded.) Shown below is an excerpt of the
code that is attempting to set up the desired button. Is there
something apecial that must be done in the code to get Explorer to
handle this properly (I have tried spelling things as both "onclick"
and also "onClick")?


You probably need to create a callback function.

function createCallBack( jsCall) {
return eval("function( ) { "+jsCall+"; }");
}

item.onclick = createCallBack( v);

Assigning things to onclick by setting attributes is not exactly right,
because onclick is an event, and not an attribute. Therefor you must
assign it like an event. Even better would be to use addEventListene r,
but I don't know how well that is supported in IE.

Good luck,
Vincent


Jul 23 '05 #3
Well, I found one ugly fix, put this after your addrow function and it
will expand... because it will need to reevaluate the DOM tree, else I
wouldn't know

mainTable = document.getEle mentById("tbl0" );
mainTable.style .display='block ';
mainTable.style .display= '';

This might also cause some more unpredictable behaviour.

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1651
by: Duncan Spence | last post by:
Hi all, I'm sure I'm doing something silly here, but can't see it! I'm creating a series of combo boxes on a Windows Form in VB.NET. The lists of all of the comboboxes are identical and are held in a table in a dataset and the number created is based on the number of rows in a second table in the same dataset.
4
5466
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
2
5653
by: Viorel | last post by:
Adding new row with default values. In order to insert programmatically a new row into a database table, without direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow, DataTable.Rows.Add and DataAdapter.Update member functions. Before adding the new row object to the Rows collection, all of the row's fields that do not accept NULL must be assigned, otherwise an exception is thrown. However, I do not want to assign...
3
2768
by: Rick | last post by:
I have an interesting problem when I run the following code in Netscape (7.02) vs. IE. This page works great in IE and all my controls bring up the validation summary dialog box if the required field is not filled out. However in Netscape NONE of the required field validations occurs at all in Netscape. The form is posting correctly because I can walk through the post back process. Any ideas why this is happening or how to fix it?
0
2393
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta version, the program is not working as in version 1.1. So what is the problem? Microsoft declared that the version 2.0 is fully backward support, but it is not. Is that the problem with 2.0 version? I find out the problem in version 2.0. After...
2
4952
by: dschectman | last post by:
This appears to be a feature of IE JavaScript. I am running IE 6.0 with the latest patches from Microsoft. Are there any workarounds other than re-coding the source HTML to place all the non-visible columns at the front? I have a page with a dynamic table. The table has some visible columns and some non-visible columns. The style "hide" is .hide { display:none; } <table id="SelectedList">
2
3105
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so that it may help others. If posting this script is against the rules in this group then please accept my appologies. I developped this script so that I can add and remove rows in a table in which I have various input fields and I would use the...
2
17074
by: cbjewelz | last post by:
Hey all. So I'm having problems with cross browser alignments. I'm looking at Safari and Mozilla Firefox. I develop in Safari and so it looks perfect there however in Firefox my vertical alignments and div widths are off. It's as if firefox has a different definition of a pixel than safari. Here is the url: http://theprize.chemouni.com/testing.php. When you select the Option from the pull down, the first part of the form appears. Then when...
0
5277
by: Eniac | last post by:
Hi, I've been working on a custom user control that needs to be modified and the validation is causing me headaches. The control used to generate a table of 4 rows x 7 columns to display all the days in the week with dates and textboxes to fill in some data. row 1: question
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8019
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
3873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1490
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.