Home › Forums › Classic DFFS › Custom calculated field in DFFS
- This topic has 0 replies, 1 voice, and was last updated 9 years, 9 months ago by Alexander Bautz.
Viewing 0 reply threads
-
AuthorPosts
-
-
March 21, 2015 at 09:10 #7298
Here is an example on how to build a custom calculation in DFFS. Add this code to the Custom JS.
(function(){ // Array of FieldInternalNames that are part of the calculation var arrOfFieldsToCalculate = ["CustomCalc1","CustomCalc2","CustomCalc3","CustomCalc4"]; // Formula to use for the calculation. You can use any mathematical expression. // Please note that the evaluation is done with "eval()". Therefore you must use choice columns and not free text input. var formula = "CustomCalc1*10+CustomCalc2*20+CustomCalc3*30+CustomCalc4*40"; // FieldInternalName to write the output to var calcResultField = "CustomCalcResult"; // Label shown when the caculation is incomplete var label = "You must fill all fields to calculate the value in this field."; // Add message spjs.$("#dffs_"+calcResultField).find("input").hide().after("<span></span><div style='color:red' id='customCalculationMsg_"+calcResultField+"'>"+label+"</div>"); // Add change function spjs.$.each(arrOfFieldsToCalculate,function(i,f){ $("#dffs_"+f).find("select").change(function(){ doCustomCalculation(formula); }); }); // Do calculation var doCustomCalculation = function(fx){ var allFilled = true, val, vObj = {}, ev; spjs.$.each(arrOfFieldsToCalculate,function(i,f){ val = getFieldValue(f); if(val === ""){ allFilled = false; return false; }else{ vObj[f] = val; } }); if(allFilled){ spjs.$.each(vObj,function(fin,v){ fx = fx.replace(new RegExp(fin,"g"),v); }); try{ ev = eval(fx); spjs.$("#customCalculationMsg_"+calcResultField).hide(); spjs.$("#dffs_"+calcResultField).find("input").val(ev).next().html(ev); }catch(err){ ev = err; spjs.$("#customCalculationMsg_"+calcResultField).hide(); spjs.$("#dffs_"+calcResultField).find("input").val("").next().html("<span style='color:red;'>"+ev+"</span>"); } }else{ spjs.$("#customCalculationMsg_"+calcResultField).show(); spjs.$("#dffs_"+calcResultField).find("input").val("").next().html(""); } } })();
The requirements for this calculation to work, is that the fields used in the calculation is choice columns (dropdown or radio button) with numbers.
You must change the variables in the top of the code snippet to match your field names, change the formula and add a custom label.
When the calculation is performed, the result is written to the single line of text field in the variable “calcResultField”.
If you want to add it to the CEWP and not in the Custom JS, you must wrap the function in the “dffs_Ready()” function like this:
function dffs_Ready(){ // add the code here }
Let me know how this works out,
Alexander
-
-
AuthorPosts
Viewing 0 reply threads
- You must be logged in to reply to this topic.