序号动态改变

在问卷调查中,常常根据前一题的选择情况来动态渲染后续题目。序号也要相应变化。

1
2
3
<p>
{{getIndex(0)}}.{{allNaireList[0].title}}
</p>

data:

1
nowIndexArr: [] // 存储序号

created:

1
2
3
4
// 开始时默认全显示
for (let i = 1; i <= 22; i++) {
this.nowIndexArr.push(true);
}

methods:

1
2
3
4
5
6
7
8
9
10
// 取序号,即取当前项在数组汇中,前面的true的个数
getIndex(index) {
let count = 0;
for (let i = 0; i <= index; i++) {
if (this.nowIndexArr[i]) {
count++;
}
}
return count;
},

watch:

1
2
3
4
5
6
7
8
// 监听某一题不显示时,将该题在数组中的状态由true改为false
showQue2(val) {
if (!val) {
this.nowIndexArr.splice(1, 1, false);
} else {
this.nowIndexArr.splice(1, 1, true);
}
},