which problem do you have? You are not able to retrieve the value and add it to another value?
You can cycle your json and do the sum for all the records that have “checked=true”
Or you can add the value to a variable each time the user click on the checkbox.
In your .html file you cycle all the checkbox that you will display on your page. For each checkbox you must pass the data of the element that it’s checked, included the price.
<ion-list>
<ion-item class="item_borded" *ngFor="let x of json_data;">
<ion-checkbox item-right color="primary" (ionChange)="onChange(x, $event.checked)"></ion-checkbox>
<ion-label text-wrap>{{x.name}}</ion-label>
<ion-label fixed text-right>€ {{x.price}}</ion-label>
</ion-item>
</ion-list>
In your .ts file retrieve the price of the checked element and you add it to your total.
var total_price;
onChange(data, isChecked: boolean) {
if(isChecked){
let price = data.price; //the price of the element that you checked
this.total_price+= price; // add the price of the checked element to a variable that will contain the total of the prices
}else {
//if is not checked you must subtract the price if the price was previously added
}
}
let a = this.Price1 ? parseFloat(this.Price1) : 0;
let b = this.Price2 ? parseFloat(this.Price2) : 0;
console.log(a + b);
but something is still isnt right i am getting the click event history as well.
How can i get the data of only checked checkboxes and not of the checkboxes checked history
I tried this method and it calculating only the last value of checkbox where i have 2 list of checkbox in which one has select option of anyone and another one i have multiple select option.
<ion-item no-lines *ngFor="let details of size; let i=index" style="font-size: 12px;">
<ion-checkbox item-left formControlName="size"
(ionChange)="selectSize(details.size, details.discounted_price, $event.checked)"> // here i want multiple checkbox to be selected
</ion-checkbox>
</ion-item>
<ion-item *ngFor="let addonitem of addon; let i = index" >
<ion-checkbox item-left formControlName="addOn" (ionChange)="selectAddon(addonitem.sub_item_name,addonitem.price, $event)" [ngModel]="checkedIdx == i" (ngModelChange)="$event ? checkedIdx = i : checkedIdx = -1"> //
</ion-checkbox>
<ion-item>
Tried many ways but unable to find the accurate method Please help
var total_price;
onChange(data, isChecked: boolean) {
if(isChecked){
let price = data.price; //the price of the element that you checked
this.total_price+= price; // add the price of the checked element to a variable that will contain the total of the prices
}else {
//if is not checked you must subtract the price if the price was previously added
}
}
this method gives me an output of “98undefined4242” if the checkbox value 98 , 42 & 42
public getTotal() {
let total = 0;
for (var i = 0; i < this.addOnPrice.length; i++) {
if (this.addOnPrice[i]) {
total += +this.addOnPrice[i];
}
}
return total;
}