簡體   English   中英

如何使排序表與來自 firebase 的 Angular 8 + 數據一起使用?

[英]How can I make sort table work with Angular 8 + data from firebase?

我有一個帶有來自 firebase 數據的材料表的 angular PWA,顯示了數據並且分頁器正在工作,但我也想對其數據進行排序,但 mat-sort 不起作用。 它顯示了對數據進行排序的箭頭,但單擊箭頭時,數據未排序。

我遵循了 Angular Material 的文檔。

組件模板中的表格:

        <ng-container matColumnDef="movimento">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Tipo de movimento</th>
            <td mat-cell *matCellDef="let movimento"> {{movimento.mapValue.fields.tipo_movimento.stringValue}}
            </td>
        </ng-container>

        <ng-container matColumnDef="valor">

            <th mat-header-cell *matHeaderCellDef mat-sort-header>Valor</th>
            <td mat-cell *matCellDef="let movimento">
                {{movimento.mapValue.fields.valor.doubleValue | number:'1.1-2'}}
                {{movimento.mapValue.fields.valor.integerValue}} €
            </td>
        </ng-container>

        <ng-container matColumnDef="data">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Data</th>
            <td mat-cell *matCellDef="let movimento"> {{movimento.mapValue.fields.data.timestampValue | date}}
            </td>
        </ng-container>

        <ng-container matColumnDef="desc">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Descrição</th>
            <td mat-cell *matCellDef="let movimento"> {{movimento.mapValue.fields.desc.stringValue}}</td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>


    </table>
    <mat-paginator [pageSizeOptions]="[10, 20, 40]" showFirstLastButtons></mat-paginator>

組件:

@Component({
  selector: 'app-movimentar',
  templateUrl: './visualizar-movimentos.component.html',
  styleUrls: ['./visualizar-movimentos.component.css']
})
export class VisualizarMovimentosComponent implements OnInit {
  user: firebase.User;

  userData;

  displayedColumns: string[] = ['movimento', 'valor', 'data', 'desc'];

  dataSource = new MatTableDataSource<Movimentos>();


  @ViewChild(MatPaginator, { static: false })
  set paginator(value: MatPaginator) {
    this.dataSource.paginator = value;
  }

  @ViewChild(MatSort, { static: false }) sort: MatSort;


  constructor(private auth: AuthService,
    private router: Router,
    private afs: AngularFirestore) { }

  ngOnInit() {
    this.auth.getUserState()
      .subscribe(user => {
        this.user = user;

        this.getUserData();
      });

  }

  /* loadLessonsPage() {
    this.dataSource._orderData;
    debugger
  } */

  getUserData(): void {
    this.auth.getUserData(this.user.uid)
      .subscribe(user => {
        this.userData = user;

        this.dataSource = new MatTableDataSource(this.userData._document.proto.fields.movimentos.arrayValue.values);

        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;

      });


  }

  login() {
    this.router.navigate(['login']);
  }

  logout() {
    this.auth.logout();
    this.router.navigate(['inicio']);
  }

  register() {
    this.router.navigate(['/register']);
  }

  trackByUid(index, item) {
    return item.uid;
  }



}```

Thanks for the help in advance.

問題在於來自 firebase 的數據,它與 mat 排序不兼容。 要解決它,您需要一張這樣的地圖:

這是地圖

    return data.reduce((acc, value) => {
      return [
        ...acc,
        Object.entries(value.mapValue.fields).reduce((accValue, [key, value]) => {
          return {
            ...accValue,
            [key]: Object.values(value)[0]
          }
        }, {})
      ]
    }, [])
  }

您需要像這樣在表中以及要對數據進行排序的列上指定 mat sort 。

<table matSort (matSortChange)="sortData($event)">
</table>

用於按列排序

<th mat-header-cell *matHeaderCellDef mat-sort-header>

在您的 .ts 文件中

 @ViewChild(MatSort, { static: false }) set content(content: ElementRef) {
  this.sort = content;
  if (this.sort) {
   this.dataSource.sort = this.sort;
  }
 }

而你在加載數據的時候就已經完成了這部分

getUserData(): void {
this.auth.getUserData(this.user.uid)
  .subscribe(user => {
    this.userData = user;

    this.dataSource = new MatTableDataSource(this.userData._document.proto.fields.movimentos.arrayValue.values);

    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

  });
}

它現在應該可以工作了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM