2017-07-19 65 views
0

我的代碼是檢索服務中的用戶列表,並顯示使用ngfor異步沒有問題。我想爲用戶提供一種使用過濾器管道過濾結果的方法。只是不能讓這個工作。試着在身體上沒有(* ngIf =「peopleData」)。我想用戶篩選器輸入用戶名不被識別。angular2 ngfor異步管道過濾不起作用

用戶名 - 過濾器 - 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; 
    } 
} 

模態用戶一覽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); 

    } 


} 

模態用戶一覽component.html

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

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 { 
} 

回答

0

你需要從循環中刪除異步和添加,如果上述情況對循環

<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> 
+0

我解決了ngif的問題,通過向if添加異步。但是,usernameFilterPipe過濾器仍然不起作用: – Bswitzer

+0

{{ editor.displayName }}
Bswitzer

0

我加入異步的,如果固定ngif問題。然而,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>