473,388 Members | 1,417 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,388 software developers and data experts.

How to update drop down box using data from last insert

32
I have table in Javascript and it has addrow, deleterow, submit, and functions.
when i click it automatically adds new row and it has select option to choose some of them
for ex. in added two rows one of them is Motherboard Asus and other is Motherboard Gigabyte.

I would like to have a drop down combo box. When i chose one item from drop down menu and in the next row of input that option let not exist in drop down menu.

Here is my code.
Thanks beforehands
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head><title>dinamik sheet</title>
  5. <script> 
  6. var i=iteration;
  7. function addrow(){
  8.     var tbl = document.getElementById('sheet');
  9.     var iteration = tbl.rows.length;
  10. document.getElementById('count_rows').value = iteration;
  11.     var row=tbl.insertRow(iteration);
  12.  
  13.     var cellLeft = row.insertCell(0);
  14.     var textNode = document.createTextNode(iteration);
  15.     cellLeft.appendChild(textNode);
  16.  
  17.     var cellRight = row.insertCell(1);
  18.     var el = document.createElement('input');
  19.     el.type = 'text';
  20.     el.name = 'txtRow'+iteration;
  21.     el.id = 'txtRow'+iteration;
  22.     el.size = 15;
  23.     el.value = '0';
  24.     el.onblur = sum;
  25.     cellRight.appendChild(el);
  26.  
  27.     var cellRight = row.insertCell(2);
  28.     var elaz = document.createElement('input');
  29.     elaz.type = 'text';
  30.     elaz.name = 'txtRowaz'+iteration;
  31.     elaz.id = 'txtRowaz'+iteration;
  32.     elaz.size = 20;
  33.     elaz.value = '0';
  34.     elaz.onblur = sum;
  35.     cellRight.appendChild(elaz);
  36.  
  37.  
  38.  
  39.     var cellRight1 = row.insertCell(3);
  40.     var ela = document.createElement('input');
  41.     ela.type = 'text';
  42.     ela.name = 'txtRowe'+iteration;
  43.     ela.id = 'txtRowe'+iteration;
  44.     ela.size = 20;
  45.     ela.value = '0';
  46.     ela.onblur = sum;
  47.     cellRight1.appendChild(ela);
  48.  
  49.     var cellRightsel = row.insertCell(4);
  50.     var sel = document.createElement('select');
  51.     sel.name = 'selRow' + iteration;
  52.     sel.id = 'selRow' + iteration;
  53.     sel.onchange = sum;
  54.     sel.options[0] = new Option('MotherBoard ASUS', 'MotherBoard ASUS');
  55.     sel.options[1] = new Option('MotherBoard Gigabyte', 'MotherBoard Gigabyte');
  56.     sel.options[2] = new Option('MotherBoard MSI', 'MotherBoard MSI');
  57.     sel.options[3] = new Option('Graphiqcard ASUS', 'Graphiqcard ASUS');
  58.     sel.options[4] = new Option('GraphigCard ATI', 'GraphigCard ATI');
  59.     sel.options[5] = new Option('GraphigCard GefORCE', 'GraphigCard GefORCE');
  60.     cellRightsel.appendChild(sel);
  61.     sum();
  62. }
  63. function removeRowFromTable()
  64. {
  65.   var tbl = document.getElementById('sheet');
  66.   var lastRow = tbl.rows.length;
  67.   if (lastRow > 2) tbl.deleteRow(lastRow - 1);
  68. }
  69.  
  70. function sum(){
  71.     var s1 = 0;
  72.     var s2 = 0;
  73.     var tbl=document.getElementById('sheet');
  74.     var iteration=tbl.rows.length-1;
  75.     for(var i=1; i<=iteration; i++){//loop through table rows
  76.         var el1 = document.getElementById('txtRow'+i);//Row's Income field
  77.         var el2 = document.getElementById('selRow'+i);//Row's percentage menu
  78.         var ela = document.getElementById('txtRowe'+i);//Row's Tax cell
  79.         var elaz=document.getElementById('txtRowaz'+i);
  80.  
  81.         if(!el1 || !el2 || !ela||!elaz) continue;
  82.         var txt = el1.value;
  83.         if(txt != ( '' + Number(txt) )) continue;  //reject non-numeric entries
  84.  
  85. var tax = Number(txt) * Number(130);
  86.  
  87. if(el2[el2.selectedIndex].value=="MotherBoard Gigabyte"){
  88.     var  tax = Number(txt) * Number(150);
  89.  
  90.  
  91. }
  92. if(el2[el2.selectedIndex].value=="MotherBoard MSI"){
  93.     var  tax = Number(txt) * Number(100);    
  94.  
  95. }
  96. if(el2[el2.selectedIndex].value=="Graphiqcard ASUS"){
  97.     var  tax = Number(txt) * Number(85);    
  98.  
  99. }
  100. if(el2[el2.selectedIndex].value=="GraphigCard ATI"){
  101.     var  tax = Number(txt) * Number(95);    
  102.  
  103. }
  104. if (el2[el2.selectedIndex].value=="GraphigCard ATI")
  105. {
  106.  
  107.         var tax = Number(txt) * Number(88);
  108. }
  109.  
  110.         ela.value = tax.toFixed(2);
  111.         elaz.value=tax.toFixed(2)/Number(txt);
  112.         if(isNaN(elaz.value)){
  113.         elaz.value=0;
  114.         }
  115.         s1 += Number(txt);
  116.         s2 += tax;
  117.  
  118. }
  119.     var t1 = document.getElementById('total');
  120.     var t2 = document.getElementById('taxtotal');
  121.     if(t1){ t1.value = s1.toFixed(2); }
  122.     if(t2){ t2.value = s2.toFixed(2); }
  123. }
  124.  
  125. onload = function(){
  126.     addrow();
  127. }
  128. </script>
  129. </head>
  130. <body>
  131. <form name="eval_edit" method="POST" action="part1.php?id=iteration-1">
  132. <table align="center" width="75%">
  133. <tr>
  134. <td align="center">Balance sheet</td></tr>
  135. <tr>
  136. <td align="center">
  137.     <table id="sheet" border="1">
  138.     <tr><td>object</td><td>Total amount</td><td>One ITEM Price</td><td name="amount">Total Item Price </td><td>Name</td></tr>
  139.  
  140.     </table>
  141. </td>
  142. </tr>
  143. <tr>
  144. <td align="center">
  145. <input type="hidden" name="count_rows" id="count_rows" />
  146. <input type="button" value="Add" onclick="addrow()" />
  147. <input type="button" value="Remove" onclick="removeRowFromTable()" />
  148. <input type="button" value="SUM" onClick="sum()"/>
  149. <input type="submit" value="Submit" /><br />
  150. </td>
  151. </tr>
  152. <tr>
  153. <td align="left">
  154. <input id="total" name="total" type="text"/>total amount of products<br />
  155. <input id="taxtotal" name="taxtotal" type="text"/>total price of products
  156. </td>
  157. </tr>
  158. </table>
  159. </form>
  160.  
  161. </body>
  162. </html>
Mar 10 '10 #1

✓ answered by azegurb

Thanks very much
never think such
I have appreciated your work
but i worked on it. but i got nothink (may be i know very little about javascript)
Thanks thanks very much

12 2776
acoder
16,027 Expert Mod 8TB
I'm not sure if I've quite understood what you want. Do you want that when a certain option is selected, in the next row, that option doesn't appear in the select box? If so, check the values of each row to decide which ones shouldn't appear. It may help to use an array to store these values.
Mar 13 '10 #2
acoder
16,027 Expert Mod 8TB
That confirms what I understood.

Use the select boxes' value of each row to see which ones have already been selected in your insert function, e.g.
Expand|Select|Wrap|Line Numbers
  1. opts[i] = selObj[i].value;
where opts would be an array containing all selected options.
Mar 13 '10 #3
azegurb
32
you can do as you said or another method you want
thank you
Mar 13 '10 #4
Using an array to keep track of the selected items is a good idea.

However, I see a problem ahead.

If a user adds the first line-item, and selects "Motherboard ASUS," then under your revised code the second line-item added would not have Motherboard ASUS as an option. So, the user selects GraphigCard ATI as their second line-item. What's the problem?

Problem 1: The first line-item still contains a complete list of products, including "GraphigCard ATI" which should not be available in that list if it has been selected as the second line-item. It needs to be updated on change.

Problem 2: If we add a third line-item, then the generated list of products should contain all products less "Motherboard ASUS" and "GraphigCard ATI." That's fine; however, if the user decides to change the product in the first line-item then the lists for every other line-item will be incorrect.

Solution: Using an array to track which items have been selected is a start. In addition to that you would need to re-iterate through each list of each line-item to update their options so that on change, at any point in time and from any line-item, all options are synchronized.

Personally I think this is an inconvenient approach in terms of interface. It would make just as much sense to have a single list up near your "Add" button which serves as the master list of available products. This makes your job easier in that you only have to account for a single SELECT element as opposed to an infinite number of them.

Let me know if you need any help coding such work.
Mar 13 '10 #5
azegurb
32
i need coding this section
but i dont know how
Mar 14 '10 #6
I'll be glad to help.

I looked through your code and found another design issue. Your delete function removes only the most recently inserted line-item. A good design should enable the user to delete any line-item in the table simply by selecting it and then clicking the delete button. We'll need to code that feature.

So here's what we need to do:

1. Use a master product list
The user chooses a product and then clicks "Add." A line-item is added to the table and the "Name" column will contain a text node holding the name of the product. No longer will a drop-down menu appear there.

2. Modify the delete function
We want the user to be able to remove any line-item in the table. In order to do this, we need to either add a "delete" column to our table with a checkbox (a traditional approach) for each line-item, or make the line-items highlight-able through CSS (a modern approach); or, we can mix the styles. Because I'm not the creator of this script, I'll help you to code using the traditional approach and leave the visual appeal up to you.

3. Consider adding table rows/cells differently
The insertCell and insertRow methods have been known to have serious compatibility issues. Especially in the Safari browser, where they cause the browser to crash. I won't change your code for you, but I suggest that you use the createElement method to add rows and cells to your table, since it is cross-browser compatible. When you examine my code you will see how I've implemented the createElement method.

Here is the modified code. Any changes I've made have been enclosed with commented lines "//--------------------":

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head><title>dinamik sheet</title>
  5. <script> 
  6. function addrow(){
  7.     var tbl = document.getElementById('sheet');
  8.     var iteration = tbl.rows.length;
  9. document.getElementById('count_rows').value = iteration;
  10.     //---------------------
  11.     var productList=document.getElementById('productList');
  12.     var thisProduct=productList.options[productList.selectedIndex].value;
  13.     var thisProductName=productList.options[productList.selectedIndex].text;
  14.     if(!thisProduct)
  15.         return false;
  16.     if(checkProducts(thisProductName)) {
  17.         alert('You have already added the product "'+thisProductName+'"');
  18.         return false;
  19.     }
  20.     //---------------------
  21.     var row=tbl.insertRow(iteration);
  22.     //---------------------
  23.     row.id='row'+iteration;
  24.  
  25.     var cellDelete=document.createElement('td');
  26.     var cBox=document.createElement('input');
  27.     cBox.type='checkbox';
  28.     cBox.id='rowDelete'+iteration.toString();
  29.     cellDelete.appendChild(cBox);
  30.     row.appendChild(cellDelete);
  31.     //---------------------
  32.  
  33.     var cellRight = row.insertCell(1);
  34.     var el = document.createElement('input');
  35.     el.type = 'text';
  36.     el.name = 'txtRow'+iteration;
  37.     el.id = 'txtRow'+iteration;
  38.     el.size = 15;
  39.     el.value = '0';
  40.     el.onblur = sum;
  41.     cellRight.appendChild(el);
  42.  
  43.     var cellRight = row.insertCell(2);
  44.     var elaz = document.createElement('input');
  45.     elaz.type = 'text';
  46.     elaz.name = 'txtRowaz'+iteration;
  47.     elaz.id = 'txtRowaz'+iteration;
  48.     elaz.size = 20;
  49.     elaz.value = '0';
  50.     elaz.onblur = sum;
  51.     cellRight.appendChild(elaz);
  52.  
  53.  
  54.  
  55.     var cellRight1 = row.insertCell(3);
  56.     var ela = document.createElement('input');
  57.     ela.type = 'text';
  58.     ela.name = 'txtRowe'+iteration;
  59.     ela.id = 'txtRowe'+iteration;
  60.     ela.size = 20;
  61.     ela.value = '0';
  62.     ela.onblur = sum; 
  63.     cellRight1.appendChild(ela);
  64.  
  65.     var cellRightsel = row.insertCell(4);
  66.     //---------------------
  67.     cellRightsel.id='selRow'+iteration.toString();
  68.     var productName=document.createTextNode(thisProductName);
  69.     cellRightsel.appendChild(productName);
  70.     //---------------------
  71.     sum();
  72. }
  73. //---------------------
  74. function checkProducts(n) {
  75.     var tbl=document.getElementById('sheet');
  76.     for(var i=0;i<tbl.rows.length;i++) {
  77.         if(tbl.rows[i].cells[4].innerHTML==n)
  78.             return true;
  79.     }
  80.     return false;
  81. }
  82. //---------------------
  83. function removeRowFromTable()
  84. {
  85.   var tbl = document.getElementById('sheet');
  86.     //---------------------
  87.  
  88.  
  89.     var stack=new Array();
  90.       for(var i=1;i<tbl.rows.length;i++) {
  91.         var row=tbl.rows[i];
  92.         var cBox=row.cells[0].firstChild;
  93.         if(cBox) {
  94.             if(cBox.checked)
  95.                 stack.push(row.id);
  96.         }
  97.     }
  98.     for(var i=0;i<stack.length;i++) {
  99.         var row=document.getElementById(stack[i]);
  100.         row.parentNode.removeChild(row);
  101.     }
  102.     sum();
  103.     //---------------------
  104. }
  105.  
  106. function sum(){
  107.     var s1 = 0;
  108.     var s2 = 0;
  109.     var tbl=document.getElementById('sheet');
  110.     var iteration=tbl.rows.length-1;
  111.     //---------------------
  112.     for(var x=1; x<=iteration; x++){//loop through table rows
  113.         var row=tbl.rows[x];
  114.         var i=row.id.substr(row.id.length-1);
  115.     //---------------------
  116.         var el1 = document.getElementById('txtRow'+i);//Row's Income field
  117.         var el2 = document.getElementById('selRow'+i);//Row's percentage menu
  118.         var ela = document.getElementById('txtRowe'+i);//Row's Tax cell
  119.         var elaz=document.getElementById('txtRowaz'+i);
  120.  
  121.         if(!el1 || !el2 || !ela||!elaz) continue;
  122.         var txt = el1.value;
  123.         if(txt != ( '' + Number(txt) )) continue;  //reject non-numeric entries
  124.  
  125. var tax = Number(txt) * Number(130);
  126.  
  127. if(el2.nodeValue=="MotherBoard Gigabyte"){
  128.     var  tax = Number(txt) * Number(150);
  129.  
  130.  
  131. }
  132. if(el2.nodeValue=="MotherBoard MSI"){
  133.     var  tax = Number(txt) * Number(100);    
  134.  
  135. }
  136. if(el2.nodeValue=="Graphiqcard ASUS"){
  137.     var  tax = Number(txt) * Number(85);    
  138.  
  139. }
  140. if(el2.nodeValue=="GraphigCard ATI"){
  141.     var  tax = Number(txt) * Number(95);    
  142.  
  143. }
  144. if (el2.nodeValue=="GraphigCard ATI")
  145. {
  146.  
  147.         var tax = Number(txt) * Number(88);
  148. }
  149.  
  150.         ela.value = tax.toFixed(2);
  151.         elaz.value=tax.toFixed(2)/Number(txt);
  152.         if(isNaN(elaz.value)){
  153.         elaz.value=0;
  154.         }
  155.         s1 += Number(txt);
  156.         s2 += tax;
  157.  
  158. }
  159.     var t1 = document.getElementById('total');
  160.     var t2 = document.getElementById('taxtotal');
  161.     if(t1){ t1.value = s1.toFixed(2); }
  162.     if(t2){ t2.value = s2.toFixed(2); }
  163. }
  164.  
  165. </script>
  166. </head>
  167. <body>
  168. <form name="eval_edit" method="POST" action="part1.php?id=iteration-1">
  169. <table align="center" width="75%">
  170. <tr>
  171. <td align="center">Balance sheet</td></tr>
  172. <tr>
  173. <td align="center">
  174.     <table id="sheet" border="1">
  175.     <tr><td>delete</td><td>Total amount</td><td>One ITEM Price</td><td name="amount">Total Item Price </td><td>Name</td></tr>
  176.  
  177.     </table>
  178. </td>
  179. </tr>
  180. <tr>
  181. <td align="center">
  182. <select id="productList">
  183.     <option value="" selected="selected">Select a Product:</option>
  184.     <option value="1">MotherBoard ASUS</option>
  185.     <option value="2">MotherBoard Gigabyte</option>
  186.     <option value="3">MotherBoard MSI</option>
  187.     <option value="4">Graphiqcard ASUS</option>
  188.     <option value="5">GraphigCard ATI</option>
  189.     <option value="6">GraphigCard GefORCE</option>
  190. </select>
  191. <input type="hidden" name="count_rows" id="count_rows" />
  192. <input type="button" value="Add" onclick="addrow()" />
  193. <input type="button" value="Remove" onclick="removeRowFromTable()" />
  194. <input type="button" value="SUM" onClick="sum()"/>
  195. <input type="submit" value="Submit" /><br />
  196. </td>
  197. </tr>
  198. <tr>
  199. <td align="left">
  200. <input id="total" name="total" type="text"/>total amount of products<br />
  201. <input id="taxtotal" name="taxtotal" type="text"/>total price of products
  202. </td>
  203. </tr>
  204. </table>
  205. </form>
  206.  
  207. </body>
  208. </html>
  209.  
Study my additions. I've done my best not to disrupt your code, but rather to add to it in a way that emulates your own understanding of the language.

If I can be of further help, please let me know!
Mar 14 '10 #7
Special items to note:

checkProducts function
This function was added as an alternative to the suggestions of others. Rather than keeping track of added products in an array--which could create obstacles when working with the delete function--I've created this function to scan the table for product names. In this way there is only one source of data to check.

sum function
This function was somewhat confusing to me, but I've done my best to work with it. You'll see that I've modified your method for gathering line-item data. Since we no longer use individual drop-down menus in the line-items, we can't check the OPTION values. You'll see that in your conditionals I've changed them to test the nodeValue properties of the "Name" columns.

Sorry I couldn't document so thoroughly. I'm adding information as I have the time.
Mar 14 '10 #8
azegurb
32
Thank you for help
but one thing i ask again if i dont disturb you (if possible)
you created drop down box with select options
i want it to be once one option selected and added (for ex: Mothboard asus) in coming rows in the drop down box motherboard asus option let not exist among select options or it disabled. because once it selected next time to choose that option will be impossible.
again thanks for attention
i will wait your responces
Mar 15 '10 #9
acoder
16,027 Expert Mod 8TB
@rbuczynski, nice work. I gave a rather simple approach to make things easier and less work to improve the code, but with those changes, there are a number of improvements. I'll leave you to complete the thread. Thanks!
Mar 15 '10 #10
azegurb
32
Thanks very much
never think such
I have appreciated your work
but i worked on it. but i got nothink (may be i know very little about javascript)
Thanks thanks very much
Mar 15 '10 #11
azegurb
32
Thanks rbuczynski and acoder
you helped me more
Mar 15 '10 #12
You're most welcome! I sure hope you can fix that problem with having no ability to think ;-)

I'm glad it worked out.

To answer your final question about removing items from the master product list, I programmed a simple alert dialog to take care of that. When you try to add another line-item of the same product, a dialog appears to prevent you from doing so:
Expand|Select|Wrap|Line Numbers
  1. if(checkProducts(thisProductName)) {
  2.   alert('You have already added the product "'+thisProductName+'"');
  3.   return false;
  4. }
That was a simple solution. If you want the items to be removed from the list, then you'll have to do more work. To do what you're proposing, you'll have to cache the removed items and preserve their positions in the list. Here's how:

1. Remove the dialog code (shown above)
We will no longer need it with these changes.

2. Remove the checkProducts function
We will no longer need it with these changes.

3. Create a new function called handleProducts
This new function will remove products from the master list when you add the selected product to the table. It will also put it back into the master list when you remove its respective line-item from the table.

Here is the code:
Expand|Select|Wrap|Line Numbers
  1. //---------------------
  2. function handleProducts(action,args) {
  3.     var productList=document.getElementById('productList');
  4.     if(action=='add') {
  5.         productCache.push(args);
  6.         productList.options[args.index].style.display='none';
  7.         productList.selectedIndex=0;
  8.     }
  9.     else if(action=='remove') {
  10.         for(var i=0;i<productCache.length;i++) {
  11.             if(productCache[i].text==args.text) {
  12.                 args=productCache[i];
  13.                 productCache.splice(i,1);
  14.                 productList.options[args.index].style.display='';
  15.                 return true;
  16.             }
  17.         }
  18.         return false;
  19.     }
  20.     else
  21.         return false;
  22. }
  23. //---------------------
Thanks to CSS, we're able to remove items from the list simply by hiding them through the "display" property. This preserves product order in the list without us having to manage it through messy code.

4. Add the global variable "productCache"
At the top of your code, outside of any functions, add this code:
Expand|Select|Wrap|Line Numbers
  1. var productCache=new Array();
This is where we'll cache information about the products.

5. Add handleProducts calls to your addrow and removeRowFromTable functions

At the bottom of your addrow function, add this code:
Expand|Select|Wrap|Line Numbers
  1. handleProducts('add',{    index:productList.selectedIndex,
  2.     text:thisProductName});
And in your removeRowFromTable function, in the for loop through the "stack" array, after the "row" variable is declared, add this:
Expand|Select|Wrap|Line Numbers
  1. handleProducts('remove',{text:row.cells[(row.cells.length-1)].innerHTML});
This will call handleProducts and successfully manage your master list items.

*PHEW!*

And that's it. As you can see, there was much more work involved, which is actually why I posted a simple solution the first time. But I can't blame you for asking. If any of these instructions have confused you, just copy the entire, revised source code from here:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head><title>dinamik sheet</title>
  5. <script>
  6. var productCache=new Array();
  7. function addrow(){
  8.     var tbl = document.getElementById('sheet');
  9.     var iteration = tbl.rows.length;
  10. document.getElementById('count_rows').value = iteration;
  11.     //---------------------
  12.     var productList=document.getElementById('productList');
  13.     var thisProduct=productList.options[productList.selectedIndex].value;
  14.     var thisProductName=productList.options[productList.selectedIndex].text;
  15.     if(!thisProduct)
  16.         return false;
  17.     //---------------------
  18.     var row=tbl.insertRow(iteration);
  19.     //---------------------
  20.     row.id='row'+iteration;
  21.  
  22.     var cellDelete=document.createElement('td');
  23.     var cBox=document.createElement('input');
  24.     cBox.type='checkbox';
  25.     cBox.id='rowDelete'+iteration.toString();
  26.     cellDelete.appendChild(cBox);
  27.     row.appendChild(cellDelete);
  28.     //---------------------
  29.  
  30.     var cellRight = row.insertCell(1);
  31.     var el = document.createElement('input');
  32.     el.type = 'text';
  33.     el.name = 'txtRow'+iteration;
  34.     el.id = 'txtRow'+iteration;
  35.     el.size = 15;
  36.     el.value = '0';
  37.     el.onblur = sum;
  38.     cellRight.appendChild(el);
  39.  
  40.     var cellRight = row.insertCell(2);
  41.     var elaz = document.createElement('input');
  42.     elaz.type = 'text';
  43.     elaz.name = 'txtRowaz'+iteration;
  44.     elaz.id = 'txtRowaz'+iteration;
  45.     elaz.size = 20;
  46.     elaz.value = '0';
  47.     elaz.onblur = sum;
  48.     cellRight.appendChild(elaz);
  49.  
  50.  
  51.  
  52.     var cellRight1 = row.insertCell(3);
  53.     var ela = document.createElement('input');
  54.     ela.type = 'text';
  55.     ela.name = 'txtRowe'+iteration;
  56.     ela.id = 'txtRowe'+iteration;
  57.     ela.size = 20;
  58.     ela.value = '0';
  59.     ela.onblur = sum; 
  60.     cellRight1.appendChild(ela);
  61.  
  62.     var cellRightsel = row.insertCell(4);
  63.     //---------------------
  64.     cellRightsel.id='selRow'+iteration.toString();
  65.     var productName=document.createTextNode(thisProductName);
  66.     cellRightsel.appendChild(productName);
  67.  
  68.     handleProducts('add',{    index:productList.selectedIndex,
  69.                 text:thisProductName});
  70.     //---------------------
  71.     sum();
  72. }
  73. //---------------------
  74. function handleProducts(action,args) {
  75.     var productList=document.getElementById('productList');
  76.     if(action=='add') {
  77.         productCache.push(args);
  78.         productList.options[args.index].style.display='none';
  79.         productList.selectedIndex=0;
  80.     }
  81.     else if(action=='remove') {
  82.         for(var i=0;i<productCache.length;i++) {
  83.             if(productCache[i].text==args.text) {
  84.                 args=productCache[i];
  85.                 productCache.splice(i,1);
  86.                 productList.options[args.index].style.display='';
  87.                 return true;
  88.             }
  89.         }
  90.         return false;
  91.     }
  92.     else
  93.         return false;
  94. }
  95. //---------------------
  96. function removeRowFromTable()
  97. {
  98.   var tbl = document.getElementById('sheet');
  99.     //---------------------
  100.     var stack=new Array();
  101.       for(var i=1;i<tbl.rows.length;i++) {
  102.         var row=tbl.rows[i];
  103.         var cBox=row.cells[0].firstChild;
  104.         if(cBox) {
  105.             if(cBox.checked)
  106.                 stack.push(row.id);
  107.         }
  108.     }
  109.     for(var i=0;i<stack.length;i++) {
  110.         var row=document.getElementById(stack[i]);
  111.     handleProducts('remove',{text:row.cells[(row.cells.length-1)].innerHTML});
  112.         row.parentNode.removeChild(row);
  113.     }
  114.     sum();
  115.     //---------------------
  116. }
  117.  
  118. function sum(){
  119.     var s1 = 0;
  120.     var s2 = 0;
  121.     var tbl=document.getElementById('sheet');
  122.     var iteration=tbl.rows.length-1;
  123.     //---------------------
  124.     for(var x=1; x<=iteration; x++){//loop through table rows
  125.         var row=tbl.rows[x];
  126.         var i=row.id.substr(row.id.length-1);
  127.     //---------------------
  128.         var el1 = document.getElementById('txtRow'+i);//Row's Income field
  129.         var el2 = document.getElementById('selRow'+i);//Row's percentage menu
  130.         var ela = document.getElementById('txtRowe'+i);//Row's Tax cell
  131.         var elaz=document.getElementById('txtRowaz'+i);
  132.  
  133.         if(!el1 || !el2 || !ela||!elaz) continue;
  134.         var txt = el1.value;
  135.         if(txt != ( '' + Number(txt) )) continue;  //reject non-numeric entries
  136.  
  137. var tax = Number(txt) * Number(130);
  138.  
  139. if(el2.nodeValue=="MotherBoard Gigabyte"){
  140.     var  tax = Number(txt) * Number(150);
  141.  
  142.  
  143. }
  144. if(el2.nodeValue=="MotherBoard MSI"){
  145.     var  tax = Number(txt) * Number(100);    
  146.  
  147. }
  148. if(el2.nodeValue=="Graphiqcard ASUS"){
  149.     var  tax = Number(txt) * Number(85);    
  150.  
  151. }
  152. if(el2.nodeValue=="GraphigCard ATI"){
  153.     var  tax = Number(txt) * Number(95);    
  154.  
  155. }
  156. if (el2.nodeValue=="GraphigCard ATI")
  157. {
  158.  
  159.         var tax = Number(txt) * Number(88);
  160. }
  161.  
  162.         ela.value = tax.toFixed(2);
  163.         elaz.value=tax.toFixed(2)/Number(txt);
  164.         if(isNaN(elaz.value)){
  165.         elaz.value=0;
  166.         }
  167.         s1 += Number(txt);
  168.         s2 += tax;
  169.  
  170. }
  171.     var t1 = document.getElementById('total');
  172.     var t2 = document.getElementById('taxtotal');
  173.     if(t1){ t1.value = s1.toFixed(2); }
  174.     if(t2){ t2.value = s2.toFixed(2); }
  175. }
  176.  
  177. </script>
  178. </head>
  179. <body>
  180. <form name="eval_edit" method="POST" action="part1.php?id=iteration-1">
  181. <table align="center" width="75%">
  182. <tr>
  183. <td align="center">Balance sheet</td></tr>
  184. <tr>
  185. <td align="center">
  186.     <table id="sheet" border="1">
  187.     <tr><td>delete</td><td>Total amount</td><td>One ITEM Price</td><td name="amount">Total Item Price </td><td>Name</td></tr>
  188.  
  189.     </table>
  190. </td>
  191. </tr>
  192. <tr>
  193. <td align="center">
  194. <select id="productList">
  195.     <option value="" selected="selected">Select a Product:</option>
  196.     <option value="1">MotherBoard ASUS</option>
  197.     <option value="2">MotherBoard Gigabyte</option>
  198.     <option value="3">MotherBoard MSI</option>
  199.     <option value="4">Graphiqcard ASUS</option>
  200.     <option value="5">GraphigCard ATI</option>
  201.     <option value="6">GraphigCard GefORCE</option>
  202. </select>
  203. <input type="hidden" name="count_rows" id="count_rows" />
  204. <input type="button" value="Add" onclick="addrow()" />
  205. <input type="button" value="Remove" onclick="removeRowFromTable()" />
  206. <input type="button" value="SUM" onClick="sum()"/>
  207. <input type="submit" value="Submit" /><br />
  208. </td>
  209. </tr>
  210. <tr>
  211. <td align="left">
  212. <input id="total" name="total" type="text"/>total amount of products<br />
  213. <input id="taxtotal" name="taxtotal" type="text"/>total price of products
  214. </td>
  215. </tr>
  216. </table>
  217. </form>
  218.  
  219. </body>
  220. </html>
Again, study this code and learn from it! Please note that the solutions that I've provided are extremely limited to your request. What I mean is, if you start to modify the structure of your table then almost everything will fail to work. This is due in part to references to specific table cells which are expected to contain key data for use in your script.

For further learning, consider studying "classes in JavaScript". Google it for more information. Overall, you're doing great and I wish the best of luck to you azegurb!
Mar 15 '10 #13

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

Similar topics

7
by: jason | last post by:
Is there a way - possibly a disconnected rs? - to update the contents of an existing pulldown on a page without having to re-submit the page for the user to see the pulldown populated with an...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
9
by: Vorpal | last post by:
Here is a small sample of data from a table of about 500 rows (Using MSSqlserver 2000) EntryTime Speed Gross Net ------------------ ----- ----- 21:09:13.310 0 0 0 21:09:19.370 9000 ...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
25
by: Neo Geshel | last post by:
This works: <form> <asp:TextBox id="name" /> <%= name.ClientID %> </form> But this DOES NOT work: <form>
4
by: tim.cavins | last post by:
I have a GridView populated by an ObjectDataSource. I am having issues passing the parameters to the objectdatasource. I have verified that the method is being called but none of the parameters...
8
by: satyabhaskar | last post by:
i am developing a web page in that i have a drop down list for Authors Name...which allows to select the Author available in the data source.and if the user wants a new author to insert into the...
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:
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.