Hello, please dont link other topics to this one as i have spent days trying to find anything but none of them work for me and there are many differant methods.
I am trying to make a running total of cost for a series of receipts that are added to an array. for now, i have dummy data but will soon be using firebase to store the data.
Any help would be massively appreciated, i cant seem to figure it out.
This is the HTML
<ion-header>
<ion-toolbar>
<ion-title class="fontB">Expenses Home</ion-title>
</ion-toolbar>
</ion-header>
<div>
<ion-searchbar animated placeholder="Search Expenses"></ion-searchbar>
</div>
<ion-content>
<ion-list>
<ion-item-sliding *ngFor="let receipt of receipts" [routerLink]="['./receipts', receipt.id]">
<ion-item>
<ion-avatar>
<ion-img [src]="receipt.imageURL"></ion-img>
</ion-avatar>
<ion-label>
<ion-text>
{{receipt.name}} £{{receipt.amount.toFixed(2)}}
</ion-text>
<ion-text>
<p> <b>ON </b>{{receipt.date}} <b>AT </b> {{receipt.time}} <b>#</b> {{receipt.id}}</p>
</ion-text>
</ion-label>
</ion-item>
<!-- <ion-item-options side="end" color="danger">-->
<!-- <ion-item-option (click)="onDeleteReceipt()">Delete</ion-item-option>-->
<!-- </ion-item-options>-->
</ion-item-sliding>
</ion-list>
</ion-content>
<ion-footer>
<ion-item>
<span class="receipt-titles">Total Amount:</span> £ {{totalPrice}}
</ion-item>
</ion-footer>
This is the TS
import { Component } from '@angular/core';
import { ReceiptService } from '../create/receipt.service';
import { Receipt } from '../create/receipts.model';
@Component({
selector: 'app-home',
templateUrl: './home.page.html',
styleUrls: ['./home.page.scss'],
})
export class HomePage {
receipts: Receipt[];
constructor(private receiptService: ReceiptService
) {
}
ionViewWillEnter() {
this.receipts = this.receiptService.getAllReceipts();
}
}
And this is the service TS with the data.
import { Injectable } from '@angular/core';
import {Receipt} from './receipts.model';
@Injectable({
providedIn: 'root'
})
export class ReceiptService {
private receipts: Receipt[] = [{
id: 1,
name: 'Pancakes',
address: 'Home',
description: 'Some pancakes for Georgia',
amount: 1.50,
date: '06-10-2020',
time: '12:20',
imageURL: 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/ReceiptSwiss.jpg/170px-ReceiptSwiss.jpg'
},
{
id: 2,
name: 'Coke',
address: 'Work',
description: 'Some coca-cola for Mills',
amount: 24.95,
date: '12-08-2020',
time: '19:20',
imageURL: 'https://i.imgur.com/9C5Q9Rt.jpg'
},
{
id: 3,
name: 'Petrol in Tesco Extra',
address: 'Home',
description: 'Some pancakes for Dummy tesxtasiuch Georgia',
amount: 1.50,
date: '06-10-2020',
time: '12:20',
imageURL: 'https://i.imgur.com/w1iF3yD.jpg'
},
{
id: 4,
name: 'Krispy Kremes',
address: 'Home',
description: 'Somekiusadlffh sadliuhdlfg saiguh pancakes for Georgia',
amount: 1.50,
date: '06-10-2020',
time: '12:20',
imageURL: 'https://i.redd.it/xvbmnif3fnt21.jpg'
},
{
id: 5,
name: 'Nuttella',
address: 'Home',
description: 'Some pancakes zsldivuhiuh sdliuhdf vlijh for Georgia',
amount: 1.50,
date: '06-10-2020',
time: '12:20',
imageURL: 'https://i.ebayimg.com/images/g/0kMAAOSw-VtbCgcF/s-l300.jpg'
},
{
id: 6,
name: 'Cherries',
address: 'Home',
description: 'Some pancakes kuihsdf sdfiuh dyh oids dsfhjefiju ds fciisidfoi sd lidvhoi for Georgia',
amount: 1.50,
date: '06-10-2020',
time: '12:20',
imageURL: 'https://i.redd.it/go8r4r6opqe11.jpg'
}];
constructor() {
}
getAllReceipts() {
return [...this.receipts];
}
getReceipt(id: number) {
return {...this.receipts.find(receipt => {
return receipt.id === id;
})};
}
deleteReceipt(id) {
this.receipts = this.receipts.filter(receipt => {
return receipt.id !== id;
});
}
}
Finally this is some images of file structure etc.