angular - 在 Angular 6 中使用 ng2-smart-table 创建服务器端分页

标签 angular typescript angular6 ng2-smart-table

我正在尝试使用组件 <ng2-smart-table> .我确实做到了,但是我需要在用户点击页面或列进行排序后刷新数据,有没有办法捕获这些事件并刷新数据?

表格.组件.html

<ng2-smart-table 
[settings]="settings" 
[source]="characters">
</ng2-smart-table>

表.服务.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class TableService {
  constructor(private http: HttpClient) { }
  url = 'http://localhost:4000';
  getCharacters() {
    return this
            .http
            .get(`${this.url}/characters`);
  }
}

表.接口(interface).ts

export interface Table {
   ticket: String;
   title: String;
   state: String;
   owner: String;
   age: String;
   priority: String;
}

表格.组件.ts

import { Component, OnInit } from '@angular/core';
import { TableService } from './table.service';
import { Table } from './table.interface';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-table',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})
export class TableComponent implements OnInit {

  characters: Table[];
  settings = {
        columns: {
          ticket: {
            title: 'Ticket',
            filter: false
          },
          title: {
            title: 'Title',
            filter: false
          },
          state: {
            title: 'State',
            filter: false
          },
          owner:{
            title: 'Owner',
            filter: false
          },
          age:{
            title: 'Age',
            filter: false
          },
          priority:{
            title: 'Priority',
            filter: false
          }
        },
        actions:{
          columnTitle: '',
          add: false,
          edit: false,
          delete: false
        }
  };    

  constructor(private tservice: TableService) { }

  ngOnInit() {
      this
        .tservice
        .getCharacters()
        .subscribe((data: Table[]) => {
          this.characters = data;
      });
  }
}

父组件.ts

import { Component, OnInit } from '@angular/core';
import { TableComponent } from '../../utils/table/table.component';

@Component({
  selector: 'app-ticket-item-list',
  templateUrl: './ticket-item-list.component.html',
  styleUrls: ['./ticket-item-list.component.scss']
})
export class TicketItemListComponent implements OnInit {

  pagination = {
    TotalItems: 100,
    CurrentPage: 1,
    PageSize: 10,
    TotalPageLinkButtons: 5,
    RowsPerPageOptions: [10, 20, 30, 50, 100]
  };

  /* Paging Component metod */
  myChanges(event) {
    this.pagination.CurrentPage = event.currentPage;
    this.pagination.TotalItems = event.totalItems;
    this.pagination.PageSize = event.pageSize;
    this.pagination.TotalPageLinkButtons = event.totalPageLinkButtons;
  }

  constructor() { }

  ngOnInit() {
  }

}

parent.component.html

<app-table></app-table>

最佳答案

您可以使用 ng2-smart-table LocalDataSource 进行过滤、设置过滤器、添加过滤器等。因此您可以将服务结果存储在 LocalDataSource 对象中。在你的初始化中是这样的: this.characters = new LocalDataSource(tableService.getCharacters());

这是供引用的来源:https://github.com/akveo/ng2-smart-table/blob/master/projects/ng2-smart-table/src/lib/lib/data-source/local/local.data-source.ts

关于angular - 在 Angular 6 中使用 ng2-smart-table 创建服务器端分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198337/

相关文章:

Angular2 - 提供的参数与调用目标的任何签名都不匹配

node.js - NodeJS 在 TypeScript 项目中上传文件时遇到问题

typescript - 为什么 `map()` 返回类型不保持与输入相同数量的值?

javascript - Canvas - Canvas 保存为图像后,橡皮擦在 Canvas 上绘制黑线

angular - 如何隐藏 Ag-grid 中的过滤器图标?

javascript - Angular 2 中多个元素的单击处理程序

javascript - 清除/重置按钮上的过滤器从事件到非事件 - Angular

javascript - 由于 angulat6 中的撇号符号无法显示图像

angular - 我可以拥有在单个 js 文件中发出 Angular 元素的 Angular 库吗?

angular - 在哪里导入(导出?)使用 forRoot 的模块?