vue.js - 如何根据复选框和输入值递增
问题描述
我正在尝试创建一个按钮,该按钮将根据输入/选择的复选框和输入值的数量而变化。
例如,我有一个按钮 html,它会根据选择的项目数量而变化
- 购买(已选择 0 件商品)
- 购买 1 件商品(选择/输入 1 件商品)
- 购买 x 件(选择/输入 x 件)
使用当前设置为 0 的 buyCounter 数据变量。我想根据复选框/输入值增加/减少此计数器。
如果选择了一个复选框,则它计为 +1,如果取消选择 -1。
在输入上,它采用输入的任何值并将其添加到计数器中。例如,如果在输入上输入 4,则将 +4 添加到计数器。
目前不关注该数字是否超过允许的数量,实际上只是想弄清楚对应的部分。
我在下面的 codepen 中创建了这个示例,以足够接近我想要实现的目标。任何正确方向的帮助将不胜感激
https://codepen.io/anon/pen/QooWZV?&editable=true&editors=101
模板
<div id="app">
<v-app id="inspire">
<v-data-table
v-model="selected"
:headers="headers"
:items="desserts"
item-key="name"
class="elevation-1"
>
<template v-slot:items="props">
<td v-if="props.item.available > 1" style="display:inline-flex">
<v-text-field style="width:5%; padding-bottom:10px;"></v-text-field> of {{ props.item.available }}
</td>
<td v-else>
<v-checkbox
v-on:change="increment($event)"
v-model="props.selected"
primary
hide-details
></v-checkbox>
</td>
<td>{{ props.item.name }}</td>
<td class="text-xs-right">{{ props.item.calories }}</td>
<td class="text-xs-right">{{ props.item.fat }}</td>
<td class="text-xs-right">{{ props.item.carbs }}</td>
<td class="text-xs-right">{{ props.item.protein }}</td>
<td class="text-xs-right">{{ props.item.iron }}</td>
</template>
</v-data-table>
<v-layout row wrap class="">
<v-flex xs4 class="text-xs-left">
<v-btn
v-if="buyCounter < 1"
color="blue-grey"
class="white--text">
Buy
</v-btn>
<v-btn
v-else-if="buyCounter > 0 && buyCounter < 2"
color="blue-grey"
class="white--text">
Buy {{ buyCounter }} Item
</v-btn>
<v-btn
v-else-if="buyCounter > 1"
color="blue-grey"
class="white--text">
Buy {{ buyCounter }} Items
</v-btn>
</v-flex>
<v-flex xs4 class="text-xs-center">
</v-flex>
<v-flex xs4 class="text-xs-right">
</v-flex>
</v-layout>
</v-app>
</div>
脚本
new Vue({
el: '#app',
data () {
return {
selected: [],
buyCounter: 0,
headers: [
{
text: 'Count',
align: 'left',
sortable: false,
value: ''
},
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name'
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' }
],
desserts: [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
available: 1
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
available: 2
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
available: 1
},
{
name: 'Cupcake',
calories: 305,
fat: 3.7,
carbs: 67,
protein: 4.3,
iron: '8%',
available: 1
},
{
name: 'Gingerbread',
calories: 356,
fat: 16.0,
carbs: 49,
protein: 3.9,
iron: '16%',
available: 3
},
{
name: 'Jelly bean',
calories: 375,
fat: 0.0,
carbs: 94,
protein: 0.0,
iron: '0%',
available: 1
},
{
name: 'Lollipop',
calories: 392,
fat: 0.2,
carbs: 98,
protein: 0,
iron: '2%',
available: 2
},
{
name: 'Honeycomb',
calories: 408,
fat: 3.2,
carbs: 87,
protein: 6.5,
iron: '45%',
available: 1
},
{
name: 'Donut',
calories: 452,
fat: 25.0,
carbs: 51,
protein: 4.9,
iron: '22%',
available: 1
},
{
name: 'KitKat',
calories: 518,
fat: 26.0,
carbs: 65,
protein: 7,
iron: '6%',
available: 3
}
]
}
},
methods:{
increment(event){
console.log(this.selected);
const isChecked = event;
if(isChecked){
this.buyCounter++;
}else{
this.buyCounter--;
}
}
}
})
希望创建一个按钮来识别将要购买的商品数量。
解决方案
推荐阅读
- python - 将简单的几何图形导出到 gltf?
- javascript - 为什么将数组作为参数传递会导致数组中的每个项目重复?
- swift - UIPickerView PanGesture 与模态呈现的 VC 解除手势一致
- html - 无限的图像,响应能力,流畅的方形/网格画廊,图像的最小和最大尺寸,但始终小于 10 像素容器边距
- python - 如何从 Project Jupyter 中的 HTML 小部件获取 python 事件中选定文本的值?
- c# - 从服务获取用户映射的网络驱动器
- ms-access - 使用 Microsoft Access 获取语法错误
- angular - 错误类型错误:无法读取未定义角度的属性“选项卡”
- javascript - “必须使用解构状态分配”:如何从对象解构并放置在对象文字内的属性上
- c++ - 如何将 RC522 UID 十六进制字节转换为十进制?