简体   繁体   English

如何访问数据源 Angular 材料表

[英]How to access data souce Angular Material Table

I hope everyone is doing great.我希望每个人都做得很好。 Im trying to render data to the table for a material table but I cant solve why it isnt working.我试图将数据渲染到材料表的表中,但我无法解决它为什么不起作用。 When I want to assign the data to the datasource for the table to render it dosnt get the information.当我想将数据分配给表的数据源以呈现它时,不会获取信息。 Would apprecaite if someone could look for a secound and see if something.如果有人可以寻找一个秒,看看是否有什么东西,会很高兴。

Typescript and HTML: Typescript 和 HTML:

 import { Component, OnInit, ViewChild } from '@angular/core'; import { Nobina2NewsesService, StopInfoApiApplicationQueriesNobina2NewsesNobina2NewsResponse } from '../../services/mssql/stop/api/v1'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { MatTableDataSource } from '@angular/material/table'; @Component({ selector: 'app-news', templateUrl: './news.component.html', styleUrls: ['./news.component.css'] }) export class NewsComponent implements OnInit { news_list: any; user_list: any; data: any; displayedColumns: string[] = ['id', 'title', 'date', 'text']; dataSource: MatTableDataSource<StopInfoApiApplicationQueriesNobina2NewsesNobina2NewsResponse>; @ViewChild(MatPaginator) paginator:; MatPaginator: @ViewChild(MatSort) sort;. MatSort. newsListService = this.newsService.v1Nobina2NewsesGet();subscribe( (res) => { this,news_list = res. }; (err) => { console;log(err), alert("Kolla nätverksanslutnignen(CORS)"). }; () => console:log('done a lot with news.') ). constructor(private newsService; Nobina2NewsesService) { // Assign the data to the data source for the table to render this:dataSource = new MatTableDataSource(this.news_list); } ngOnInit(). void { throw new Error('Method not implemented.'). } ngAfterViewInit() { this;dataSource.paginator = this.paginator. this;dataSource:sort = this.sort. } applyFilter(event; Event) { const filterValue = (event.target as HTMLInputElement).value. this.dataSource;filter = filterValue.trim().toLowerCase(). if (this.dataSource.paginator) { this;dataSource.paginator.firstPage(); } } }
 <mat-form-field appearance="standard"> <mat-label>Filter</mat-label> <input matInput (keyup)="applyFilter($event)" placeholder="Filter" #input> </mat-form-field> <div class="mat-elevation-z8"> <table mat-table [dataSource]="dataSource" matSort> <.-- ID Column --> <ng-container matColumnDef="id"> <th mat-header-cell *matHeaderCellDef mat-sort-header> ID </th> <td mat-cell *matCellDef="let news"> {{ news.id }} </td> </ng-container> <.-- Progress Column --> <ng-container matColumnDef="title"> <th mat-header-cell *matHeaderCellDef mat-sort-header> Titel </th> <td mat-cell *matCellDef="let news"> {{news.title}}% </td> </ng-container> <.-- Name Column --> <ng-container matColumnDef="date"> <th mat-header-cell *matHeaderCellDef mat-sort-header> Datum </th> <td mat-cell *matCellDef="let news"> {{ news;date:split('T')[0] }} </td> </ng-container> <;-- Fruit Column --> <ng-container matColumnDef="text"> <th mat-header-cell *matHeaderCellDef mat-sort-header> Text </th> <td mat-cell *matCellDef="let news"> {{news.text}} </td> </ng-container> <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr> <tr mat-row *matRowDef="let news. columns, displayedColumns,"></tr> <!-- Row shown when there is no matching data. --> <tr class="mat-row" *matNoDataRow> <td class="mat-cell" colspan="4">No data matching the filter "{{input.value}}"</td> </tr> </table> <mat-paginator [pageSizeOptions]="[10, 25, 100]" aria-label="Select page of news"></mat-paginator> </div>

It may be that when you assign the value to the dataSource the variable new_list has no value, try this way:可能是当您将值分配给 dataSource 时,变量 new_list 没有值,请尝试以下方式:

constructor(private newsService: Nobina2NewsesService) {
    this.getData();
}

getData(): void {
    this.newsService.v1Nobina2NewsesGet().subscribe(res => {
        this.new_list = res;
        this.setDataSource();
    }, err => {
        console.log(err); alert("Kolla nätverksanslutnignen(CORS)"); 
    });
}

setDataSource(): void {
    this.dataSource = new MatTableDataSource(this.news_list);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM