Created
January 8, 2020 06:47
-
-
Save wisetc/e70c20ea7ceb288feb15644b50b2c57f to your computer and use it in GitHub Desktop.
不定项录入表单
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div class="field-collection"> | |
<el-form class="_item" v-for="(form, i) in C" :key="i" inline label-width="120px"> | |
<el-form-item class="_item__col" :label="type + '编号'"> | |
<el-input type="text" class="_input-el" v-model="form.number" @change="emitForm" /> | |
</el-form-item> | |
<el-form-item class="_item__col" :label="priceLabel"> | |
<el-input type="number" class="_input-el" v-model.number="form.price" @change="emitForm" /> | |
<span class="_price-unit">{{ priceUnit }}</span> | |
</el-form-item> | |
</el-form> | |
</div> | |
</template> | |
<script> | |
export default { | |
props: { | |
count: { | |
type: Number, | |
default: 1, | |
}, | |
type: { | |
type: String, | |
validator: function(value) { | |
return ['车位', '储物柜', '一卡通'].indexOf(value) !== -1 | |
}, | |
}, | |
}, | |
name: 'FieldCollection', | |
computed: { | |
priceUnit() { | |
return this.type === '车位' || this.type === '储物柜' ? '元/月' : '元' | |
}, | |
priceLabel() { | |
return this.type === '车位' || this.type === '储物柜' ? '月租金' : '押金' | |
}, | |
}, | |
data() { | |
return { | |
// 因为 type 初值为空,所以 C 的初值最好是放在生命周期中 | |
C: [], | |
} | |
}, | |
created() { | |
// 列表原始数据 | |
this.C = this.initC() | |
}, | |
watch: { | |
count(val) { | |
// | |
this.C = this.getC(val) | |
}, | |
// 类型变化,清空数据 | |
type() { | |
// | |
this.C = this.initC() | |
}, | |
}, | |
methods: { | |
initC() { | |
// use map instead of fill in case mutation. | |
return new Array(this.count).fill(null).map(() => this.getOriginForm()) | |
}, | |
// 得到表单的初始数据 | |
getOriginForm() { | |
return { | |
number: '', | |
price: 0, | |
priceLabel: this.priceLabel, | |
priceUnit: this.priceUnit, | |
} | |
}, | |
/** | |
* 得到表单列表 | |
* @param {number} count | |
* @returns {any[]} | |
*/ | |
getC(count) { | |
// count FIN equals C.length | |
// if count > C.length: concat C and sub empty form list | |
// elif count < C.length: splice C from 0 to count | |
const len = this.C.length | |
if (count > len) { | |
return this.C.concat( | |
new Array(count - len).fill(null).map(o => this.getOriginForm()) | |
) | |
} else if (count < len) { | |
return this.C.slice(0, count) | |
} | |
return [] | |
}, | |
/** | |
* 表单是否完整 | |
* @param {{number: string; price: number}} form | |
* @returns {boolean} | |
*/ | |
isFormValid(form) { | |
return Boolean(form.number) && Boolean(form.price) | |
}, | |
/** | |
* 是否完整校验 | |
* @returns {boolean} | |
*/ | |
validate() { | |
// if one of form in C has empty value: return false | |
if (this.C.length === 0) { | |
return new Error('房源信息不能为空。') | |
} else if (this.C.find(form => !this.isFormValid(form))) { | |
return new Error('请完善房源信息。') | |
} else { | |
// + number 不能重复 | |
const arr = this.C.map(o => o.number) | |
if (arr.length !== new Set(arr).size) { | |
return new Error('房源信息数据重复。') | |
} | |
} | |
return null | |
}, | |
// 输出到表单的值 | |
getForm() { | |
return { | |
houseInfoList: JSON.stringify(this.C), | |
} | |
}, | |
emitForm() { | |
this.$emit('input', this.getForm()) | |
}, | |
}, | |
} | |
</script> | |
<style lang="scss"> | |
.field-collection { | |
padding: 0 24px; | |
margin-bottom: 24px; | |
._item { | |
border-top: 1px solid #cccccc; | |
padding-top: 22px; | |
} | |
._item__col { | |
position: relative; | |
width: 48%; | |
} | |
._price-unit { | |
position: absolute; | |
top: 0; | |
right: 1em; | |
} | |
._input-el { | |
width: 12em; | |
} | |
.el-form-item__content { | |
min-height: auto; | |
} | |
._input-el input[type='number']::-webkit-inner-spin-button { | |
display: none; | |
} | |
} | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```vue | |
<template> | |
<div> | |
<field-collection | |
v-if="fakeForm.houseType" | |
ref="fcRef" | |
:type="getOptionLabel('houseType', fakeForm.houseType)" | |
:count="fakeForm.count" | |
/> | |
</div> | |
</template> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment