console
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app" style="width: 60%">
<h1>Vue 设置自定义属性演示</h1>
<el-form label-width="80px">
<el-form-item v-for="attr in attrs" :label="attr.name">
<el-input v-if="attr.type === 'TEXT'" v-model="attr.setValues[0]"></el-input>
<el-select v-if="attr.type === 'SINGLE_CHOICE'" v-model="attr.setValues[0]">
<el-option v-for="value in attr.values" :key="value.key" :label="value.name" :value="value.key">
</el-option>
</el-select>
<el-checkbox-group v-if="attr.type === 'MULTI_CHOICE'" v-model="attr.setValues">
<el-checkbox v-for="value in attr.values" :label="value.key">{{ value.name }}</el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="save">保存</el-button>
</el-form-item>
</el-form>
</div>
</body>
<script src="https://unpkg.com/vue/dist/vue.js">
</script>
<script src="https://unpkg.com/element-ui/lib/index.js">
</script>
<script>
new Vue({
el: '#app',
data: function() {
return {
visible: false,
settedColor: '',
attrs:[
{
key: 'HOBBY',
name: '爱好',
type: 'TEXT',
setValues: [],
values:[]
},
{
key: 'GENDER',
name: '性别',
type: 'SINGLE_CHOICE',
setValues: [],
values:[{
key: 'MALE',
name: '男'
},{
key: 'FEMALE',
name: '女'
}]
},
{
key: 'COLOR',
name: '颜色',
type: 'MULTI_CHOICE',
setValues: [],
values:[{
key: 'RED',
name: '红色'
},{
key: 'YELLOW',
name: '黄色'
},{
key: 'BLUE',
name: '蓝色'
}]
}
]
}
},
methods: {
save() {
console.log('========== 设置的属性为 ========== ')
this.attrs.forEach(attr => {
if (attr.setValues && attr.setValues.length > 0) {
console.log(attr.key + ':' + attr.setValues)
}
});
}
}
})
</script>
</html>