Question / Feature?
If you have an expression that sums two values but one of the values is hidden it stills counts the value. Is there an option to say: only use visible values in the expression otherwise "0". I am aware of the "clearInvisibleValues" option but that has a negative impact on other form fields in my case.
Ignore hidden fields?
See the code below (copy paste it in the survey builder).
201030, which is correct. 5. The sum becomes 15 (5 + the hidden 10 of B).
Is this expected behavior, if so. Is there a way to ignore the hidden fields in the expression?
{
pages: [
{
name: "page1",
elements: [
{
type: "text",
name: "questiona",
title: "Question A"
},
{
type: "text",
name: "questionb",
visibleIf: "{questiona} > 10",
title: "Question B"
},
{
type: "expression",
name: "expression",
expression: "{questiona} + {questionb}"
}
]
}
],
showQuestionNumbers: "off",
questionTitleLocation: "left",
questionTitleTemplate: "{title} {require}"
}
I'm not sure. This can be overcomed by more complex expression. Or custom function. @andrewtelnov @dmitrykurmanov what do you think?
@kaphert, @tsv2013 iif function works great here. Here is the example
Thank you,
Andrew
@andrewtelnov thank you. I didn't knew this existed. In my case the expression is a little bit more complex and could exists of 20 integer values. Based on other form input each integer field will be visible or not. So creating an if else structure here would be pretty complicated I am afraid.
I think I have found an alternative solution that would work perfect in my case with the combination of the visibilityChanged event listener and a custom property on the question that states if the value should be cleared if hidden (same idea as the clearInvisibleValues = onHidden but than on the level of the question.
Working example: https://plnkr.co/edit/w9Jq92ql120291kuGPgk?p=preview
// register custom property
Survey.JsonObject.metaData.addProperty('questionbase', 'clearOnHidden');
// listen for visibility changes
survey.onVisibleChanged.add(function(sender, options){
if (!options.visible && options.question.clearOnHidden) {
survey.clearValue(options.name);
console.log('cleared value for: ', options.name);
}
});
And than add the property to the definition of the question:
{
type: "text",
name: "questionb",
visibleIf: "{questiona} > 10",
clearOnHidden: true,
title: "Question B"
}
I am closing this ticket, I think above solution would be a fine work around for anyone needing a similar solution.