Home › Forums › Classic DFFS › Custom JS count sum (€)
Tagged: grand total, JS sum, sum, validate fields
- This topic has 7 replies, 2 voices, and was last updated 4 years, 10 months ago by Alexander Bautz.
-
AuthorPosts
-
-
January 22, 2020 at 11:40 #28340
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.
SteoAttachments:
-
January 22, 2020 at 17:37 #28348
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
-
January 27, 2020 at 19:46 #28382
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 -
January 27, 2020 at 20:42 #28391
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
-
January 27, 2020 at 21:18 #28400
Thank you Alex for pointing out the errors.
Now, it works perfectly.
Steo -
January 28, 2020 at 10:09 #28406
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:
-
January 28, 2020 at 13:43 #28409
-
January 28, 2020 at 19:34 #28412
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
-
-
AuthorPosts
- You must be logged in to reply to this topic.