【问题标题】:How can I make sort table work with Angular 8 + data from firebase?如何使排序表与来自 firebase 的 Angular 8 + 数据一起使用?
【发布时间】:2019-12-05 13:10:39
【问题描述】:

我有一个带有来自 firebase 数据的材料表的角度 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.

【问题讨论】:

    标签: javascript angular firebase google-cloud-firestore angularfire2


    【解决方案1】:

    问题出在 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]
              }
            }, {})
          ]
        }, [])
      }
    

    【讨论】:

      【解决方案2】:

      您需要像这样在表中以及要对数据进行排序的列上指定 mat 排序。

      <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;
      
        });
      }
      

      它现在应该可以工作了!

      【讨论】:

      • 它不起作用,我在 sortData() 函数中需要什么?第一个 this.sort 也没有被识别
      • 你不需要实现它。仅供参考。只关注table标签中的matSort
      • 还是不行
      • 在 getUserData() 中试试这个。 setTimeout(() => this.dataSource.sort = this.sort);
      • 它给了我一个错误:属性 'setTimeout' 在类型 'void'.ts(2339) 上不存在
      猜你喜欢
      • 2019-01-12
      • 1970-01-01
      • 2020-09-13
      • 2015-08-31
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多