简体   繁体   English

如何实现 ng-bootstrap 4 表格排序、分页和过滤

[英]How to implement ng-bootstrap 4 table sorting, pagination and filtering

I am going through the official documentation of ng-bootstrap , in some of their official examples, the code is not working.我正在阅读ng-bootstrap官方文档,在他们的一些官方示例中,代码不起作用。 In particular I am talking about this and this examples, when you open them in stackblitz.特别是当你在 stackblitz 中打开它们时,我正在谈论这个这个例子。 This makes it hard for me to understand how the code works so that I can implement it my way.这让我很难理解代码是如何工作的,以便我可以按照自己的方式实现它。

I have seen this question but the answer is outdated as it is for angularjs.我见过这个问题,但答案已经过时了,因为它是 angularjs。

So now my questions is:所以现在我的问题是:

  1. How can I implement ng-bootstrap 4 table sorting, pagination and filtering as shown here ?我如何能实现NG-引导4表格排序,分页和过滤如图所示这里 What is the issue with the given code why is it not working?给定的代码有什么问题,为什么它不起作用?

A working example would be great as it would help me see and understand how the code works.一个工作示例会很棒,因为它可以帮助我查看和理解代码的工作原理。 Thank you.谢谢你。

I finally have a solution.我终于有了解决方案。 I went to their github to check the issues and someone confirmed that indeed the stackblitz code has an issue but gave the source code and a work-around.我去他们的 github检查问题, 有人确认 stackblitz 代码确实有问题,但提供了源代码和解决方法。

Here is the working code of an ng-bootstrap 4 table with sorting, pagination and filtering.这是带有排序、分页和过滤功能的 ng-bootstrap 4 表的工作代码

I basically organised the code and added a few things that were omitted in the original stackblitz code .我基本上组织了代码并添加了一些在原始 stackblitz 代码中省略的内容

Among the issues that were there, was that the directive wasn't declared in the declarations' array in AppModule.存在的问题之一是指令未在 AppModule 的声明数组中声明。

I came across the search feature and solved it like below: Search and filering solution (Not working part):我遇到了搜索功能并解决了如下问题:搜索和归档解决方案(不工作部分):

 constructor(pipe: DecimalPipe) {
    this.countries$ = this.filter.valueChanges.pipe(
      startWith(''),
      map(text => search(text, pipe))
    );
  }
}

The valueChanges would emit to a subscription, so if you change the code like below to subscribe to the changes: valueChanges 将发送到订阅,因此如果您更改如下代码以订阅更改:

constructor(pipe: DecimalPipe) {
    this.filter.valueChanges.pipe(
      startWith(''),
      map(text => {
         this.countries$ = search(text, pipe))
      }
    ).subscribe();
  }
}

Also, change the search method to return you a Observable instead of the array like below:此外,更改搜索方法以返回一个 Observable 而不是像下面这样的数组:

search(text: string, pipe: PipeTransform): Observable<Country[]> {
  return Observable.of(COUNTRIES.filter(country => {
    const term = text.toLowerCase();
    return country.name.toLowerCase().includes(term)
        || pipe.transform(country.area).includes(term)
        || pipe.transform(country.population).includes(term);
  }));
}

this should work.这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM