Calculating Total from multiple fields

Forums Classic DFFS Calculating Total from multiple fields

Viewing 7 reply threads
  • Author
    Posts
    • #21051
      mk3jamie
      Participant

      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!

    • #21062
      mk3jamie
      Participant

      **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

    • #21064
      Alexander Bautz
      Keymaster

      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

    • #21076
      mk3jamie
      Participant

      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);
      }
      • This reply was modified 5 years, 10 months ago by mk3jamie.
      • This reply was modified 5 years, 10 months ago by mk3jamie.
      • This reply was modified 5 years, 10 months ago by mk3jamie.
    • #21081
      Alexander Bautz
      Keymaster

      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

    • #21095
      mk3jamie
      Participant

      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?

    • #21118
      Alexander Bautz
      Keymaster

      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

    • #21126
      mk3jamie
      Participant

      Perfect Alex, that’s done the trick 🙂

      Your a gentleman and a scholar!

Viewing 7 reply threads
  • You must be logged in to reply to this topic.