【发布时间】:2021-12-30 02:18:40
【问题描述】:
我一直在开发食品订购应用程序,但是在限制复选框时遇到了这个问题,当我选中复选框时,我希望限制仅适用于组而不是所有复选框。因为有些组可以检查多个。我假设它的 V-MODEL=modal.selectedAddOn 问题我试图设置动态但应用程序不会。
<fieldset class="row mb-2" v-for="(variant,variantIndex) in modal.variants" v-if="Object.keys(modal.variants).length !== 0">
<legend class="col-form-label col-sm-4 pt-0">{{variant.description}}</legend>
<div class="col-sm-8">
<div class="form-check" v-for="(extra,extraIndex) in variant.extras">
<input class="form-check-input" type="checkbox" :id="'variant-'+extra.id" :value="extra.id" v-model="modal.selectedAddOn" :disabled="modal.selectedAddOn.length > variant.limitation && modal.selectedAddOn.indexOf(extra.id) === -1">
<label class="form-check-label" :for="'variant-'+extra.id" >
{{extra.variant_name}} --- B$ {{extra.price}}
}
</label>
</div>
</div>
</fieldset>
<script>
var app = new Vue({
el: '#app',
data: {
title: 'Food Menu',
currentFilter: 'ALL',
products : null,
selectedAddOn : [],
modal : {
name : "",
product_id : "",
variants : {},
price : 0,
quantity : 1,
remark :"",
},
},
filters : {
},
mounted(){
},
methods : {
variantOpen(e){
var id = e.relatedTarget.id
var product = this.products[id]
//get variant
axios.get('<?= base_url('product/getNewVariant/'); ?>'+ product.id,{})
.then(response => {
if(!Object.keys(response.data).length){
console.log("no data found");
}else{
this.modal.variants = response.data;
}
})
this.modal.product_id = product.id;
this.modal.name = product.name;
this.modal.price = product.price;
},
setFilter : function(filter) {
this.currentFilter = filter;
},
addToCart : function () {
axios({
method: "post",
url: "<?= base_url('product/addToCart'); ?>",
data: bodyFormData,
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
//handle error
console.log(response);
});
},
},
});
这是来自我的服务器 (Codeigniter) 的响应
[
{
"id": "1",
"variant_manager_id": "1",
"description": "Choose Flavours",
"limitation": "1",
"status": "1",
"create_date": "2021-12-29 16:56:00",
"extras": [
{ "id": "1", "variant_id": "1", "variant_name": "Lotus", "status": "1", "is_checked": "false", "price": "0.00" },
{ "id": "2", "variant_id": "1", "variant_name": "Pandan", "status": "1", "is_checked": "false", "price": "0.00" },
{ "id": "3", "variant_id": "1", "variant_name": "Red Bean", "status": "1", "is_checked": "false", "price": "0.00" }
]
},
{
"id": "2",
"variant_manager_id": "1",
"description": "Add On",
"limitation": "1",
"status": "1",
"create_date": "2021-12-29 16:56:00",
"extras": [
{ "id": "4", "variant_id": "2", "variant_name": "Egg", "status": "1", "is_checked": "false", "price": "1.00" },
{ "id": "5", "variant_id": "2", "variant_name": "Chicken", "status": "1", "is_checked": "false", "price": "1.00" }
]
},
{
"id": "3",
"variant_manager_id": "1",
"description": "Choose Size",
"limitation": "1",
"status": "1",
"create_date": "2021-12-29 16:56:00",
"extras": [
{ "id": "6", "variant_id": "3", "variant_name": "Small", "status": "1", "is_checked": "false", "price": "1.00" },
{ "id": "7", "variant_id": "3", "variant_name": "Medium", "status": "1", "is_checked": "false", "price": "1.00" }
]
},
{
"id": "4",
"variant_manager_id": "1",
"description": "Add On 2",
"limitation": "1",
"status": "1",
"create_date": "2021-12-29 16:56:00",
"extras": [
{ "id": "8", "variant_id": "4", "variant_name": "Left", "status": "1", "is_checked": "false", "price": "1.00" },
{ "id": "9", "variant_id": "4", "variant_name": "Middle", "status": "1", "is_checked": "false", "price": "1.00" },
{ "id": "10", "variant_id": "4", "variant_name": "Right", "status": "1", "is_checked": "false", "price": "1.00" }
]
}
]
【问题讨论】:
-
你的
selectedAddOn不在modal,应该是v-model="selectedAddOn" -
您好,我在复选框处更改为 selectedAddOn,但它仍然不限制我的复选框
-
你的意思是用户只能根据限制勾选复选框?例如限制 = 1 表示用户只能选择一项,限制 = 2 表示用户可以选择 2 项?
-
如果我的回答对您有用,请标记为已接受。如果没有,请告诉我缺少什么。
-
兄弟真的非常感谢你的帮助。我是 vue 的新手,过去几天一直在尝试解决这个问题。