用element ui渲染问卷,问卷包括单选多选和判断,请给我一个模板布局
时间: 2023-06-07 11:08:00 浏览: 179
当然,我可以帮您提供一个Element UI渲染问卷的模板布局。请参考以下代码:
```html
<template>
<div class="questionnaire-container">
<el-form :model="form" ref="form" :rules="rules">
<div v-for="(question, index) in questionnaire.questions" :key="question.id" class="question">
<p class="question-title">{{index + 1}}. {{question.title}}</p>
<el-form-item :prop="question.id">
<el-radio-group v-if="question.type === 'single-select'" v-model="form[question.id]">
<el-radio v-for="option in question.options" :key="option.id" :label="option.value">{{option.label}}</el-radio>
</el-radio-group>
<el-checkbox-group v-if="question.type === 'multi-select'" v-model="form[question.id]">
<el-checkbox v-for="option in question.options" :key="option.id" :label="option.value">{{option.label}}</el-checkbox>
</el-checkbox-group>
<el-select v-if="question.type === 'judge'" :placeholder="question.placeholder" v-model="form[question.id]">
<el-option label="是" value="true"></el-option>
<el-option label="否" value="false"></el-option>
</el-select>
</el-form-item>
</div>
<el-button type="primary" @click="submitForm()">提交</el-button>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
form: {},
rules: {},
questionnaire: {
id: 1,
questions: [
{
id: 'q1',
title: '单选题示例',
type: 'single-select',
options: [
{id: 'o1', label: '选项1', value: '1'},
{id: 'o2', label: '选项2', value: '2'},
{id: 'o3', label: '选项3', value: '3'},
{id: 'o4', label: '选项4', value: '4'}
]
},
{
id: 'q2',
title: '多选题示例',
type: 'multi-select',
options: [
{id: 'o5', label: '选项5', value: '5'},
{id: 'o6', label: '选项6', value: '6'},
{id: 'o7', label: '选项7', value: '7'},
{id: 'o8', label: '选项8', value: '8'}
]
},
{
id: 'q3',
title: '判断题示例',
type: 'judge',
placeholder: '请选择',
}
]
}
}
},
methods: {
submitForm() {
this.$refs.form.validate((valid) => {
if (valid) {
console.log(this.form);
} else {
console.log('error submit!!');
return false;
}
});
}
}
}
</script>
<style scoped>
.questionnaire-container {
max-width: 800px;
margin: 0 auto;
}
.question {
margin-bottom: 20px;
}
.question-title {
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
</style>
```
以上模板布局使用了Element UI提供的表单组件,包括单选框、多选框、下拉选择框。根据您的需求,可以在此基础上进行相应的修改和调整。
阅读全文