javascript - Better way to Add fields up and display total -
i have xpage(see below), shows several rows number fields , 3 columns (regular, overtime, total).
i have code on each field under regular , overtime (onblur):
var reg1 = parsefloat(document.getelementbyid("#{id:reg1}").value) || 0; var over1 = parsefloat(document.getelementbyid("#{id:over1}").value) || 0; var total1 = reg1 + over1; document.getelementbyid("#{id:total1}").value = total1; document.getelementbyid("#{id:total1}").disabled = true
eventually, i'll have more rows need add. best way add them can show total regular column , total overtime column?
any comments on code above appreciated.
see xpage:
<?xml version="1.0" encoding="utf-8"?> <xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xe="http://www.ibm.com/xsp/coreex"> <xp:this.data> <xp:dominodocument var="document1" formname="testhrs"></xp:dominodocument> </xp:this.data> <xp:table style="width:383.0px"> <xp:tr> <xp:td>regular</xp:td> <xp:td>overtime</xp:td> <xp:td>total</xp:td> </xp:tr> <xp:tr> <xp:td> <xe:djnumbertextbox id="reg1" value="#{document1.rghr1}"> <xe:this.onblur><![cdata[var reg1 = parsefloat(document.getelementbyid("#{id:reg1}").value) || 0; var over1 = parsefloat(document.getelementbyid("#{id:over1}").value) || 0; var total1 = reg1 + over1; document.getelementbyid("#{id:total1}").value = total1; document.getelementbyid("#{id:total1}").disabled = true]]></xe:this.onblur> </xe:djnumbertextbox> </xp:td> <xp:td> <xe:djnumbertextbox id="over1" value="#{document1.ovhr1}"> <xe:this.onblur><![cdata[var reg1 = parsefloat(document.getelementbyid("#{id:reg1}").value) || 0; var over1 = parsefloat(document.getelementbyid("#{id:over1}").value) || 0; var total1 = reg1 + over1; document.getelementbyid("#{id:total1}").value = total1; document.getelementbyid("#{id:total1}").disabled = true]]></xe:this.onblur> </xe:djnumbertextbox> </xp:td> <xp:td> <xe:djnumbertextbox id="total1" value="#{document1.tohr1}"> </xe:djnumbertextbox> </xp:td> </xp:tr> <xp:tr> <xp:td> <xe:djnumbertextbox id="reg2" value="#{sessionscope.reg2}"> <xe:this.onblur><![cdata[var reg2 = parsefloat(document.getelementbyid("#{id:reg2}").value) || 0; var over2 = parsefloat(document.getelementbyid("#{id:over2}").value) || 0; var total2 = reg2 + over2; document.getelementbyid("#{id:total2}").value = total2; document.getelementbyid("#{id:total2}").disabled = true]]></xe:this.onblur> </xe:djnumbertextbox> </xp:td> <xp:td> <xe:djnumbertextbox id="over2" value="#{sessionscope.over2}"> <xe:this.onblur><![cdata[var reg2 = parsefloat(document.getelementbyid("#{id:reg2}").value) || 0; var over2 = parsefloat(document.getelementbyid("#{id:over2}").value) || 0; var total2 = reg2 + over2; document.getelementbyid("#{id:total2}").value = total2; document.getelementbyid("#{id:total2}").disabled = true]]></xe:this.onblur> </xe:djnumbertextbox> </xp:td> <xp:td> <xe:djnumbertextbox id="total2" value="#{sessionscope.tot2}"> </xe:djnumbertextbox> </xp:td> </xp:tr> </xp:table>  total regular                                       total overtime<xp:br></xp:br> <xe:djnumbertextbox id="totalregular"></xe:djnumbertextbox>               <xe:djnumbertextbox id="totalover"></xe:djnumbertextbox></xp:view>
if need sum several inputs, maybe set same css class them, , use css selector of them , maths:
<xe:djnumbertextbox styleclass="reg"></xe:djnumbertextbox>
then can use csjs function this:
// i.e. pass function '.reg' function getsumbyclass(strclassname) { var sum = 0; var allinputs = dojo.query(strclassname); (var = 0, l = allinputs.length; < l; i++) { sum += parsefloat(allinputs[i].value); } return sum; }
also looking @ code, better layout use css padding / margin properties instead of several spaceblank html chars.
Comments
Post a Comment