I'm trying to save a data array object with many attributes to a favorite list using Ionic SQlite storage.
For instance, I have a data array in data provider:
data.ts
private options: any[] = [
{
"name": "option 01",
"website": "www.google.com",
"about": "choose this option 01",
"id": "1"
},
{
"name": "option 02",
"website": "www.yahoo.com",
"about": "choose this option 02",
"id": "2"
},
{
"name": "option 03",
"website": "www.bing.com",
"about": "choose this option 03",
"id": "3"
},
{
"name": "option 04",
"website": "www.stackoverflow.com",
"about": "choose this option 04",
"id": "4"
}
]
and I'm calling these data objects in my home html page which has an option to save to favorite list.
<ion-card *ngFor="let option of options">
<h1>{{option.name}}</h1>
<h1>{{option.about }}</h1>
<h4>{{option.website}}</h4>
<button ion-button block (click)="saveToFavorite()">Save to Favorite List</button>
<button ion-button block (click)="removeFromFavorite()">Remove from Favorite List</button>
</ion-card>
and here's incomplete home.ts file
import { Storage } from '@ionic/storage';
const STORAGE_KEY = 'favoriteOptions';
saveToFavorite(option) {
this.storage.set();
}
removeFromFavorite(option) {
this.storage.remove();
}
I want saveToFavorite() will save option data array object to a favorite options storage key so I can load them all up on a favoriteList HTML page:
<ion-content>
<div *ngFor="let option of options">
<h1>{{option.name}}</h1>
<p>{{option.about}}</p>
<button ion-button block (click)="removeFromFavorite()">Remove from Favorite List</button>
</div>
</ion-content>
favoriteList ts file:
getAllFavoriteOptions() {
return this.storage.get(STORAGE_KEY);
}
removeFromFavorite(option) {
this.storage.remove();
}
Somehow I'm kinda lost. Please correct my codes on ts files.
Thanks in advance!
make a function to save the favorites:
then to get favorites add this function:
for ionic: if you are using
@ionic/storage
then to load items use async pipe:then to access them in *ngFor do this: