Custom JS count sum (€)

Forums Classic DFFS Custom JS count sum (€)

Viewing 7 reply threads
  • Author
    Posts
    • #28340
      Steve
      Participant

      Hi guys,
      I need some help with the dynamic form.

      I have 5 products (5 fields) and linked prices (other 5 fields). If a user fills in a column of the product (he wants 2 pieces of croissant for example) another field show (0,7 €). If he changes his mind and fills in only one piece of croissant – another column shows (0,35 €).

      So now, I have a button “count €” – and I need it to be validated when the five columns with pieces changes. It works when I use – If this trigger – form is saved. But I would like to have a button to be working when user clicks on it to show sum (total price).

      Attachment – yellow boxes are invisible price fields.
      The last yellow invisible yellowed box should show the total price if a user clicks on it.

      Thank for your advice.
      Steo

      Attachments:
    • #28348
      Alexander Bautz
      Keymaster

      Hi,
      See if you can use the method described here to do this with custom js: https://spjsblog.com/forums/topic/dffs-sum-multiple-fields-in-a-totals-field/

      Alexander

    • #28382
      Steve
      Participant

      Hi Alex,
      I tried that and it worked perfectly. Then I made “tot” read-only and customized the JS function and it also worked followed by your instructions. But I need to sum 5 columns summed in “tot” column. So I have created 5 columns. “sumAll” is a column where it adds up.

      Here is my code:

      
      
      jQuery("#dffs__x0043_hK1000 input, #dffs__x0043_hK450 input,#dffs_RozStan50 input, #dffs__RozGrah60 input, #dffs_Vian350 input").on("keyup",function(){
          var str = jQuery(this).val().replace( /[^0-9.]/g, "" );
          jQuery(this).val(str);
          setFinalCost();
      });
      
      function setFinalCost(){
      	var a, b, c, d, e, f, aa, bb, cc, dd, ee, ff;
      	a = Number($("#dffs__x0043_hK1000 input").val());
      	b = Number($("#dffs__x0043_hK450 input").val());
      	c = Number($("#dffs_RozStan50 input").val());
      	d = Number($("#dffs_RozGrah60 input").val());
      	e = Number($("#dffs_Vian350 input").val());
      	f = Number($("#dffs_sumAll input").val());
      	aa = !isNaN(a) ? a : 0;
      	bb = !isNaN(b) ? b : 0;
      	cc = !isNaN(c) ? c : 0;
      	dd = !isNaN(d) ? c : 0;
      	ee = !isNaN(e) ? c : 0;
      	ff = !isNaN(f) ? c : 0;
      	
      	if(isNaN(a)){
      		alert("Number format error in \""+spjs.dffs.fieldData._x0043_hK1000.disp+"\"");
      	}
      	if(isNaN(b)){
      		alert("Number format error in \""+spjs.dffs.fieldData._x0043_hK450.disp+"\"");
      	}
      	if(isNaN(c)){
      		alert("Number format error in \""+spjs.dffs.fieldData.RozStan50.disp+"\"");
      	}
      	if(isNaN(d)){
      		alert("Number format error in \""+spjs.dffs.fieldData.RozGrah60.disp+"\"");
      	}
      	if(isNaN(e)){
      		alert("Number format error in \""+spjs.dffs.fieldData.Vian350.disp+"\"");
      	}
      	if(isNaN(f)){
      		alert("Number format error in \""+spjs.dffs.fieldData.sumAll.disp+"\"");
      	}
      	setFieldValue("Tot",aa+bb+cc+dd+ee+ff);
      	setTimeout(function(){
      		$("input.fieldValueUpdated").removeClass("fieldValueUpdated");
      	},1000);
      }

      Thank you in advance.
      Steo

    • #28391
      Alexander Bautz
      Keymaster

      First you have some errors in the code – when you assign the value to dd, ee and ff you use c and not d, e and f – change it like this:

      aa = !isNaN(a) ? a : 0;
      bb = !isNaN(b) ? b : 0;
      cc = !isNaN(c) ? c : 0;
      dd = !isNaN(d) ? d : 0;
      ee = !isNaN(e) ? e : 0;
      ff = !isNaN(f) ? f : 0;

      I’m not sure I understand what you are asking – what is it that doesn’t work?

      Alexander

    • #28400
      Steve
      Participant

      Thank you Alex for pointing out the errors.
      Now, it works perfectly.
      Steo

    • #28406
      Steve
      Participant

      The last thing, I cannot save the “item”, because of the value, for example: “0.7“, but in Czech, we have the value with a decimal point (comma) “0,7“. Can you help me?
      Otherwise, it works perfectly.

      Steo

      Attachments:
    • #28409
      Steve
      Participant

      Well, all columns have been set as “Number”. I set them as “one line of text” and it works out.

      • This reply was modified 4 years, 3 months ago by Steve.
    • #28412
      Alexander Bautz
      Keymaster

      The reason for this is that JavaScript uses a dot as decimal separator. If you need it to be a number in your locale format you can change your code like this:

      var tot = (aa+bb+cc+dd+ee+ff).toLocaleString(_spPageContextInfo.currentCultureName);
      setFieldValue("Tot",tot);

      Or if that does not work (old IE versions cannot use this method) you can use this method:

      var tot = String(aa+bb+cc+dd+ee+ff).split(".").join(",");
      setFieldValue("Tot",tot);

      Alexander

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