Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java variables calculation on change (Excel replica)

Status
Not open for further replies.

simasi

Programmer
Oct 1, 2009
20
US
I have a excel sheet that I need to replicate in JSP (using struts 1 framework). Now everything is done but I am stuck at the calculation part. As you might have seen in excel, some columns are calculations based on other columns.
So if I change field 'a', 'd' field should change automatically based on its formula (let's say, d=a*1.1 +b*2.1).

Now I tried to use the funstion in javascript where I read 'a' and 'b' values from JSP, then I calculate and everything works fine. But when I try to assign the calculation result for field 'd' in JSP throush javascript, 'd' does not change.

function adjustCalcs ()
{


var a = eval("document.form.al.value");
var b = eval("document.form.b.value");
var dCalc = ((a * 1.05) + (b * 1.1));


document.form.d.value = dCalc ; (this is where it should change the value of field 'd' in JSP, but ti won't...donno why?)


}

Or is there any better way to do these dynamic changes in JSP? Would appreciate the insight.

Thanks,
 
just a few thoughts :

1. Don't use EVAL unless you have to, google it for some detailed articles on its usage and problems.

2. Can you alert the value of dCalc before you try and populate it.

3. Can you provide a sample output HTML page so we can see what is going on in your HTML.

4. Is your FORM called "form" ?

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
>2. Can you alert the value of dCalc before you try and populate it.

alert for the dCalc comes ok. But assigning part doesn't work. I do not see value of 'd' being changed.

>4. Is your FORM called "form" ?
Yes.

 
Now I read the values from JSP text fields as:

var vala = parseInt(document.getElementById("A").value);
var valb = parseInt(document.getElementById("B").value);

alert(vala);
alert(valb);

var valc=document.getElementById("C");
valc.value = ((vala * 4) + (valb * 2) );


Now the issue here is, there is an onchange even on JSP textfield, so if i change textfield 'A', the value of 'vala'
comes to be an old value but not the changed value. Let's say its value initial was 25 and I changed it to 35, so the onchange event calls this javascript but the value it reads is 25 not 35? What could be the issue here?

 
JSP is server side. Javascript is client side. If you want a server side solution, try going here. Otherwise please provide the html for "d".

-----------------------------------------
I cannot be bought. Find leasing information at
 
>What browser are you using? IE 8

HTML: (JSP actually)
<tr>
<td>
<html:text onkeyup="adjustCalcs()" property="A" />
</td>
</tr>

//Javascript function
function adjustCalcs() {
var vala = parseInt(document.getElementById("A").value);
var valb = parseInt(document.getElementById("B").value);

alert(vala);
alert(valb);

var valc=document.getElementById("C");
valc.value = ((vala * 4) + (valb * 2) );
}

My requirements are if somone chnages this field, 'C' should automatically change because it is a calculation of fields including 'A'.
So initially the value of field 'A' was 25, when I changed it to 35 it calls the function but reads it as 25 not 35.
 
Actually I wanted to see the HTML for d, meaning the client side markup for the value you're trying to change. Not your JSP using tag libraries.

This works...
Code:
<html>
<input type="text" name="A" id="A" value="2" onkeyup="updateC()"/>
<input type="text" name="B" id="B" value="4" onkeyup="updateC()"/>
<input type="text" name="C" id="C" value="3.14159"/>
<script type="text/javascript">
function updateC(){
var a = document.getElementById('A').value;
var b = document.getElementById('B').value;
var c = (a*2) + (b*3);
document.getElementById('C').value = c;
}
</script>

</html>

-----------------------------------------
I cannot be bought. Find leasing information at
 
Right, my code also works but the issue is that the value of 'A' which I read is the old one(25) not the changed one(35).
But surprisingly the changed value of 'B' is being read correctly. Confusing? browser issue?
 
Not confusing. Probably not a browser issue. My guess is its something simple. But since you're not providing the HTML, I can't say for sure.

-----------------------------------------
I cannot be bought. Find leasing information at
 
:) I am not using any HTML. The code I sent you is all I am using in JSP which in turns get converted to HTML usually.

<html>
<table>
<tr>
<td>
<html:text onkeyup="adjustCalcs()" property="A" />
</td>
</tr>
</table>
</html> ....like this.
 
Most browsers have a view source option that shows the html created by the JSP. Does yours?

-----------------------------------------
I cannot be bought. Find leasing information at
 
<table bgcolor="#FFFFCC" id="tblGrid" align="left" border="1px" width="250px" style="position:relative">
<thead>
<tr bgcolor="#ababab">
<th bgcolor="#ababab" align="center" valign="middle" colspan="2" rowspan="2">
A Location Costs
</th>
</tr>
</thead>
<tr>
<td>A LOC:</td>
<td >
<input type="text" name="A" tabindex="2" value="25" onblur="adjustCalcs()">
</td>
</tr>
</table>
 
Try this and see if it works:

<input type="text" name="A" id="A"tabindex="2" value="25" onblur="adjustCalcs()">
 
Burt since this is autgenerated from JSP, I cannot change it manually, is there any thing I can do in jsp?

in JSP:
<html:text onkeyup="adjustCalcs()" property="A" />

getc onverted to HTML:
<input type="text" name="A" tabindex="2" value="25" onblur="adjustCalcs()">
 
Oh! Forgot the JSP part:

This is a struts tag, right? How about this then?

<html:text onkeyup="adjustCalcs()" property="A" styleId="A"/>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top