944,153 Members | Top Members by Rank

Ad:
Nov 11th, 2009
0

Dynamic table rows

Expand Post »
Hello!

I have been working on a dynamic solution for a web based purchase order system. When i enter a purchase order i am greeted with:



And i have started to code a solution where I can add rows when I need them!

Inspiration is:



The code I have so far is:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type='text/javascript'>
  4. var emptyRow = null;
  5. function init() {
  6. var table = document.getElementById( 'insertorder' );
  7. var tbody = table.getElementsByTagName( 'TBODY' )[ 0 ];
  8. var tr = tbody.firstChild;
  9. while ( tr && tr.nodeName != 'TR' ) {
  10. tr = tr.nextSibling;
  11. }
  12. emptyRow = tr.cloneNode( true );
  13. }
  14. function AddRow() {
  15. var table = document.getElementById( 'insertorder' );
  16. var tbody = table.getElementsByTagName( 'TBODY' )[ 0 ];
  17. var numRows = tbody.rows.length;
  18. var where = tbody.rows[ numRows - 1 ];
  19. var newRow = emptyRow.cloneNode( true );
  20. insertAfter( where, newRow );
  21. }
  22. function insertAfter( here, newNode ) {
  23. here.parentNode.insertBefore( newNode, here.nextSibling );
  24. }
  25. </script>
  26. </head>
  27. <body onload='init()'>
  28. <table border='1' id='insertorder'>
  29. <thead>
  30. <tr>
  31. <th>Part Number</th>
  32. <th>Description</th>
  33. <th>Qty</th>
  34. <th>Unit Cost</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <tr>
  39. <th><input type='text' size='10'></th>
  40. <th><input type='text' size='20'></th>
  41. <th><input type='text' size='5'></th>
  42. <th><input type='text' size='5'></th>
  43. </tr>
  44. </tbody>
  45. </table><br>
  46. <input type='button' value='AddRow' onclick='AddRow()'>
  47. </body>
  48. </html>

Currently each row has a unique reference

Partnumber line 1 is "l1pn" line 2 is "l2pn" and so on till 12
Description line 1 is "l1desc" line 2 is "l2desc" and so on till 12
etc etc

What else do I need to add to the above code to make it when i add a new row that the row reference changes from "l1pn" to "l2pn" etc and limit it to creating 12 rows only.

Many thanks in advance
Similar Threads
oku
Reputation Points: 10
Solved Threads: 0
Newbie Poster
oku is offline Offline
11 posts
since May 2008
Nov 12th, 2009
0
Re: Dynamic table rows
Its working perfectly fine , you can impose the limit like this -
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var numRows = tbody.rows.length;
  2. if(numRows >12)return false;
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Nov 12th, 2009
0
Re: Dynamic table rows
Many thanks network18,

The only issue remaining is that the first row has input names like l1pn, l1desc, l1qty and l1uc. When the second row is generated it doesn't seem to make it l2pn, l2desc, l1qty and 12uc etc, is there a way of doing this?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type='text/javascript'>
  4. var emptyRow = null;
  5. function init() {
  6. var table = document.getElementById( 'insertorder' );
  7. var tbody = table.getElementsByTagName( 'TBODY' )[ 0 ];
  8. var tr = tbody.firstChild;
  9. while ( tr && tr.nodeName != 'TR' ) {
  10. tr = tr.nextSibling;
  11. }
  12. emptyRow = tr.cloneNode( true );
  13. }
  14. function AddRow() {
  15. var table = document.getElementById( 'insertorder' );
  16. var tbody = table.getElementsByTagName( 'TBODY' )[ 0 ];
  17. var numRows = tbody.rows.length;
  18. if(numRows >12)return false;
  19. var where = tbody.rows[ numRows - 1 ];
  20. var newRow = emptyRow.cloneNode( true );
  21. insertAfter( where, newRow );
  22. }
  23. function insertAfter( here, newNode ) {
  24. here.parentNode.insertBefore( newNode, here.nextSibling );
  25. }
  26. </script>
  27. </head>
  28. <body onload='init()'>
  29. <table border='0' id='insertorder' style='width: 100%' align='center'>
  30. <thead>
  31. <tr>
  32. <th class='style5' style='width: 158px'>Part Number</th>
  33. <th class='style5' style='width: 525px'>Description</th>
  34. <th class='style5' style='width: 77px'>Qty</th>
  35. <th class='style5' style='width: 77px'>Unit Cost (<?php echo $currency; ?>)</th>
  36. </tr>
  37. </thead>
  38. <tbody>
  39. <tr>
  40. <th style="height: 26px;"><input type="text" name='l1pn' size=20 style="width: 160px" class=""></th>
  41. <th style="height: 26px;"><input type="text" name='l1desc' size=200 style="width: 522px" class=""></th>
  42. <th style="height: 26px;"><input type="text" name='l1qty' size=20 style="width: 83px" class=""></th>
  43. <th style="height: 26px;"><input type="text" name='l1uc' size=20 style="width: 83px" class=""></th>
  44. </tr>
  45. </tbody>
  46. </table>
  47. <br>
  48. <input type='button' value='Add a new line' onclick='AddRow()'>
  49. <div align="center"><input type='Submit' value='Submit'></center></div>
  50. </body>
  51. </html>

Many thanks again!
oku
Reputation Points: 10
Solved Threads: 0
Newbie Poster
oku is offline Offline
11 posts
since May 2008
Nov 12th, 2009
0
Re: Dynamic table rows
tried something like below , but still it will take some more efforts to get it working -
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function AddRow() {
  2. var table = document.getElementById( 'insertorder' );
  3. var tbody = table.getElementsByTagName( 'TBODY' )[ 0 ];
  4. var numRows = tbody.rows.length;
  5. if(numRows >12)return false;
  6. var where = tbody.rows[ numRows - 1 ];
  7. var newRow = emptyRow.cloneNode( true );
  8. for(var i=0; i<tbody.rows.length; i++)
  9. {
  10. var new_name = "l2pn";
  11. for(var j=0; j<tbody.rows[i].childNodes.length; j++)
  12. {
  13. alert(">>"+tbody.rows[i].childNodes[j].childName+">>");
  14. tbody.rows[i].childNodes[j].setAttribute('name',new_name);
  15. }
  16.  
  17. }
  18. insertAfter( where, newRow, numRows );
  19. }
  20. function insertAfter( here, newNode, numRows) {
  21. here.parentNode.insertBefore( newNode, here.nextSibling );
  22. //var here_name = here.name;
  23.  
  24. //alert(numRows+">>"+document.getElementByTagName('here.nextSibling'));
  25.  
  26. //alert(">>"+here_name+">>"+here_name.substring(1,2));
  27. var newNode_name = '';
  28. // document.getElementByTagName('').style.name =
  29. }
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Cookies with javascript
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: java script program to find dates between two dates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC