Survey-library: Only use visible values in the expression calculation?

Created on 30 Oct 2018  路  4Comments  路  Source: surveyjs/survey-library

Are you requesting a feature, reporting a bug or asking a question?

Question / Feature?

What is the current behavior?

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.

What is the expected behavior?

Ignore hidden fields?

How would you reproduce the current behavior (if this is a bug)?

See the code below (copy paste it in the survey builder).

  1. Fill in question A: 20
  2. Fill in question B: 10
  3. You now see that the sum of those fields is 30, which is correct.
  4. If you change question A to a value of 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?

Provide the test code and the tested page URL (if applicable)

{
 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}"
}
enhancement question

All 4 comments

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.

Was this page helpful?
0 / 5 - 0 ratings