Home › Forums › Classic DFFS › Calculating Total from multiple fields
- This topic has 8 replies, 2 voices, and was last updated 6 years, 5 months ago by Alexander Bautz.
-
AuthorPosts
-
-
June 1, 2018 at 17:33 #21051
Hi Alex,
You previously helped me with some custom JS in DFFS that helped to calculate the total value of multiple fields that had been filled in.
function setFinalCost(){ var a, b, c, aa, bb, cc; a = Number($("#dffs_Add_x0020_New_x0020_Steel_x0020_ input").val()); b = Number($("#dffs_Connection_x0020_Design input").val()); c = Number($("#dffs_Connection_x0020_Detailing input").val()); aa = !isNaN(a) ? a : 0; bb = !isNaN(b) ? b : 0; cc = !isNaN(c) ? c : 0; if(isNaN(a)){ alert("Number format error in \""+spjs.dffs.fieldData.Add_x0020_New_x0020_Steel_x0020_.disp+"\""); } if(isNaN(b)){ alert("Number format error in \""+spjs.dffs.fieldData.Connection_x0020_Design.disp+"\""); } if(isNaN(c)){ alert("Number format error in \""+spjs.dffs.fieldData.Connection_x0020_Detailing.disp+"\""); } $("#dffs_VO_x0020_Planned_x0020_Hours input").addClass("fieldValueUpdated").val(aa+bb+cc); setTimeout(function(){ $("input.fieldValueUpdated").removeClass("fieldValueUpdated"); },1000); }
I’m hoping you can help me with an IF Statement. Basically when i’m adding all the values to the single field at the end, i’d like to be able to add them to two different fields depending on the value in a Status field.
s = String($("#dffs_Issue_x0020_Status input").val()); if(s=="Tender"){ $("#dffs_VO_x0020_Planned_x0020_Hours input").addClass("fieldValueUpdated").val(aa+bb+cc+dd+ee+ff+gg+hh+ii+jj+kk+ll+mm+nn); setTimeout(function(){ $("input.fieldValueUpdated").removeClass("fieldValueUpdated"); },1000); } /*else { $("#dffs_VO_x0020_Actual_x0020_Hours input").addClass("fieldValueUpdated").val(aa+bb+cc+dd+ee+ff+gg+hh+ii+jj+kk+ll+mm+nn); setTimeout(function(){ $("input.fieldValueUpdated").removeClass("fieldValueUpdated"); },1000); }
Basically doesn’t execute and total the values, hopefully you can help!
-
June 4, 2018 at 15:32 #21062
**Just an edit!
The syntax error in my code above is just from my copy and paste, forgot to remove it!
” /*else { ”
So ignore that comment line :p
-
June 4, 2018 at 17:53 #21064
Hi,
I have used your code and added an if-statement that sets the value in one field is the status is “Tender”, and in another if it is anything else – look at it and see if it gets you back on track:function setFinalCost() { var s, a, b, c, aa, bb, cc; a = Number($("#dffs_Add_x0020_New_x0020_Steel_x0020_ input").val()); b = Number($("#dffs_Connection_x0020_Design input").val()); c = Number($("#dffs_Connection_x0020_Detailing input").val()); aa = !isNaN(a) ? a : 0; bb = !isNaN(b) ? b : 0; cc = !isNaN(c) ? c : 0; if (isNaN(a)) { alert("Number format error in \"" + spjs.dffs.fieldData.Add_x0020_New_x0020_Steel_x0020_.disp + "\""); } if (isNaN(b)) { alert("Number format error in \"" + spjs.dffs.fieldData.Connection_x0020_Design.disp + "\""); } if (isNaN(c)) { alert("Number format error in \"" + spjs.dffs.fieldData.Connection_x0020_Detailing.disp + "\""); } // Find Status s = $("#dffs_Issue_x0020_Status input").val(); if(s === "Tender"){ // Set value for Tender $("#dffs_FIRST_FIELD_NAME input").addClass("fieldValueUpdated").val(aa + bb + cc); }else{ // Set value for all other status values $("#dffs_SECOND_FIELD_NAME input").addClass("fieldValueUpdated").val(aa + bb + cc); } setTimeout(function () { $("input.fieldValueUpdated").removeClass("fieldValueUpdated"); }, 1000); }
Alexander
-
June 4, 2018 at 23:03 #21076
Still having issues Alex, no doubt something silly!
$("#dffs_NumField1 input, #dffs_NumField2 input, #dffs_NumField3 input").change(function(){ setFinalCost(); }); function setFinalCost(){ var a,b,c,s,aa,bb,cc; a = Number($("#dffs_Add_x0020_New_x0020_Steel_x0020_ input").val()); b = Number($("#dffs_Connection_x0020_Design input").val()); c = Number($("#dffs_Connection_x0020_Detailing input").val()); aa = !isNaN(a) ? a : 0; bb = !isNaN(b) ? b : 0; cc = !isNaN(c) ? c : 0; if(isNaN(a)){ alert("Number format error in \""+spjs.dffs.fieldData.Add_x0020_New_x0020_Steel_x0020_.disp+"\""); } if(isNaN(b)){ alert("Number format error in \""+spjs.dffs.fieldData.Connection_x0020_Design.disp+"\""); } if(isNaN(c)){ alert("Number format error in \""+spjs.dffs.fieldData.Connection_x0020_Detailing.disp+"\""); } s = $("#dffs_Issue_x0020_Status input").val(); if(s === "Tender"){ // Set value for Tender $("#dffs_VO_x0020_Planned_x0020_Hours input").addClass("fieldValueUpdated").val(aa + bb + cc); }else{ // Set value for all other status values $("#dffs_VO_x0020_Actual_x0020_Hours input").addClass("fieldValueUpdated").val(aa + bb + cc); } setTimeout(function () { $("input.fieldValueUpdated").removeClass("fieldValueUpdated"); }, 1000); }
-
June 6, 2018 at 08:34 #21081
Hi,
Do you get an error message, or is just nothing happening?You may be able to find out where it fails by inserting a console.log or typing in the text debugger; in the top of the function and use the developer tools to step through the code.
Alexander
-
June 6, 2018 at 23:15 #21095
Shockingly it was a syntax error on my part! It’s working perfectly on the New Form but i’m having trouble with the Edit Form.
So when “Task” is selected it will add the hours to the Actual hours. When tender is selected, it will add the hours to the Planned hours.
When i edit an item, if i change from Task to Tender or vice versa, the if statement still reads the initial value as the value being selected, and therefore adds the hours to the previous total field.
So like the above image, if I switched it to “Tender” i want to now add the hours to the Planned Hours total.
The hours don’t add to Planned, the still add to Actual.
Is there a way for me to be able to reset the function each time the VO Status field is changed?
-
June 7, 2018 at 19:45 #21118
You can call this function on change of the status field like this – change STATUS_FIELD with your FieldInternalName:
jQuery("#dffs_STATUS_FIELD select").change(function(){ setFinalCost(); });
Alexander
-
June 7, 2018 at 22:31 #21126
Perfect Alex, that’s done the trick 🙂
Your a gentleman and a scholar!
-
June 8, 2018 at 16:20 #21134
I’m glad we got it sorted out!
Alexander
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.