【问题标题】:Angular Material table dataSource not being rendered角度材质表数据源未呈现
【发布时间】:2019-07-10 12:30:16
【问题描述】:

我可以正确显示 dataSource2(角度库中的示例), 但 dataSource 变量(我的数据源)不呈现。

我不知道为什么会这样。我已经让两个数据源看起来很相似,并且我使用角度材料库的方式也相似。我需要一些输入;这是材质库的工作方式吗?

See image - Message on dataSource not rendered

我的 .ts 文件

import { Component, OnInit, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BackendcallingService } from './../backendcalling.service';
import { AvailableRolesEntity, RoleFunctionsEntity, ChildRolesEntity } from 'src/models/AvailableRoles.model';
import { MatTableDataSource } from '@angular/material';

@Component({
  selector: 'app-roles',
  templateUrl: './roles.component.html',
  styleUrls: ['./roles.component.css']
})
export class RolesComponent implements OnInit {


  jsondata: any;
  jsonifiedrole: any;
  availableRoles: any;
  jsonifiedavailableRoles: any[];

  dataSource: any = [];
  dataSource2: any =[];

displayedColumns: string[] = ['position', 'name', 'weight', 'symbol']; // columns for example dataSource2
        columnsToDisplay = ['name', 'priority', 'editUrl', 'active', 'createdId'];  // columns for my dataSource

  ELEMENT_DATA: any[] = [
    { position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' },
    { position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' },
    { position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' },
    { position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be' },
  ];

  svc: any;
  http: any;

  response: any;



  constructor(http: HttpClient, svc: BackendcallingService) {
    this.http = http;
    this.svc = svc;

  }

  ngOnInit() {




    this.http.get('http://localhost:8080/epsdk-app-os/get_role?role_id=undefined')
      .subscribe(response => {
        this.response = response;

        this.jsondata = (JSON.parse(response.data));
        this.role = this.jsondata.role;
        this.jsonifiedrole = JSON.parse(this.role);

        this.availableRoles = this.jsondata.availableRoles;
        this.jsonifiedavailableRoles = JSON.parse(this.availableRoles);


        this.dataSource2 = new MatTableDataSource<any>(this.ELEMENT_DATA); // example data source
        this.dataSource = new MatTableDataSource<any>(this.jsonifiedavailableRoles); //my datasource

        console.log(this.jsondata);
        console.log(this.role);
        console.log(this.jsonifiedrole);
        console.log(this.availableRoles);
        console.log(this.jsonifiedavailableRoles);

      })

  }

}

jsonifiedavailableRoles 看起来像这样。我不知道它与呈现为 dataSource2 的 ELEMENT_DATA 有何不同

See image - jsonifiedavailableRoles looks like this

我的 .html 文件

<div>
  <table mat-table [dataSource]="dataSource2" class="mat-elevation-z8">
    <ng-container matColumnDef="position">
      <th mat-header-cell *matHeaderCellDef> No. </th>
      <td mat-cell *matCellDef="let element"> {{element.position}} </td>
    </ng-container>

    <ng-container matColumnDef="name">
      <th mat-header-cell *matHeaderCellDef> Name </th>
      <td mat-cell *matCellDef="let element"> {{element.name}} </td>
    </ng-container>

    <ng-container matColumnDef="weight">
      <th mat-header-cell *matHeaderCellDef> Weight </th>
      <td mat-cell *matCellDef="let element"> {{element.weight}} </td>
    </ng-container>

    <ng-container matColumnDef="symbol">
      <th mat-header-cell *matHeaderCellDef> Symbol </th>
      <td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
    </ng-container>

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

</div>

<div>
  <br>

  <table> mat-table [dataSource]="dataSource">
<ng-container matColumnDef="name">
  <th mat-header-cell *matHeaderCellDef> Name </th>
  <td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>

<ng-container matColumnDef="priority">
  <th mat-header-cell *matHeaderCellDef> Priority </th>
  <td mat-cell *matCellDef="let element"> {{element.priority}} </td>
</ng-container>

<ng-container matColumnDef="editUrl">
  <th mat-header-cell *matHeaderCellDef> Edit </th>
  <td mat-cell *matCellDef="let element"> {{element.editUrl}} </td>
</ng-container>


<ng-container matColumnDef="active">
  <th mat-header-cell *matHeaderCellDef> Active? </th>
  <td mat-cell *matCellDef="let element"> {{element.active}} </td>
</ng-container>

<ng-container matColumnDef="createdId">
  <th mat-header-cell *matHeaderCellDef> Delete </th>
  <td mat-cell *matCellDef="let element"> {{element.createdId}} </td>
</ng-container>

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

【问题讨论】:

  • 您的jsonifiedavailableRoles 不符合显示的列
  • 您的问题解决了吗?
  • @dileepkumarjami 仍然不起作用。请参阅我按照您和 Ramesh 的建议在问题代码中所做的新更改,我更改了 columnsToDisplay = ['name', 'priority', 'editUrl', 'active', 'createdId'];

标签: javascript angular typescript angular-material frontend


【解决方案1】:

同时更改html,

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

同样在你的 ngOnInit 中的 get() 中,传递 this.jsonifiedavailableRoles

 this.dataSource = new MatTableDataSource<any>(this.jsonifiedavailableRoles);

【讨论】:

  • 同样的错误。做了这个改变: this.dataSource = new MatTableDataSource(this.jsonifiedavailableRoles);为什么在 columnsToDisplay 的末尾有额外的分号? (但即使更改也不起作用) this .jsonifiedavailableRoles 已经是一个数组,如问题中的控制台屏幕截图所示。不确定复制到另一个阵列是否可行。此外,该数组本身由其中的复杂 json 对象组成。不使用所有属性应该不是问题。
  • 您是否更改了columnstodisplay
  • 编辑了我的回复。是的,我对 columnsToDisplay 进行了更改
  • 我的意思是*matHeaderRowDef="columnsToDisplay"
  • 是的,它一直是 *matHeaderRowDef="columnsToDisplay"。 *matHeaderRowDef="displayedColumns" 用于 dataSource2
【解决方案2】:

columnsToDisplay 应该与您的 html 标记匹配。

columnsToDisplay = ['name', 'priority', 'editUrl', 'active', 'createdId'];  // columns for my dataSource

<td mat-cell *matCellDef="let element"> {{element.name}} </td>

【讨论】:

  • 我做出了改变。还是同样的错误。我按照您的建议更改了问题代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多