Angular matSort does not sort

后端 未结 8 1750
深忆病人
深忆病人 2020-12-09 03:07

I\'m having trouble with importing matSort into my matTable.

I\'m providing you with my code:

dashboard.component.ts



        
相关标签:
8条回答
  • 2020-12-09 03:38

    If you use "*ngIf" to initialy hide the table container in your template, the viewchield will be undefined.

    0 讨论(0)
  • 2020-12-09 03:41

    I've found solution for this problem.

    The main problem was that table was rendered before data arrived. I've loaded data first and sent it like source to dataSource constructor. After it, problem disappeared. That's the thing with async http.get and services.

    Thanks for you help.

    fetchData(id){
      this.fetch.getProcesses(id).subscribe(result => {
        this.processes = result;
        this.dataSource = new TableDataSource(this.processes);
        Observable.fromEvent(this.filter.nativeElement, 'keyup')
          .debounceTime(150)
          .distinctUntilChanged()
          .subscribe(() => {
            if (!this.dataSource) {
              return;
            }
            this.dataSource.filter = this.filter.nativeElement.value;
          });
      });
    }
    
    
    
    
    export class TableDataSource extends DataSource<Proces>{
      _filterChange = new BehaviorSubject('');
      get filter(): string {
        return this._filterChange.value;
      }
      set filter(filter: string) {
        this._filterChange.next(filter);
      }
      filteredData: any =[];
    
      constructor(private processes:Proces[]){
        super();
      }
      connect(): Observable<Proces[]>{
        const displayDataChanges = [
          this.processes,
          this._filterChange,
        ];
        return Observable.merge(...displayDataChanges).map(() => {
          this.filteredData = this.processes.slice().filter((item: Proces) => {
            const searchStr = (item.name).toLowerCase();
            return searchStr.indexOf(this.filter.toLowerCase()) !== -1;
    
          });
          return this.filteredData;
        })
      }
      disconnect(){}
    }
    
    
    
          <div class="header">
        <mat-form-field floatPlaceholder="never">
          <input matInput #filter placeholder="Pretraži procese">
        </mat-form-field>
      </div>
      <mat-table #table [dataSource]="dataSource" class="table-mantis" matSort>
    
        <ng-container matColumnDef="col1">
          <mat-header-cell *matHeaderCellDef class="table-header-cell" mat-sort-header> Name</mat-header-cell>
          <mat-cell *matCellDef="let row" class="example-cell" > {{row.nazivProcesa}} </mat-cell>
        </ng-container>
    
        <ng-container matColumnDef="col2">
          <mat-header-cell *matHeaderCellDef class="table-header-cell" mat-sort-header> Cell 2</mat-header-cell>
          <mat-cell *matCellDef="let row" class="example-cell" > {{row.nazivVlasnika
            }} </mat-cell>
        </ng-container>
    
        <ng-container matColumnDef="col3">
          <mat-header-cell *matHeaderCellDef class="table-header-cell" mat-sort-header> Cell 3</mat-header-cell>
          <mat-cell *matCellDef="let row" class="example-cell" > {{row.interniAkt}} </mat-cell>
        </ng-container>
    
        <mat-header-row *matHeaderRowDef="displayedColumns" class="example-header-row"></mat-header-row>
        <mat-row class="example-row" *matRowDef="let row; columns: displayedColumns;"></mat-row>
      </mat-table>
    
    0 讨论(0)
提交回复
热议问题