Dynamic table rows

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: May 2008
Posts: 11
Reputation: oku is an unknown quantity at this point 
Solved Threads: 0
oku oku is offline Offline
Newbie Poster

Dynamic table rows

 
0
  #1
22 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 527
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 62
network18 network18 is offline Offline
Posting Pro
 
0
  #2
22 Days Ago
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;
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: oku is an unknown quantity at this point 
Solved Threads: 0
oku oku is offline Offline
Newbie Poster
 
0
  #3
21 Days Ago
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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 527
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 62
network18 network18 is offline Offline
Posting Pro
 
0
  #4
21 Days Ago
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. }
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC