| | |
help with dynamic table options
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 24
Reputation:
Solved Threads: 0
Hello, I have one question again if i dont disturb you
thank you very much you helped me
with your help i created one table again and in this table i added some another options
for ex at the previous table there were only one problem sum of the dynamically added rows
in this beside sum i need to sum of the percent of this sum
for ex 10% of this dynamically added rows
for ex. i added ten rows and i entered different values into these rows. in front of each row there are different option of percent selection.
in row1 i added 1000 and chose from drop down box 10%
in row2 i added 2500 and chose from drop down box 30%
.............................................................
and so on
and finally i summed separately sum of the values and sum percentage of this values to other texbox value
if possible help me
thanks beforehands
here is code
thank you very much you helped me
with your help i created one table again and in this table i added some another options
for ex at the previous table there were only one problem sum of the dynamically added rows
in this beside sum i need to sum of the percent of this sum
for ex 10% of this dynamically added rows
for ex. i added ten rows and i entered different values into these rows. in front of each row there are different option of percent selection.
in row1 i added 1000 and chose from drop down box 10%
in row2 i added 2500 and chose from drop down box 30%
.............................................................
and so on
and finally i summed separately sum of the values and sum percentage of this values to other texbox value
if possible help me
thanks beforehands
here is code
html Syntax (Toggle Plain Text)
<html><head><title>dinamik sheet</title> <script> function addrow(){ var tbl=document.getElementById('sheet'); var lastrow=tbl.rows.length; var iteration=lastrow; var row=tbl.insertRow(lastrow); var cellLeft=row.insertCell(0); var textNode=document.createTextNode(iteration); cellLeft.appendChild(textNode); var cellRight=row.insertCell(1); var el=document.createElement('input'); el.type='text'; el.name='txtRow'+iteration; el.size=40; el.setAttribute('sumMe',"1"); cellRight.appendChild(el); var cellRight2=row.insertCell(2); var el1=document.createElement('input'); el1.type='text'; el1.name='txtRowe'+iteration; el1.size=40; el1.setAttribute('sumMe',"1"); cellRight2.appendChild(el1); var cellRightsel=row.insertCell(3); var sel=document.createElement('select'); sel.name='selRow'+iteration; sel.options[0]=new Option('10%','value0'); sel.options[1]=new Option('20%','value1'); sel.options[2]=new Option('30%','value2'); cellRightsel.appendChild(sel); var cellRightsel2=row.insertCell(4); var sel1=document.createElement('input'); sel1.type='button'; sel1.value='sum row'+iteration; sel1.name='button'+iteration; cellRightsel2.appendChild(sel1); } </script> <script> function sum(){ var form=document.getElementById('eval_edit'); if(!form) return; var fields=form.getElementsByTagName('input'); var s=0; for (var i=0;i<fields.length;i++){ if( fields[i].getAttribute('sumMe') != "1" ) continue;//reject any field not carring the "sumMe" attribute var txt = fields[i].value; if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries s += Number(txt); } if(form['total']){ form['total'].value = s; } } onload = function(){ sum(); } </script> </head> <body> <form name="eval_edit" method="POST"> <table align="center" width="75%"> <tr> <td align="center">Balance sheet</td></tr> <tr><td align="center"> <table border="1" id="sheet"><tr><td>object</td><td>Income</td><td>Tax from income</td><td>instruktor</td></tr> <tr><td>1</td> <td><input sumMe="1" type="text" name="txtrow1" id="txtrow1" size="40"/></td><td><input sumMe="1" type="text" name="txtrowe" id="txtrowe" size="40"/></td> <td><select name="selRow0"> <option value="value0">10%</option> <option value="value1">20%</option> <option value="value2">30%</option></select></td><td><input type="button" name="button1" value="sum row1" id="button1"></tr></table> INCOME SUM<input name="total" type="text"/> <input type="button" value="Add" onclick="addrow()" /> <input type="button" value="Remove" onclick="removeRow()" /> <input type="button" value="SUM" onClick="sum()"/> <input type="submit" value="Submit" /> <input name="taxtotal" type="text"/>Tax SUM with desirable percent for ex: 20% </td> </tr> </table> </form> </body> </html>
Last edited by peter_budo; Sep 27th, 2009 at 8:27 am. Reason: Correcting openning tag
Here's a revised
This does everything necessary to calculate the tax for each row and to calculate the two sum totals (income and tax).
You will also need some changes to the
sum function: javascript Syntax (Toggle Plain Text)
function sum(){ var form=document.getElementById('eval_edit'); if(!form) return; var s1 = 0; var s2 = 0; var tbl=document.getElementById('sheet'); var iteration=tbl.rows.length-1; for(var i=1; i<=iteration; i++){ var el = form['txtRow'+i]; if(!el) continue; var txt = el.value; if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries var el2 = form['selRow'+i]; var el3 = document.getElementById('txtRowe'+i); if(!el2 || !el3) alert('Error in calculating totals'); var percent = Number(el2[el2.selectedIndex].value)/100; var tax = Number(txt) * percent; el3.innerHTML = tax.toFixed(2); s1 += Number(txt); s2 += tax; } if(form['total']){ form['total'].value = s1.toFixed(2); } if(form['taxtotal']){ form['taxtotal'].value = s2.toFixed(2); } }
This does everything necessary to calculate the tax for each row and to calculate the two sum totals (income and tax).
You will also need some changes to the
add function. Rather than do the whole thing for you, here are some clues:- Because tax is calculated not user-entered, you do not need input fields for tax. Instead, the revised sum() function puts the tax for each row directly into the tax cells. Make sure the tax cell is given
id = 'txtRowe'+iteration. - Ensure that el and sel are given a name and id (with identical values).
- In the select menu change the options values to : value="10" value="20" value="30"
- Get rid of "sum RowX" buttons (and their table cells). They are not necessary because the revised sum() function performs these calculations every time it is called.
- Add the following events:
- el.onblur=sum;
- sel.onchange=sum;
- el.onblur=sum;
- Remove your hard-coded HTML row and call
addrow();in the anonymous onload function. This will guarantee that your first row is of identical format to other rows (easier to maintain).
Last edited by Airshow; Sep 26th, 2009 at 12:54 pm.
50% of the solution lies in accurately describing the problem!
Azegurb,
Changes to the HTML are very simple. Changes to the
I must go out now - important birthday party (my neighbour's 70th) and may not get back her until Monday evening (GMT).
This should give you plenty of time to work through my instructions and fix the code for yourself. You may need to insert some debug statements to work out where it's going wrong. I'm sure you can do it on your own and I will check back here on Monday to see how you got on.
Airshow
Changes to the HTML are very simple. Changes to the
addRow() function are a bit harder.I must go out now - important birthday party (my neighbour's 70th) and may not get back her until Monday evening (GMT).
This should give you plenty of time to work through my instructions and fix the code for yourself. You may need to insert some debug statements to work out where it's going wrong. I'm sure you can do it on your own and I will check back here on Monday to see how you got on.
Airshow
Last edited by Airshow; Sep 26th, 2009 at 2:53 pm.
50% of the solution lies in accurately describing the problem!
Azegurb,
Neighbour's party was good, thank you.
Here is some code, but you must be sure to go through and see how it works. Learn from it, don't just accept it. I am sure you will.
Airshow
Neighbour's party was good, thank you.
Here is some code, but you must be sure to go through and see how it works. Learn from it, don't just accept it. I am sure you will.
HTML Syntax (Toggle Plain Text)
<body> <form name="eval_edit" method="POST"> <table align="center" width="75%"> <tr> <td align="center">Balance sheet</td></tr> <tr> <td align="center"> <table id="sheet" border="1"> <tr><td>object</td><td>Income</td><td>Tax from income</td><td>instruktor</td></tr> </table> </td> </tr> <tr> <td align="center"> <input type="button" value="Add" onclick="addrow()" /> <input type="button" value="Remove" onclick="removeRow()" /> <input type="button" value="SUM" onClick="sum()"/> <input type="submit" value="Submit" /><br /> </td> </tr> <tr> <td align="left"> <input id="total" name="total" type="text"/> INCOME SUM<br /> <input id="taxtotal" name="taxtotal" type="text"/> Tax SUM with desirable percent for ex: 20% </td> </tr> </table> </form> </body>
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function addrow(){ var tbl = document.getElementById('sheet'); var iteration = tbl.rows.length; var row=tbl.insertRow(iteration); var cellLeft = row.insertCell(0); var textNode = document.createTextNode(iteration); cellLeft.appendChild(textNode); var cellRight = row.insertCell(1); var el = document.createElement('input'); el.type = 'text'; el.name = 'txtRow'+iteration; el.id = 'txtRow'+iteration; el.size = 40; el.value = '0'; el.onblur = sum; cellRight.appendChild(el); var cellRight1 = row.insertCell(2); cellRight1.id = 'txtRowe'+iteration; var cellRightsel = row.insertCell(3); var sel = document.createElement('select'); sel.name = 'selRow' + iteration; sel.id = 'selRow' + iteration; sel.onchange = sum; sel.options[0] = new Option('10%', '10'); sel.options[1] = new Option('20%', '20'); sel.options[2] = new Option('30%', '30'); cellRightsel.appendChild(sel); sum(); } function sum(){ var s1 = 0; var s2 = 0; var tbl=document.getElementById('sheet'); var iteration=tbl.rows.length-1; for(var i=1; i<=iteration; i++){//loop through table rows var el1 = document.getElementById('txtRow'+i);//Row's Income field var el2 = document.getElementById('selRow'+i);//Row's percentage menu var el3 = document.getElementById('txtRowe'+i);//Row's Tax cell if(!el1 || !el2 || !el3) continue; var txt = el1.value; if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries var tax = Number(txt) * Number(el2[el2.selectedIndex].value)/100; el3.innerHTML = tax.toFixed(2); s1 += Number(txt); s2 += tax; } var t1 = document.getElementById('total'); var t2 = document.getElementById('taxtotal'); if(t1){ t1.value = s1.toFixed(2); } if(t2){ t2.value = s2.toFixed(2); } } onload = function(){ addrow(); }
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Sep 2009
Posts: 24
Reputation:
Solved Threads: 0
Hi,
Airshow
I just tried this code so i inputted this code into HTML form
before we created
but nothing are got
I tried it with your suggested HTML code
in this code there are also some missing
so my code are here below
pls see it if possible
may i suggest smth to you may be we create Percent function separate or it is not needed
Airshow
I just tried this code so i inputted this code into HTML form
before we created
but nothing are got
I tried it with your suggested HTML code
in this code there are also some missing
so my code are here below
pls see it if possible
may i suggest smth to you may be we create Percent function separate or it is not needed
•
•
Join Date: Sep 2009
Posts: 24
Reputation:
Solved Threads: 0
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title></title>function addrow(){ var tbl = document.getElementById('sheet'); var iteration = tbl.rows.length; var row=tbl.insertRow(iteration); var cellLeft = row.insertCell(0); var textNode = document.createTextNode(iteration); cellLeft.appendChild(textNode); var cellRight = row.insertCell(1); var el = document.createElement('input'); el.type = 'text'; el.name = 'txtRow'+iteration; el.id = 'txtRow'+iteration; el.size = 40; el.value = '0'; el.onblur = sum; cellRight.appendChild(el); var cellRight1 = row.insertCell(2); cellRight1.id = 'txtRowe'+iteration; var cellRightsel = row.insertCell(3); var sel = document.createElement('select'); sel.name = 'selRow' + iteration; sel.id = 'selRow' + iteration; sel.onchange = sum; sel.options[0] = new Option('10%', '10'); sel.options[1] = new Option('20%', '20'); sel.options[2] = new Option('30%', '30'); cellRightsel.appendChild(sel); sum(); } function sum(){ var s1 = 0; var s2 = 0; var tbl=document.getElementById('sheet'); var iteration=tbl.rows.length-1; for(var i=1; i<=iteration; i++){//loop through table rows var el1 = document.getElementById('txtRow'+i);//Row's Income field var el2 = document.getElementById('selRow'+i);//Row's percentage menu var el3 = document.getElementById('txtRowe'+i);//Row's Tax cell if(!el1 || !el2 || !el3) continue; var txt = el1.value; if(txt != ( '' + Number(txt) )) continue;//reject non-numeric entries var tax = Number(txt) * Number(el2[el2.selectedIndex].value)/100; el3.innerHTML = tax.toFixed(2); s1 += Number(txt); s2 += tax; } var t1 = document.getElementById('total'); var t2 = document.getElementById('taxtotal'); if(t1){ t1.value = s1.toFixed(2); } if(t2){ t2.value = s2.toFixed(2); } } onload = function(){ addrow(); } </script> </head> <body> <form action="miro.html" name="eval_edit" method="post" format="html"> <table align="center" width = "75%"> <tr> <td align = "center"> click add to you know, add a row, and remove to remove a row, and submit to submit your page! whee! </td> </tr> <tr> <td align = "center"> <!--- very imporant to give the table an id ---> <!--- otherwise it won't know where to edit ---> <table border="1" id="mySampleTable"> <tr> <td> Lesson </td> <td> Title </td> <td> Instructor </td> </tr> <tr> <td> 1 </td> <td><input sumMe="1" type="text" name="txtRow1" id="txtRow1" size="40" /></td><tr> <tr> <td><input sumMe1="1" type="text" name="txtRowe1" id="txtRowe1" size="40" /> </td> <td> <select name="selRow0"> <option value="10">10%</option> <option value="20">20%</option> <option value="30">30%</option> </select> </td> </tr> </table> <input name="total" type="text"/> <!--- our buttons call our javascript functions when clicked ---> <input type="button" value="Add" onclick="addRow();" /> <input type="button" value="Remove" onclick="removeRow();" /> <input type="button" value="SUM" onClick="sum()"/> <input type="button" value="SUM1" onClick="sum1()"/> <input type="submit" value="Submit" /> </td> <td><input name="taxtotal" type="text"/></td> </tr> </table> </form> </body> </html>
Azegurb,
Two problems here:
You could do the percent calculation in a separate function if you wish but it's so simple (one line) that it's not really necessary. In my experience, it's best to perform all calculations on every occasion, otherwise there's a chance the whole thing will get out of synchronisation. You should never rely on the user clicking two or more buttons in the right sequence. On modern computers of even very modest performance, javascript runs so fast ther'e no penalty for calculations of this manitude. You would need a hundred rows or more before the user would start to notice the calculation delay.
Airshow
Two problems here:
- There's no opening
<script>tag. - You are trying to run the new javascript with a hybrid of your old <body>...</body> with only some of my new <body>...</body> inserted into it. Try using my <body>...</body> is its entirity.
You could do the percent calculation in a separate function if you wish but it's so simple (one line) that it's not really necessary. In my experience, it's best to perform all calculations on every occasion, otherwise there's a chance the whole thing will get out of synchronisation. You should never rely on the user clicking two or more buttons in the right sequence. On modern computers of even very modest performance, javascript runs so fast ther'e no penalty for calculations of this manitude. You would need a hundred rows or more before the user would start to notice the calculation delay.
Airshow
Last edited by Airshow; Sep 29th, 2009 at 9:02 am.
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- dynamic form with dynamic table...please help (JavaScript / DHTML / AJAX)
- Dynamic Table Creation (JavaScript / DHTML / AJAX)
- dynamic table (PHP)
- Event Handler in dynamic table rows (JavaScript / DHTML / AJAX)
- Dynamic Table name (Oracle)
- Table options (VB.NET)
- pygtk dynamic table (Python)
- Help with Dynamic Checkbox (PHP)
- How will I Capture this Dynamic Data (ASP.NET)
Other Threads in the JavaScript / DHTML / AJAX Forum
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column cookies createrange() css cursor dependent disablefirebug dom download dropdown editor element engine error events explorer ext file form forms google gwt gxt hiddenvalue highlightedword html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onerror onmouseoutdivproblem onreadystatechange parent pdf php player post problem progressbar rated rating regex runtime scroll search security select session shopping size software sql star stars synchronous text textarea unicode validation w3c web website window windowofwords windowsxp wysiwyg xml \n





