首页 > 解决方案 > Angular DataTables - 来自本地存储的 ajax 调用

问题描述

我正在寻找一种方法来对已经存储了 json 对象的本地存储进行 ajax 调用,以便可以将其加载到表中。

loadDataTable() {
  this.jsonService.getJson().subscribe(response => {
    this.dataToRead = response.data;
  });

  this.dtOptions = {
    ajax: this.dataToRead,
    columns: [{
     title: 'ID',
     data: 'id'
    },
    {
     title: 'Name',
     data: 'name'
    },
    {
     title: 'Age',
     data: 'age',
    },
    {
     title: 'Address',
     data: 'address'
    }],
    rowCallback: (row: Node, data: any[] | Object, index: number) => {
      const self = this;
      $('td', row).unbind('click');
      $('td', row).bind('click', () => {
      self.viewFarmDetails(data);
      });
      return row;
   }
};

标签: angularangular-datatables

解决方案


如果数据在本地存储中,您可以读取它。

导入组件中的任何实用程序库(例如 ngx-webstorage)

import { LocalStorageService } from 'ngx-webstorage';

将其添加到您的构造函数

constructor(private $localStorage: LocalStorageService) {
}

然后在需要时从 localStorage 中读取

this.$localStorage.retrieve('my-data-in-storage')

用您在存储中存储该数据的密钥替换“my-data-in-storage”!

希望这会有所帮助!


推荐阅读