首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular 6中使用ng2-smart-table创建服务器端分页

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

Stack Overflow用户
提问于 2018-07-06 03:09:37
回答 2查看 10.1K关注 0票数 6

我正在尝试使用组件。我确实做到了,但是在用户单击页面或列进行排序后,我需要刷新数据,有什么方法可以捕获这些事件并刷新数据吗?

table.component.html

代码语言:javascript
复制
<ng2-smart-table 
[settings]="settings" 
[source]="characters">
</ng2-smart-table>

table.service.ts

代码语言:javascript
复制
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`);
  }
}

table.interface.ts

代码语言:javascript
复制
export interface Table {
   ticket: String;
   title: String;
   state: String;
   owner: String;
   age: String;
   priority: String;
}

table.component.ts

代码语言:javascript
复制
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;
      });
  }
}

parent.component.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<app-table></app-table>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-06 03:32:42

您可以使用ng2-smart-table LocalDataSource来过滤、设置过滤器、添加过滤器等。因此,您可以将服务结果存储在LocalDataSource对象中。在您的初始化中,类似于: this.characters =新的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

票数 0
EN

Stack Overflow用户

发布于 2019-01-23 04:11:23

对于分页,请使用setPaging,如

代码语言:javascript
复制
this.YourlocalDataSource.setPaging(1, 10, true);

1是页面,10是每页的行数,true强制渲染页面

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51198337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档