i would like to add a custom property 'points' to a survey , but i when i try to get the value i get undefined returned, am i missing something. i just want to add all the points earned in a survey for a score at the end.
adding the custom property with a default of 0
Survey.JsonObject.metaData.addProperty("itemvalue", { name: "points", default: "0" });
survey.onValueChanged.add(function (sender, question) {
var thepoints = question.points;
});
Hello,
As described in the documentation (https://surveyjs.io/Documentation/Library/?id=surveymodel#onValueChanged)
So you need to use
options.question.points
:Survey.JsonObject.metaData.addProperty("question", { name: "points", default: "0" }); survey.onValueChanged.add(function (sender, options) { var thepoints = options.question.points; console.log("thepoints: " + thepoints); });
This works ok for me in this plunk - https://plnkr.co/edit/zKLFjKHGeHeYu08mO9sV?p=preview
Thanks, Serge
SurveyJS Team
this still seems to return undefined. even if i add a points property to question this specific one is itemvalue.
my JSON is populated with the points etc… this is what it looks like, but the points are on any type itemvalue
"choices": [{
"value": "Blue",
"text": "Blue",
"points": 5
}, {
"text": "Red",
"value": "Red",
"points": 1
}],
Phil,
You have to add "points" to itemvalue object and not a question:
Survey.JsonObject.metaData.addProperty("itemvalue", { name: "points", default: "0" });
You will be able to edit this property in the Editor.
Thank you,
Andrew
SurveyJS Team
thanks andrew, just need to be able to collect all the points together i can access the points using var thepoints = options.question.choices[0].points but needs to be the points values that have been selected
Here is the code:
var points = 0; var val = options.question.value; var choices = options.questions.choices; for(var i = 0; i < choices.length; i ++) { if(choices[i].value == val) { points = choices[i].points; break; } }
Why can't you set your points to the value directly?
Thank you,
Andrew
SurveyJS Team
Phil,
You can count total score in the
onComplete
event, here is working sample:https://plnkr.co/edit/zKLFjKHGeHeYu08mO9sV?p=preview
The code:
survey .onComplete .add(function (result) { var totalScore = result.getAllQuestions().reduce(function(total, q) { if(Array.isArray(q.choices)) { return q.choices.reduce(function(s, c) { if(q.value.indexOf(c.value) !== -1) { s += c.points } return s; }, total); } return total; }, 0); console.log(totalScore); });
Thanks, Serge
SurveyJS Team
Superb Serge, works perfect thank you so much to you and andrew for your help on this.