Currently, I am facing an issue with the template code for my survey builder. I am successfully receiving responses from the server, but the addAnotherQuestion value is not updating as expected. Despite trying various approaches, I have been unable to resolve this issue. Any assistance in resolving this problem would be greatly appreciated.
<div v-for="(addItem, addItemIndex) in addAnotherQuestion" :key="addItemIndex">
{{addItem.question}}
{{addItem.options}}
</div>
This is the default value set:
data() {
return {
addAnotherQuestion: [
{
id: 1,
enableSingle: false,
enableMultiple: false,
enableLong: false,
enableScale: false,
enableMatrix: false,
enableMatrixMulti: false,
enablePlain: false,
type: "single",
// options: ["", ""],
question_number: 1,
disableOption: true,
question: "",
required: true,
hasOther: false,
isOpen: false,
scale: '0',
rows: [
{
label : "ROW 1",
id : 1
},
{
label : "ROW 2",
id : 2
},
],
columns: [
{
label : "COL 1",
id : 1
},
{
label : "COL 2",
id : 2
},
],
},
],
}
mounted(){
this.getSurveyQuestion();
}
computed:{ ...mapGettersSurvey(["questionList"])},
methods:{
...mapActionsSurvey(["GET_SURVEY_QUESTION"]),
getSurveyQuestion() {
let params = { id: this.surveyId };
let isDetailsFound=false;
this.GET_SURVEY_QUESTION(params).then((result) => {
console.log('result', result)
if (result) {
//this is just a response from server not a code
[
{
"id": 1,
"question": "Test question -1",
"question_number": 1,
"options": [
"Option 1",
"Option 2",
"Option 3"
],
"required": true,
"hasOther": false,
"type": "single"
},
{
"id": 2,
"question": "Test Question -2",
"question_number": 2,
"options": [
"Option -1",
"Option -2"
],
"required": true,
"hasOther": false,
"type": "multiple_choice"
}
]
//response over--this response is from server
let details = this.questionList;
if (details.length > 0) {
isDetailsFound = true;
for (let i = 0; i < details.length; i++) {
if (details[i].type === "single") {
details[i].enableSingle = true;
} else if (details[i].type === "multiple_choice") {
details[i].enableMultiple = true;
} else if (details[i].type === "open_text") {
details[i].enableLong = true;
} else if (details[i].type === "distribution_scale") {
details[i].enableScale = true;
} else if (details[i].type === "single_variant") {
details[i].enableMatrix = true;
} else if (details[i].type === "multiple_variant") {
details[i].enableMatrixMulti = true;
} else if (details[i].type === "plain_text_number") {
details[i].enablePlain = true;
}
}
this.addAnotherQuestion = details;
}
}
if(!isDetailsFound){
this.addAnotherQuestion[0].enableSingle = true;
}
});
}
}
The issue lies with the options which are not updating correctly. https://i.sstatic.net/kCVnc.png
I have attempted the following approach:
if (details.length > 0) {
let temp = this.addAnotherQuestion[0];
let addAnotherQuestion2 = [];
console.log('addQ',this.addAnotherQuestion);
isDetailsFound = true;
for (let i = 0; i < details.length; i++) {
let resultQuestion = temp;
console.log('temp',temp);
if (details[i].type === "single") {
resultQuestion.id = details[i].id;
resultQuestion.question_number = details[i].question_number
resultQuestion.enableSingle = true;
console.log(details[i].question);
resultQuestion.question = details[i].question;
resultQuestion.options = details[i].options;
resultQuestion.type = details[i].type;
resultQuestion.enableMultiple = false;
resultQuestion.enableLong = false;
resultQuestion.enableScale = false;
resultQuestion.enableMatrix = false;
resultQuestion.enableMatrixMulti = false;
resultQuestion.enablePlain = false;
resultQuestion.disableOption = true,
resultQuestion.required = details[i].required;
resultQuestion.hasOther = details[i].hasOther;
resultQuestion.isOpen = false;
addAnotherQuestion2.push(resultQuestion);
} else if (details[i].type === "multiple_choice") {
resultQuestion.id = details[i].id;
resultQuestion.question_number = details[i].question_number
resultQuestion.enableSingle = false;
resultQuestion.question = details[i].question;
console.log(details[i].question);
resultQuestion.options = details[i].options;
resultQuestion.type = details[i].type;
resultQuestion.enableMultiple = true;
resultQuestion.enableLong = false;
resultQuestion.enableScale = false;
resultQuestion.enableMatrix = false;
resultQuestion.enableMatrixMulti = false;
resultQuestion.enablePlain = false;
resultQuestion.disableOption = true,
resultQuestion.required = details[i].required;
resultQuestion.hasOther = details[i].hasOther;
resultQuestion.isOpen = false;
addAnotherQuestion2.push(resultQuestion);
}
this.addAnotherQuestion.push(addAnotherQuestion2);