473,802 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Creation of table

Hi Guys,
Could any one help me out with codes to add rows to a table. Well I
kinda of got the codes from the following site
(http://www.interviewboard.com/DHTMLS...GridSample.htm) the
problem is this example only shows input box. Now i have a list that
means a 'select box' for which the value is retrieved from Database and
populated into the select list box. Now according to the example if all
residing in the javascript how am i gng to do that!. For example I have
the default row which has 3 text box, a select box(populated from
database and a delete button). Outside the table i have add row
When i click on add row another row should be created with 3 text box,
a select box and a delete button. all these input box and select box
whould have unique names cas i need to pass them to another page to get
the values.

following is the code that I have written :

-*************** *************** *********

<script language="javas cript">

//add a new row to the table
function addRow()
{
var tbl = document.getEle mentById("tblGr id");
var nextRow = tbl.tBodies[0].rows.length;

if (nextRow 5)
{
return;
}
else
{
//add a row to the rows collection and get a reference to the newly
added row
var newRow = document.all("t blGrid").insert Row();

alert ('nextrow' + nextRow);
var popCal1 = 'javascript:cal 1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal 2' + nextRow + '.popup();'

var nameBegDt = 'txtBeginDt'+ nextRow
var nameEndDt = 'txtEndDt'+ nextRow
var nameSubTyp = 'selSubTyp'+ nextRow
var nameLeaveAppl = 'txtLeaveAppl'+ nextRow
//add 3 cells (<td>) to the new row and set the innerHTML to contain
text boxes

var oCell = newRow.insertCe ll();
oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.b lur();'/><a href='" + popCal1 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";

oCell = newRow.insertCe ll();
oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><input
type='text' SIZE='10' name='" + nameEndDt + "'
onFocus='this.b lur();'/><a href='" + popCal2 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";

oCell = newRow.insertCe ll();
oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><select
name='"+ nameSubTyp +"' >--Select--<option value='F'></option><option
value='AM'>AM (Half)</option><option value='PM'>PM
(Half)</option><option value='S'>Sat Leave</option><option
value='T'>1.5 days</option></select></TD>";

oCell = newRow.insertCe ll();
oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><input
SIZE='4' maxlength='4' type='text' name='" + nameLeaveAppl + "'></TD>";

oCell = newRow.insertCe ll();
oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><input
type='image' src='images/delete.jpg'
onclick='remove Row(this);'/></TD>";

}
}

//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <trelement,
get the parent of the parent (in this case case <tr>)
*/
var tbl = document.getEle mentById("tblGr id");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);

if (nextRow < 3)
{
return;
}
else
{
var oRow = src.parentEleme nt.parentElemen t;

//once the row reference is obtained, delete it passing in its
rowIndex
document.all("t blGrid").delete Row(oRow.rowInd ex);
}
}

</script>
*************** *************** *************** *******
can anyone help please.

Thanks in advance

Regards
philip

Sep 25 '06 #1
2 2140
ph*******@gmail .com wrote:
Hi Guys,
Could any one help me out with codes to add rows to a table. Well I
kinda of got the codes from the following site
(http://www.interviewboard.com/DHTMLS...GridSample.htm) the
If this is an example of the quality of the code from that site, stop
using it.

problem is this example only shows input box. Now i have a list that
means a 'select box' for which the value is retrieved from Database and
populated into the select list box. Now according to the example if all
residing in the javascript how am i gng to do that!. For example I have
the default row which has 3 text box, a select box(populated from
database and a delete button). Outside the table i have add row
When i click on add row another row should be created with 3 text box,
a select box and a delete button. all these input box and select box
whould have unique names cas i need to pass them to another page to get
the values.

following is the code that I have written :

-*************** *************** *********

<script language="javas cript">
The language attribute is deprecated (years ago), type is required.

<script type="text/javascript">

>
//add a new row to the table
When posting code, use 2 or 4 spaces for indents and manually wrap code
at about 70 characters to prevent wrapping. Allowing auto-wrapping
nearly always introduces more errors.

function addRow()
{
var tbl = document.getEle mentById("tblGr id");
DOM methods should be tested before use.

var nextRow = tbl.tBodies[0].rows.length;
Why not put the tbody element in the HTML and add the id to that? Then
you'll know you are getting the right table section, rather than just
guessing that you want the first one - what if you later decide to add
a thead element? Or another tbody?

if (nextRow 5)
{
return;
}
else
{
//add a row to the rows collection and get a reference to the newly
added row
var newRow = document.all("t blGrid").insert Row();
Since you already have a reference to the table stored in the variable
"tbl", there seems little point in gettting it again. Also,
document.all should be included only for support of old IE, not as a
main part of the code. It should be dealt with when you detected
support for getElementById (as should support for insertRow).

The insertRow method requires an argument, either the row index to
insert the row at, or -1 to add it as the last row. In IE, if you
don't include a rowIndex argument, it will add it as the last row. In
other browsers will likey cause an error.

<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-39872903 >

alert ('nextrow' + nextRow);
var popCal1 = 'javascript:cal 1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal 2' + nextRow + '.popup();'

var nameBegDt = 'txtBeginDt'+ nextRow
var nameEndDt = 'txtEndDt'+ nextRow
var nameSubTyp = 'selSubTyp'+ nextRow
var nameLeaveAppl = 'txtLeaveAppl'+ nextRow
//add 3 cells (<td>) to the new row and set the innerHTML to contain
text boxes

var oCell = newRow.insertCe ll();
Like insertRow, insertCell must have a cell index value as its argument
(again, IE tollerates no argument and adds the cell as the last cell
but some (most?) other browsers don't).

<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-68927016 >

oCell.innerHTML = "<td align = 'center' bgcolor='#fffff f'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.b lur();'/><a href='" + popCal1 + "'><img
src='img/cal.gif' width='16' height='16' border='0' alt='Click Here to
Pick up the date'></a></TD>";
Here you are attempting to use innerHTML to create a TD element as a
child of a TD element. Whatever a particular browser may make of that
will be the result of error correction (and likely different across
browsers).

You can use innerHTML to modify the content of a cell, but never any
component of a table (although you can use it to write an entire
table). The rest of this function is similarly flawed - use DOM
methods to modify the cell, then add the innerHTML if you must - but
DOM methods would be better for that too.
[...]
//deletes the specified row from the table
function removeRow(src)
{
/* src refers to the input button that was clicked.
to get a reference to the containing <trelement,
get the parent of the parent (in this case case <tr>)
*/
var tbl = document.getEle mentById("tblGr id");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);

if (nextRow < 3)
{
return;
}
else
{
var oRow = src.parentEleme nt.parentElemen t;
Rather than guessing where the TR is, why not have a function that
returns the type of parent you are looking for:

function getParentByTagN ame(el, tagName){
var p = el.parent;
tagName = tagName.toLower Case();
(while p && tagName != p.tagName.toLow erCase()){
p = p.parentNode;
}
return p;
}

Then in your function:

var oRow = getParentByTagN ame(src, 'tr');
if (oRow && oRow.rowIndex 3){
oRow.parentNode .removeChild(oR ow);
}

>
//once the row reference is obtained, delete it passing in its
rowIndex
document.all("t blGrid").delete Row(oRow.rowInd ex);
You don't need to user document.all (or getElementById) to remove an
element if you already have a reference to the element.

[...]
can anyone help please.
I hope I did.
--
Rob

Sep 26 '06 #2
JRS: In article <11************ **********@m73g 2000cwd.googleg roups.com>,
dated Mon, 25 Sep 2006 02:27:58 remote, seen in
news:comp.lang. javascript, ph*******@gmail .com posted :
Well I
kinda of got the codes from the following site
(http://www.interviewboard.com/DHTMLS...GridSample.htm) the
Code copied from the Web is usually badly-written.
>
<script language="javas cript">
^^^^^^^^^^^^^^^ ^^^^^ Deprecated. Use type="text/javascript"
>
//add a new row to the table
function addRow()
Don't over-indent, especially in News. Two spaces per level is
sufficient. If your news-editor cannot replace tabs globally, ditch it.
{
var tbl = document.getEle mentById("tblGr id");
var nextRow = tbl.tBodies[0].rows.length;

if (nextRow 5)
{
return;
}
else
Using else after if () return is pointless.
{
//add a row to the rows collection and get a reference
to the newly
added row
Don't allow your posting agent to wrap code. Code in news should be
directly readable and executable, if you want readers to bother with it.
var newRow = document.all("t blGrid").insert Row();
Using document.all is at best unfashionable. Read FAQ 4.39.

It's a good idea to read the newsgroup and its FAQ. See below.
--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 26 '06 #3

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

Similar topics

1
2234
by: Klom Dark | last post by:
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and the name of a common command (btn_Command) assigned to it's Command property. The creation of the table is done by a function called drawGeo. drawGeo is called during the initial Page_Load (!IsPostBack), but should be called by btn_Command on...
2
2562
by: Dave Williamson | last post by:
When a ASPX page is created with dynamic controls based on what the user is doing the programmer must recreate the dynamic controls again on PostBack in the Page_Load so that it's events are wired and are called like a static control. Here is the problem that I need to solve. The processing overhead that occurs to determine what dynamic controls need to be added involves business logic and a query or queries of data in a sql server...
3
3988
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which step you're on. This all works fine, but I ran into some trouble when I started creating controls dynamically in my code-behind file. Each panel contains a table which is filled with various radio buttons, text fields and the such which are...
1
4837
by: russ | last post by:
Hi all, Here's a problem I'm having with a dynamic table. Following the guidelines here (http://www.codeproject.com/aspnet/dynamiccontrols.asp), which make perfect sense. The problem is that the table contains a SELECT box populated on the initial load. Every time I postback I'm inserting a column into the table, the dropdown always remains in the last column. First time I postback the dropdown is populated okay. The second time...
0
2074
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab, System and Software Engineering Lab), ULB (deComp) and the Belgian Association for Dynamic Languages (BADL) are very pleased to invite you to a whole day of presentations about the programming languages Self, Smalltalk and Common Lisp by experts in...
8
23399
by: william_dean | last post by:
Hello, I've done some searching around the post, and I have found quite a bit of information related to the setAttribute and it's related uses. My problem lies in the usage of colspanning in dynamic table creation. Code Snippet <SCRIPT LANGUAGE="JavaScript"> function morecase(id){ var tbody = document.getElementById(id).getElementsByTagName("TBODY"); var row1=document.createElement('TR'); var...
13
17170
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually launching Excel for data viewing. I'd prefer the user stay in Access. Creating dynamic crosstab queries is pretty simple. The problem is that the column count may shrink or grow depending on the filter.
3
2120
by: arunank | last post by:
Hi, The following code for dynamic table creation is not working. Can anyone please help me. The dynamically created rows and columns are not getting populated. CODE: ========= <html>
1
4920
by: cdmsenthil | last post by:
I have an Infragistics UltrawebGrid . Each Row in the grid is attached to a context menu using Infragistics CSOM Upon click on the menu, I am creating an Iframe dynamically which points to another page in the same domain which also contains infragistics datagid populated with default data retrieved from Data Base. After creating the frame I am attaching it to the HTML DOM and show it as modal popup with OK and Cancel Button inside an...
3
4858
by: tokcy | last post by:
hi everyone, i am creating dynamic row in a table using javascript its working fine and now i want to create more than 1 table using javascript which is dynamic its also working fine but when i am taking the value of each table individually thats i am not able to get any value. if i get the value of table then its not taking properly. let me explain the whole scenario... i want to add product specification . it may vary in number of...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9559
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
10301
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...
0
10058
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9107
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6835
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5492
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
5620
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2964
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.