代码之家  ›  专栏  ›  技术社区  ›  Lorem Ipsum

角度材质+GAPI:表格显示行,但单元格为空

  •  1
  • Lorem Ipsum  · 技术社区  · 7 年前

    我正在集成angular material和Google api。我想在一个表中显示api的响应。问题是没有填充mat表行的mat单元格,但显示了该行。这是我的桌子:

        <mat-table [dataSource]="dataSource">
    
        <ng-container matColumnDef="name">
          <mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
          <mat-cell *matCellDef="let user"> {{user.name}} </mat-cell>
        </ng-container>
        <ng-container matColumnDef="age">
          <mat-header-cell *matHeaderCellDef> Age </mat-header-cell>
          <mat-cell *matCellDef="let user"> {{user.age}} </mat-cell>
        </ng-container>
        <ng-container matColumnDef="city">
          <mat-header-cell *matHeaderCellDef> City </mat-header-cell>
          <mat-cell *matCellDef="let user"> {{user.city}} </mat-cell>
        </ng-container>
        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
      </mat-table>
    

    这是我创建的一个界面:

        export interface User { name: string; age: number; city: string; }
    

    这是与表关联的组件:

        export class UsertableComponent implements OnInit, OnChanges {
      @Input() apiReady: boolean;
    
      dataSource: UserDataSource;
      displayedColumns = ['name', 'age', 'city'];
    
      constructor(private userService: UserService) {
    
      }
    
      ngOnInit() {
    
      }
    
      ngOnChanges(changes: SimpleChanges) {
        if (this.apiReady) {
          this.dataSource = new UserDataSource(this.userService);
        }
      }
    }
    
    export class UserDataSource extends DataSource<any> {
      constructor(private userService: UserService) {
        super();
      }
    
      connect(): Observable<User[]> {
        return this.userService.getUsers();
      }
    
      disconnect() {
    
      }
    }
    

    最后,这里是噩梦。调用gapi并返回数据的服务。我可以确保api响应,尤其是这一行代码 回答后果用户[0]。名称全名 与具有值的字符串对象相对应。

    export class UserService {
      private serviceUrl = environment.apiUrl + '/getcust/';
      private apiLoaded = false;
      constructor(private http: HttpClient) { }
    
      getUsers(): Observable<User[]> {
        /* // THIS WORKS!!!
            const u: User = {
              'name': 'ss',
              'city': 'città',
              'age': 3
            };
            return Observable.create(observable => {
              observable.next([u]);
              observable.complete();
            });
         */
    
        return Observable.create(observable => { // THIS DOESN'T WORK
          return gapi.client.directory.users.list({
            'customer': 'my_customer',
            'maxResults': 1,
            'orderBy': 'email'
          }).then(response => {
    
            const u: User = {
              'name': response.result.users[0].name.fullName,
              'city': 'citta',
              'age': 3
            };
    
            observable.next([u]);
            observable.complete();
          });
        });
      }
    }
    

    正如您在评论中看到的,被评论的行有效,其他行无效。

    Result with empty cells

    Result with populated cells, without calling GAPI

    1 回复  |  直到 5 年前
        1
  •  2
  •   Lorem Ipsum    7 年前

    解决方案是:

    导入此项:

    import {ChangeDetectorRef} from '@angular/core';
    

    组件的构造函数:

    constructor(private cd: ChangeDetectorRef) {
      }
    

    在ngInit中初始化数据源:

        ngOnInit() {
        this.dataSource = new UserDataSource();
    }
    

    使用 行为主体 (和方法 A可观测 )要随时更改数据源的内容,但当表中的数据更改时,不要忘记调用:

    这cd。detectChanges();

    更新单元格内容的步骤