addRow3
by duckduck0054
HTML
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/[email protected]/lib/index.js"></script>
<div id="app">
<el-form :inline="true" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
<div class="col">
<el-form-item label="Description"
:prop="'items.' + index + '.description'"
:rules="rules.description">
<el-input v-model="item.description" maxlength="20"></el-input>
</el-form-item>
<el-form-item label="Price"
:prop="'items.' + index + '.price'"
:rules="{required: true, message: '전화번호 입력하세요', trigger: 'blur'}">
<el-input v-model="item.price"></el-input>
</el-form-item>
<el-button @click="deleteRow(index)"><i class="el-icon-close"></i></el-button>
</div>
</div>
<el-button type="primary" @click="addRow">AddRow</el-button>
<el-button type="primary" @click="submitForm('ruleForm')">Submit</el-button>
<el-button @click="resetForm('ruleForm')">Reset</el-button>
<el-button @click="setFocus">focus</el-button>
</el-form>
</div>
CSS
@import url("//unpkg.com/[email protected]/lib/theme-default/index.css");
.deleteRow {
background-color: #FF0000;
}
JavaScript
var Main = {
data() {
return {
maxlength: 20,
ruleForm: {
items: [{
description: '',
price: ''
}]
},
rules: {
description:[
{required: true, message: '이름 입력하세요', trigger: 'blur'},
{max: 20, message: '최대 20자까지 입력할 수 있습니다.' }
]
}
};
},
methods: {
submitForm(formName) {
console.log(JSON.stringify(this.ruleForm))
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!');
} else {
console.log('error submit!!');
return false;
}
});
},
resetForm(formName) {
this.$refs[formName].resetFields();
},
deleteRow(index){
console.log("index ::::: " + index)
if(index === 0){
return false;
}else{
this.ruleForm.items.splice(index,1)
}
},
setFocus: function()
{
// Note, you need to add a ref="search" attribute to your input.
this.$refs.search.focus();
},
addRow() {
this.ruleForm.items.push({
description: '',
price: ''
})
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')