Пользователь может нажать на кнопку +
и -
, чтобы увеличить или уменьшить значение. Как добавить минимальное и максимальное значение, например, min = 1 и max = 10 для приложения <span>[[ count ]]</span>
?
My Vue.js
:
<div id="app">
<a class="btn" v-on:click="increment">Add 1</a>
<a class="btn" v-on:click="decrement">Remove 1</a>
<span>[[ count ]]</span>
</div>
<script>
const App = new Vue({
el: '#app',
delimiters: ['[[',']]'],
data() {
return {
min: 1,
max: 10,
count: 1
}
},
methods: {
increment() {
this.count = this.count === this.max ? this.max : this.count + 1;
},
decrement() {
this.count = this.count === this.min ? this.min : this.count + 1;
}
}
})
</script>
Обновление: Над кодом работает сейчас.
1) Как мне изменить <span>[[ count ]]</span>
на <input type="number" min="0" max="10" />
, управляемый этими кнопками?
2) Как добавить класс, например disabled
, когда [[count]] === 1?
Обновление 2:
Я изменил его в поле ввода:
<input type="number" name="lineItems[{{ product.id }}][quantity]" value="{{ quantity }}" v-model.number="quantity" min="{{ product.minPurchase }}" max="{{ product.calculatedMaxPurchase }}" class="custom-qty__qty-field">
И настройте входное значение кнопками min и plus:
<script>
const app = new Vue({
el: '#app',
delimiters: ['[[',']]'],
data() {
return {
quantity: 1,
max: {{ product.calculatedMaxPurchase }},
min: {{ product.minPurchase }}
}
},
methods: {
increment() {
//this.count++
this.quantity = this.quantity === this.max ? this.max : this.quantity + 1;
},
decrement() {
//this.count--
this.quantity = this.quantity === this.min ? this.min : this.quantity - 1;
}
}
})
</script>
Например {{ product.minPurchase }}
являются twig
переменными, которые содержат настройки из Бэкэнд ShopWare.
Это чистый способ? И как мне добавить CSS класс, когда счет достигает 1, чтобы я мог отключить кнопку?