473,509 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to add one row in table

228 New Member
i'm trying to add one row each time user click a link - but <table> doesnt want to wark this way - i tried different things but it gave me unknow run time error
the only thing which work is to reload whole table - but that is no solution for me
i cant reload any of the rows (they are part of a form)

that is how it looks like:
[html]<html>
<head>
<title>simple document</title>
<script language="javascript"><!--
//i am using ajax to get extrarow
var extrarow ='<tr><td>simply row</td></tr>';

function addrow(){

//this is wrong!! i know it is
//and i dont know how to repair it
document.getElementById('myTable').lastchild = extrarow;
//any ideas?
}

--></script>
</head>
<body>

<a href="javascript:addrow()"> event with add row</a>
<table id='myTable'><tr><td>existing row</td></tr></table>

</body>
</html>[/html]
i would appriciate any idea

thanks
jx2
Aug 15 '07 #1
7 1888
pbmods
5,821 Recognized Expert Expert
Heya, jx2.

Try something like this:
Expand|Select|Wrap|Line Numbers
  1. function addrow()
  2. {
  3.     .
  4.     .
  5.     .
  6.     // AJAX stuff goes here
  7.     .
  8.     .
  9.     .
  10.     // var extraRow = '<td>simply row</td>';
  11.  
  12.     var newRow = document.createElement('tr');
  13.     newRow.innerHTML = extraRow;
  14.  
  15.     document.getElementById('myTable').appendChild( newRow);
  16. }
  17.  
Aug 15 '07 #2
jx2
228 New Member
yeahi found it i cant believe!! i feel like writing in C ...
...So much code for simply thing!!

thats the function:
Expand|Select|Wrap|Line Numbers
  1. function addrow(){
  2.  
  3.     var myTT = document.getElementById('myTable') ;
  4.     var myTR = document.createElement('tr');
  5.     var myTD = document.createElement('td');
  6.     var myText = document.createTextNode('my text in new cell');
  7.     var myTBODY = document.createElement("tbody");
  8.     myTD.appendChild(myText);
  9.     myTR.appendChild(myTD);
  10.     myTBODY.appendChild(myTR);
  11.     myTT.appendChild(myTBODY);
  12. }
i cant believe!! or maybe i did something unneccesserlly?

thx pbmods!! this time i was faster :-)

regards
jx2
Aug 15 '07 #3
jx2
228 New Member
i 'va got a question hehe;-)
if you were writing this code what would you use? i mean i wanted to sent formated row(s) through ajax
lets say :"<tr><td>value1</td><td>value2</td></tr>"

but in this case it seems to be usless

cose i have to create all nodes in javascript (or maybe i dont have to?)

any solution?

regards
jx2
Aug 15 '07 #4
pbmods
5,821 Recognized Expert Expert
Heya, jx2.

One option would be to have your AJAX call fetch a JSON-encoded object instead of a string.

For example, instead of "<tr><td>value1</td><td>value2</td></tr>", it would return something similar to this:
Expand|Select|Wrap|Line Numbers
  1. [
  2.     {
  3.         'nodeName':'tr',
  4.         'contents':
  5.         [
  6.             {
  7.                 'nodeName':'td',
  8.                 'contents':'value1'
  9.             },
  10.  
  11.             {
  12.                 'nodeName':'td',
  13.                 'contents':'value2'
  14.             }
  15.         ]
  16.     }
  17. ]
  18.  
Your JavaScript could run through this object similarly to the way, for example, print_r() does it, creating nodes as you go.

But rather than reinvent an HTML interpreter on each system, why not just use the one built into the browser?

Let innerHTML do the work for you.
Aug 15 '07 #5
jx2
228 New Member
But rather than reinvent an HTML interpreter on each system, why not just use the one built into the browser?

Let innerHTML do the work for you.
well i know innerHTML would be great solution but is imposible to make it work with a tables ive got unknow run time error in IE if i try to do something like :
document.getElementById('myTbody').innerHTML = myRows;
this doesnt work it work fine with divs but not with tables!!
therefore i'm trying to to it In this ugly way
regards
jx2
Aug 15 '07 #6
pbmods
5,821 Recognized Expert Expert
Heya, JX2.

well i know innerHTML would be great solution but is imposible to make it work with a tables ive got unknow run time error in IE if i try to do something like :
document.getElementById('myTbody').innerHTML = myRows;
this doesnt work it work fine with divs but not with tables!!
therefore i'm trying to to it In this ugly way
regards
jx2
Is it possible to go with a hybrid solution? Instead of '<tr><td>value1</td><td>value2</td></tr>', can you fetch '<td>value1</td><td>value2</td>' instead? Then it would be easy to create the tr element and edit the innerHTML.
Aug 15 '07 #7
jx2
228 New Member
... can you fetch '<td>value1</td><td>value2</td>' instead? Then it would be easy to create the tr element and edit the innerHTML.
no !! it woudnt work!! any atempt to use innerHTML (except for <td>) creates error - believe me i tried everything!! e.g.
[html]
<table>
<tr><td>existing row</td></tr>
<div id="myDiv"> </div>
</table>[/html]

this look okey - its correct XML or XHTML code
but if you try to do something like
myDiv.innerHTML = '<Tr><td>newrow</td></tr>';
it will not work

i try also
[html]
<table>
<tr><td>existing row</td></tr>
<tr ><td id="myTD"></td> </tr>
</table>[/html]
and :
myTD.innerHTML = 'new row</td></tr><tr><td>anohter row';
well i know tricky but if the javascript did what i ask it will look like that:
[html]<table><tr><td>existing row</td></tr>
<tr ><td id="myTD">new row</td></tr>
<tr><td>anohter row</td> </tr>
</table>[/html]
which look correct but it doesnt work too

:-)
yeah its painfull but i think json_encode() is way to go... ;-/

regards jx2
Aug 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6576
by: Gaz | last post by:
Hi, I need to have a table nested within another table. The tables are alongside each other visually speaking, and the nested table (on the right) can vary in size. My problem is that when the...
61
24397
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
3
11347
by: Terrence Brannon | last post by:
I don't know what Postgres considers a relation and had no intention of creating one when piping my schema to it... I always DROP TABLE before CREATE TABLE, so here are the ERRORS emitted when...
4
15797
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
4
548
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int...
117
18411
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
76
271459
MMcCarthy
by: MMcCarthy | last post by:
Normalisation is the term used to describe how you break a file down into tables to create a database. There are 3 or 4 major steps involved known as 1NF (First Normal Form), 2NF (Second Normal...
7
4797
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
5
3816
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
5
4902
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
7234
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,...
0
7136
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...
0
7344
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,...
1
7069
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...
0
7505
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...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3216
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...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.