surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.12k stars 802 forks source link

Using custom ItemValue properties in CalculatedValues #2925

Closed JeroenJADS closed 7 months ago

JeroenJADS commented 3 years ago

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

Question

What is the current behavior?

When using CalculatedValues like

{"name": "Scoreq1", "expression": {question1}  }

the outcome of this calculatedvalue is automatically the 'value' property of the selected question answer.

In our setup we have a custom property added to itemvalue called 'Score'. Is it possible to instead of always using the value property in a CalculatedValue use the "Score" property of the answer?

Using the "value" property wont suffice, since the value property can't be identical between rows in a question, even though the score of the answers could be identical.

What is the expected behavior?

This could work like

{"name": "Scoreq1", "expression": {question1.score}  }

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

.

Specify your

andrewtelnov commented 3 years ago

@JeroenJADS You have to create a custom function, register it in SurveyJS and then you can reuse it in any expression. Please take a look at "getPrice" at the following example. It is very close to what you are looking for. You will need to change the following code:

var question = !!this.row ? this.row.getQuestionByColumnName(params[0]) : null;

to

var question = !!this.row ? this.row.getQuestionByColumnName(params[0]) : this.question;

Or review another example.

Thank you, Andrew