【问题标题】:Sort table header using Orderby in Angular 2在Angular 2中使用Orderby对表头进行排序
【发布时间】:2018-03-08 09:01:37
【问题描述】:

我对 Angular 很陌生,我正在从互联网上的一些教程中学习它。 现在我想学习如何使用 orderby 对我的英雄表的表头进行排序。我不明白管道以及如何从组件中调用它。 我只是不明白如何使用管道。我不明白他们何时在教程中解释了这一点。有人可以向我解释一下,以便我了解真正发生的事情吗?我不明白转换方法。这是我的代码,你能看出有什么问题吗?提前致谢

Superheroes.component.html
enter code here`
    <tr *ngFor="let hero of heroes | orderBy:['-hero.id', 'hero.name',
            '-hero.phone-number', 'hero.country']"> 

I generated the pipe, but it is not working.

Orderby.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'orderby', pure: false })

export class OrderbyPipe implements PipeTransform {

  transform(heroes: any[], name: string): any[]  {

    heroes.sort((a: any, b: any) => {

      if (a[name] < b[name]) {
        return -1;
      } else if (a[name] > b[name]) {
        return 1;
      } else {
        return 0;
      }
    });
    return heroes;
  }
}


    Superheros.component.ts
    import { Component, OnInit, Pipe, PipeTransform, Input } from '@angular/core';
    import { FormControl, FormGroup, Validators } from '@angular/forms';

    import { OrderbyPipe } from '../orderby.pipe';

    import { Heroes } from '../model/hero';
    import { HeroService } from '../services/hero.service';

    @Component({
      selector: 'app-superheroes',
      templateUrl: './superheroes.component.html',
      styleUrls: ['./superheroes.component.css'],
      providers: [HeroService],

    })

    export class SuperheroesComponent implements OnInit {

      isValidFormSubmitted: boolean = null;
      searchForm = null;
      statusmessage: string = "Nothing submitted";

      //records: Array<any>;
      isDesc: boolean = false;
      column: string = 'Name';
      direction: number;
      heroes: Hero[];

      constructor(private heroService: HeroService) { }

      ngOnInit(): void {
        this.searchForm = new FormGroup({
          searchmode: new FormControl('', Validators.required)

        });

      //  this.setDefaultValues();
        this.getHeroes();
      }

      getHeroes(): void {
        this.heroService.getHeroes().then(heroes => this.heroes = heroes);
      }

      sort(property) {
        this.isDesc = !this.isDesc;
        this.column = name;
        this.direction = this.isDesc ? 1 : -1;
      }
    }

【问题讨论】:

  • 把它想象成一个真正的管道。你有一些东西,通过管道按一下,然后又出来了……只是看起来可能和以前有点不同。根据管道的外观,内容将被转换。
  • 你可以使用material angulars sort header组件,检查这个:material.angular.io/components/sort/overview

标签: angular sorting tableheader


【解决方案1】:
My app.module.ts is like this.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { FormControl, FormGroup, Validators,  } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { OrderbyPipe } from './orderby.pipe';
import { SuperherosComponent } from './superheros/superheros.component';

export const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'superheros', component: SuperherosComponent }

  ];

@NgModule({
  declarations: [
    AppComponent,
    OrderbyPipe,
    SuperherosComponent
  ],
  imports: [
    BrowserModule, ReactiveFormsModule, RouterModule.forRoot(routes)

  ],
  providers: [],
  bootstrap: [AppComponent],

})
export class AppModule { }

【讨论】:

    【解决方案2】:

    首先,您制作的管道使用 string[] 作为参数,而不仅仅是字符串。 其次,不能使用[name],见https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe。 你可以使用一些像

    //use <tr *ngFor="let hero of heroes | orderBy:'name'">
    export class OrderbyPipe implements PipeTransform {
    
    transform(heroes: any[], name: string): any[]  {
      if (name=='name'){
         heroes.sort((a: any, b: any) => {
         return a.name==b.name?0:a.name<b.name?-1:1;
      }
      return heroes;
    }
    

    【讨论】:

    • 我做了更改,排序没有被执行?我认为这与 和 component.ts 文件有关。
    • 在您的 app.module 中,您必须在“声明”中包含 pipper @NgModule({ imports: [..],declarations: [orderby,...]],providers: [..] ,bootstrap:[AppComponent]})
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签