How to save a data array object to ionic SQlite storage (TypeScript, Angular 5, Ionic 3)

不羁的心 提交于 2019-12-02 15:56:08

问题


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:


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>


来源:https://stackoverflow.com/questions/49961425/how-to-save-a-data-array-object-to-ionic-sqlite-storage-typescript-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!