我正在做一个简单的表格,显示3个元素。
用户在每个字段中输入2个条形码。barcodeEnd值的前2位与allBarcodePrefixes数据进行比较,看是否匹配,如果不匹配则打印相应的机构名称或 "Agency Not Found"。如果没有匹配,表单就会停止(用户必须修正条码输入),但是如果它找到了匹配,它就会弹出一行新的字段和v-models的新生成集。
这工作很好,直到我把v-models做成动态的,这样我就可以独立地操作每个条目进行验证和取值。
问题有两件奇怪的事情发生,我需要帮助的家伙。
showAgencyName()
方法应该是获取v-model,然后与数据进行比较,以获取机构名称。但是我的v-model是动态的,而且是以字符串文字形式存在的。我不知道如何提取这个值。我的代码在一个 密码笔 你可以去看看,看看我的意思。我把 onAddBarcodes
函数中的失败行,这样你就可以看到它正确地验证了(这部分工作)。最终,这一行将被删除,因为它只应该在先对机构进行验证后对minLength和maxLength进行验证。
<div id="q-app">
<div class="q-pa-md">
<div>
<div v-for="(barcode, index) in barcodes" :key="index">
<div class="full-width row no-wrap justify-start items-center q-pt-lg">
<div class="col-3">
<label>Starting Roll #:</label>
<q-input outlined square dense v-model="$v[`barcodeStart${index}`].$model"></q-input>
<div class="error-msg">
<div v-if="!$v[`barcodeStart${index}`].maxLength || !$v[`barcodeStart${index}`].minLength">
<span> Must be exactly 9 characters. </span>
</div>
</div>
</div>
<div class="col-3">
<label>Ending Roll #:</label>
<q-input outlined square dense v-model="$v[`barcodeEnd${index}`].$model" @change="showAgencyName(barcode)"></q-input>
<div class="error-msg">
<div v-if="!$v[`barcodeEnd${index}`].maxLength || !$v[`barcodeEnd${index}`].minLength">
<span> Must be exactly 9 characters. </span>
</div>
</div>
</div>
<div class="col-3">
<label>Agency:</label>
<div v-if="barcode.agencyName">
{{ barcode.agencyName }}
</div>
<div v-else></div>
</div>
</div>
</div>
</div>
</div>
</div>
Vue.use(window.vuelidate.default)
const { required, minLength, maxLength } = window.validators
new Vue({
el: '#q-app',
data () {
return {
barcodes: [
{
barcodeStart: "",
barcodeEnd: "",
agencyName: ""
}
],
newPackage: "",
reset: true,
allBarcodePrefixes: {
"10": "Boston",
"11": "New York",
"13": "Houston",
"14": "Connecticut",
"16": "SIA",
"17": "Colorado",
"18": "Chicago"
}
}
},
validations() {
const rules = {};
this.barcodes.forEach((barcode, index) => {
rules[`barcodeStart${index}`] = {
minLength: minLength(9),
maxLength: maxLength(9)
};
});
this.barcodes.forEach((barcode, index) => {
rules[`barcodeEnd${index}`] = {
minLength: minLength(9),
maxLength: maxLength(9)
};
});
return rules;
},
methods: {
onAddBarcodes() {
// creating a new line when requested on blur of barcodeEnd
const newBarcode = {
barcodeStart: "",
barcodeEnd: "",
agencyName: ""
};
this.barcodes.push(newBarcode);
},
showAgencyName(barcode) {
var str = barcode.barcodeEnd; // I need to pull the v-model value
var res = str.substring(0, 2); //get first 2 char of v-model
if (this.allBarcodePrefixes[res] == undefined) {
//compare it to data
barcode.agencyName = "Agency not found"; //pass this msg if not matched
this.onAddBarcodes(); //adding it to the fail just for testing
} else {
barcode.agencyName = this.allBarcodePrefixes[res]; //pass this if matched
this.onAddBarcodes(); //bring up a new line
}
},
}
})
先谢谢你
你可以做的是在你的v-for循环中设置v-model为对象。这样,它将看起来像这样。
<q-input outlined square dense v-model="barcode.barcodeStart"></q-input>
和
<q-input outlined square dense v-model="barcode.barcodeEnd"></q-input>
这样他们就会更新条码对象,由于它是一个可观察的对象,你的 showAgencyName
函数可以保持原样,其中的条码对象将被更新。
编辑
当你调用onchange事件时,你可以通过index
@change="showAgencyName(barcode, index)"
然后,在你的showAgencyName函数中,你可以用以下方法访问验证 this.$v
.
为了确保规则已经过去,你将需要更新的 $model
. 像这样
this.$v[`barcodeStart${index}`].$model = barcode.barcodeStart
this.$v[`barcodeEnd${index}`].$model = barcode.barcodeEnd
// Check for errors
if (this.$v[`barcodeEnd${index}`].$error) {
return
}