简体   繁体   English

angular2 ngfor异步管道过滤不起作用

[英]angular2 ngfor async pipe filtering not working

My code is retrieving a list of users from a service and displaying using ngfor async with no issues. 我的代码正在从服务中检索用户列表,并使用ngfor async正常显示。 I wanted to provide a way for the user to filter the results using a filter pipe. 我想为用户提供一种使用过滤器管道过滤结果的方法。 Just cannot get this to work. 只是无法使它工作。 Triedwith and without the ( *ngIf="peopleData") on the body. 在正文上不带(* ngIf =“ peopleData”)进行尝试。 I am thinking the user filter input userName is not being recognized. 我认为用户过滤器输入userName无法识别。

username-filter-pipe.ts 用户名 - 过滤器 - pipe.ts

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'usernameFilterPipe'
})

export class UsernameFilterPipe implements PipeTransform {

  transform(value: any[], args: String[]): any {
    let filter = args[0].toLocaleLowerCase();
    return filter ? value.filter(user => 
user.displayName.toLowerCase().indexOf(filter) !== -1) : value;
  }
}

modal-user-list-component.ts 模式 - 用户列表component.ts

import {Component, Input} from '@angular/core';

import {NgbModal, NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';
import { Observable } from 'rxjs';
import { WorkflowService } from './workflow.service';
import { GrcUser } from './grc-user';
import {UsernameFilterPipe} from './username-filter-pipe';

@Component({
  selector: 'ngbd-modal-content',
  providers: [WorkflowService],
  template: `
<div class="modal-header">
  <input type="text" id="inputUserName" class="form-control" placeholder="UserName" [(ngModel)]="userName">
  <button type="button" id="workflow_display_task_assign_modal_dismiss" class="close" aria-label="Close" (click)="activeModal.dismiss('Cross click')">
  <span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body"  *ngIf="peopleData" style="width:auto;height:auto">
    <table class="table table-bordered table-responsive table-hover table-sm">
    <thead>
        <tr>
            <th> <div>UserName</div></th>
        </tr>
    </thead>
    <tbody>
        <tr *ngFor="let editor of peopleData | async | usernameFilterPipe:userName " (click)="setCurrentEditor($event, editor)" 
         [class.table-info]="isSelected(editor)">
            <td>
               {{ editor.displayName }}
            </td>
        </tr>
      </tbody>
    </table> 
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-secondary" id="workflow_display_task_assign_modal_submit" (click)="activeModal.close('Close click')">Submit</button>
 </div>
`
})
export class NgbdModalContent {
  @Input() peopleData: Observable<Array<any>>;
  currentSelectedEditor : GrcUser;

  constructor(public activeModal: NgbActiveModal,private peopleService: WorkflowService) {

    this.peopleData = peopleService.getAllUserInfo();

    console.log("PeopleData in NgbdModalContent constructor :" + this.peopleData);

  }

  isSelected(editor: any) {
    return ((this.currentSelectedEditor) && (this.currentSelectedEditor.userId == editor.userId));
  }

  setCurrentEditor(event: any, editor: any) {
    if (this.isSelected(editor)) {
      this.currentSelectedEditor = null;
    } else {
      this.currentSelectedEditor = editor;
    }
  }



}

@Component({
  selector: 'ngbd-modal-component',
  templateUrl: './modal-user-list-component.html'
})


export class NgbdModalComponent {

  constructor(private modalService: NgbModal) {}

  open() {

    const modalRef = this.modalService.open(NgbdModalContent);

  }


}

modal-user-list-component.html 模式 - 用户列表component.html

<button class="btn btn-primary" (click)="open()">Assign</button>

app.module.ts app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule, CUSTOM_ELEMENTS_SCHEMA, APP_INITIALIZER } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AppComponent } from './app.component';
import { SidebarComponent } from "./core/nav/sidebar.component";

import { WorkflowDisplayComponent } from  './components/workflow_display/workflow-display.component'
import { TaskComponent } from "./components/workflow_display/task.component";
import { WorkflowService } from  './components/workflow_display/workflow.service'
import { PropertyService } from  './shared/property.service';
import { NotificationsService } from "./shared/notifications/notifications.service";
import { Notifications } from "./shared/notifications/notifications.component";
import { TaskViewService } from "./components/workflow_display/views/task-view-service";
import { CookieService } from "./shared/cookie.service";
import { AppRoutingModule } from "./app-routing.module";
import { DatePipe } from "./shared/date-pipe";
import {UsernameFilterPipe} from './components/workflow_display/username-filter-pipe';
import { NgbdModalComponent, NgbdModalContent } from './components/workflow_display/modal-user-list-component';

function propertyServiceFactory(config: PropertyService) {
  return () => config.load();
}

@NgModule({
  declarations: [
    AppComponent,
    Notifications,
    SidebarComponent,
    WorkflowDisplayComponent,
    TaskComponent,
    DatePipe,
    AppComponent,
    NgbdModalComponent,
    NgbdModalContent,
    UsernameFilterPipe,

  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpModule,
    NgbModule.forRoot(),
  ],
  providers: [
    {
      // Provider for APP_INITIALIZER
      provide: APP_INITIALIZER,
      useFactory: propertyServiceFactory,
      deps: [PropertyService],
      multi: true
    },
    NotificationsService,
    PropertyService,
    CookieService,
    WorkflowService,
    TaskViewService
  ],
  entryComponents: [NgbdModalContent],
  bootstrap: [AppComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class AppModule {
}

you need remove async from for loop and add if case above for loop 您需要从for循环中删除async,并在上述for循环中添加case

<div *ngIf="peopleData?.length > 0">

     <tr *ngFor="let editor of peopleData  | usernameFilterPipe:userName " (click)="setCurrentEditor($event, editor)" 
             [class.table-info]="isSelected(editor)">
                <td>
                   {{ editor.displayName }}
                </td>
            </tr>

    </div>

I fixed the issue with ngif by adding async to the if. 我通过向if添加异步来解决了ngif的问题。 However the usernameFilterPipe filter is still not working: 但是,usernameFilterPipe过滤器仍然无法正常工作:

    <tbody>
    <div *ngIf="(peopleData | async)?.length > 0">
        <tr *ngFor="let editor of peopleData | async | usernameFilterPipe:userName " (click)="setCurrentEditor($event, editor)" 
         [class.table-info]="isSelected(editor)">
            <td>
               {{ editor.displayName }}
            </td>
        </tr>
        </div>
      </tbody>

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

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