473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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="javascript">

//add a new row to the table
function addRow()
{
var tbl = document.getElementById("tblGrid");
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("tblGrid").insertRow();

alert ('nextrow' + nextRow);
var popCal1 = 'javascript:cal1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal2' + 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.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.blur();'/><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.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='text' SIZE='10' name='" + nameEndDt + "'
onFocus='this.blur();'/><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.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><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.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
SIZE='4' maxlength='4' type='text' name='" + nameLeaveAppl + "'></TD>";

oCell = newRow.insertCell();
oCell.innerHTML = "<td align = 'center' bgcolor='#ffffff'><input
type='image' src='images/delete.jpg'
onclick='removeRow(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.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);

if (nextRow < 3)
{
return;
}
else
{
var oRow = src.parentElement.parentElement;

//once the row reference is obtained, delete it passing in its
rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
}
}

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

Thanks in advance

Regards
philip

Sep 25 '06 #1
2 2112
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="javascript">
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.getElementById("tblGrid");
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("tblGrid").insertRow();
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:cal1' + nextRow + '.popup();'
var popCal2 = 'javascript:cal2' + 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.insertCell();
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='#ffffff'><input
type='text' SIZE='10' name='" + nameBegDt + "'
onFocus='this.blur();'/><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.getElementById("tblGrid");
var nextRow = tbl.tBodies[0].rows.length;
alert("here:" + nextRow);

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

function getParentByTagName(el, tagName){
var p = el.parent;
tagName = tagName.toLowerCase();
(while p && tagName != p.tagName.toLowerCase()){
p = p.parentNode;
}
return p;
}

Then in your function:

var oRow = getParentByTagName(src, 'tr');
if (oRow && oRow.rowIndex 3){
oRow.parentNode.removeChild(oRow);
}

>
//once the row reference is obtained, delete it passing in its
rowIndex
document.all("tblGrid").deleteRow(oRow.rowIndex);
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**********************@m73g2000cwd.googlegroups .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="javascript">
^^^^^^^^^^^^^^^^^^^^ 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.getElementById("tblGrid");
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("tblGrid").insertRow();
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.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.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
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...
2
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...
3
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...
1
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...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
8
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...
13
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...
3
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
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...
3
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.