Источник данных таблицы угловых материалов не отображается - PullRequest
0 голосов
/ 16 февраля 2019

Я могу получить dataSource2 (пример из угловой библиотеки) для корректного отображения, но переменная dataSource (мой источник данных) не отображается.

Я не уверен, почему это так.Я сделал оба источника данных похожими, и способ использования библиотеки угловых материалов также схож.Мне нужен вклад;работает ли библиотека материалов?

См. изображение - сообщение об источнике данных не отображается

My .ts file

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 выглядит следующим образом.Я не знаю, чем он отличается от ELEMENT_DATA, который отображается как dataSource2

Смотрите изображение - jsonifiedavailableRoles выглядит следующим образом

Мой файл .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>

Ответы [ 3 ]

0 голосов
/ 16 февраля 2019

Также измените HTML,

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

Также в вашем get () внутри ngOnInit, передайте this.jsonifiedavailableRoles

 this.dataSource = new MatTableDataSource<any>(this.jsonifiedavailableRoles);
0 голосов
/ 17 февраля 2019

Это была опечатка.mat-table [dataSource] = "dataSource"> теперь изменен на И это работает!

0 голосов
/ 16 февраля 2019

columnsToDisplay должно соответствовать вашей HTML-разметке.

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

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