How I can create an input box having a + and - button. Clicking upon which user can change the quantity of product selected, like this screen:
Asked
Active
Viewed 1.6k times
2 Answers
6
Here's a quickly thrown together example for Ionic 2. If you are using Ionic 1 you should be able to adapt it pretty easily.
You just need a couple controller/class functions to increment and decrement, then call those on tap from the buttons. This example covers just one button, so something like this wrapped in an ngFor
or a <ion-list>
page.ts:
private currentNumber = 0;
constructor () { }
private increment () {
this.currentNumber++;
}
private decrement () {
this.currentNumber--;
}
page.html:
<ion-icon name="remove-circle" (click)="decrement()">
{{currentNumber}}
<ion-icon name="add-circle" (click)="increment()">

amuramoto
- 2,838
- 1
- 11
- 15
-
And who not permit value < 0? – Ramos Sep 06 '16 at 13:00
-
How I can put a Sub-Total in the Header? – Ramos Sep 06 '16 at 13:43
-
1To prevent the value from gong below zero: private decrement () { if (this.currentNumber > 0) { this.currentNumber--; } } – amuramoto Sep 07 '16 at 17:00
-
1For subtotal, just create a function to add them, set the result to a var like this.subtotal, then show that in the template with {{subtotal}} – amuramoto Sep 07 '16 at 17:02
-
Thank you, Great! – Ramos Mar 28 '17 at 14:45
-
If you wrap this in a list, all the values will be increased or decresed with this answer. It should decrease or increase only the current item. – 1x2x3x4x Jan 12 '18 at 08:06
5
As for ionic v.1 at your template you could have something like:
<div class="flex_row">
<button class="button icon ion-minus-circled red" ng-click="sub(item)">
<p> {{item.quantity}} </p>
<button class="button icon ion-plus-circled green" ng-click="add(item)">
</div>
At your css
.red:before {
color: red;
}
.green:before {
color: green;
}
.flex_row {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
}
And in your controller
$scope.sub = function(i) {
i.quantity--;
}
$scope.add = function(i) {
i.quantity++;
}

korteee
- 2,640
- 2
- 18
- 24