How to save a data array object to ionic SQlite st

2020-05-01 06:42发布

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!

1条回答
成全新的幸福
2楼-- · 2020-05-01 06:55

make a function to save the favorites:

addFavorite(favorite:any):void{
  let favorites = this.storage.get('favorites');
  if(favorites){
    favorites.push(favorite);
    this.storage.set('favorites', favorites);
  }else{
    favorites = [];
    favorites.push(favorite);
    this.storage.set('favorites',favorites);
  }

then to get favorites add this function:

getFavorites():any{
  let favorites = this.storage.get("favorites");
  return favorites;
}

for ionic: if you are using @ionic/storage then to load items use async pipe:

getFavorites():Promise<any>{
  let favorites = this.storage.get("favorites");
  return favorites;
}

then to access them in *ngFor do this:

<div *ngFor="let item of getFavorites() | async">
      <div>{{item.name}}</div>
</div>
查看更多
登录 后发表回答