gpt4 book ai didi

javascript - Angular : No Error but not showing data in Material Table

转载 作者:行者123 更新时间:2023-11-30 14:03:53 25 4
gpt4 key购买 nike

我的控制台没有错误,但无法在 Material 数据表中显示它。在代码中查看了一段时间,但无法弄清楚。我唯一想到的是 API 不对。 xxxxx 表示 API 的一些 URL。一律不允许我显示真实链接。

restaurant.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Restaurant } from '../models/restaurant.model';

@Injectable({
providedIn: 'root'
})
export class RestaurantService {
private restaurantsUrl = 'https://xxxxxx/getAllServices.php';

constructor(private http: HttpClient) { }

getRestaurants(): Observable<Restaurant[]> {
return this.http.get<Restaurant[]>(this.restaurantsUrl);
}
}

餐厅表.component.ts

import { Component, ViewChild,OnInit } from '@angular/core';
import { RestaurantService } from '../services/restaurant.service';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import {DataSource} from '@angular/cdk/collections';
import { Restaurant } from '../models/restaurant.model';


@Component({
selector: 'app-restaurants-table',
templateUrl: './restaurants-table.component.html',
styleUrls: ['./restaurants-table.component.css']
})

export class RestaurantsTableComponent implements OnInit {

dataSource = new RestaurantDataSource(this.restaurantService);
displayedColumns = ['id', 'ime_restorana', 'opis', 'broj_telefona', 'adresa_restorana','edits'];

constructor(private restaurantService: RestaurantService) {
}

ngOnInit() {
}

}

export class RestaurantDataSource extends DataSource<any> {

constructor(private restaurantService: RestaurantService) {
super();
}

connect(): Observable<Restaurant[]> {
return this.restaurantService.getRestaurants();
}

disconnect() {}

}

restaurant-table.component.html

<div class="">
<mat-table [dataSource]="dataSource">

<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> ID </mat-header-cell>
<mat-cell *matCellDef="let restaurant"> {{ restaurant.id }} </mat-cell>
</ng-container>

<ng-container matColumnDef="ime_restorana">
<mat-header-cell *matHeaderCellDef> Naziv </mat-header-cell>
<mat-cell *matCellDef="let restaurant"> {{ restaurant.ime_restorana }} </mat-cell>
</ng-container>

<ng-container matColumnDef="opis">
<mat-header-cell *matHeaderCellDef> Opis </mat-header-cell>
<mat-cell *matCellDef="let restaurant"> {{ restaurant.opis }} </mat-cell>
</ng-container>

<ng-container matColumnDef="broj_telefona">
<mat-header-cell *matHeaderCellDef> Broj Telefona </mat-header-cell>
<mat-cell *matCellDef="let restaurant"> {{ restaurant.broj_telefona }} </mat-cell>
</ng-container>

<ng-container matColumnDef="adresa_restorana">
<mat-header-cell *matHeaderCellDef> Adresa </mat-header-cell>
<mat-cell *matCellDef="let restaurant"> {{ restaurant.adresa_restorana }} </mat-cell>
</ng-container>

<ng-container matColumnDef="edits">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let element">
<button mat-raised-button class="edit-btn"><mat-icon>edit</mat-icon></button>
<button mat-raised-button class="edit-btn"><mat-icon>remove_red_eye</mat-icon></button>
</td>
</ng-container>

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

</mat-table>
</div>

和 user.model.ts

export interface User {
name: string;
email: string;
phone: string;
company: {
name: string;
}
}

最佳答案

您当前的 dataSourceRestaurantDataSource 类的实例,您想要的(我想)是餐厅的实际列表,因此

dataSource = new RestaurantDataSource(this.restaurantService).connect();

但是,您的 connect() 方法返回一个 Observable,而不是列表本身,因此您需要在模板中使用 async 管道

<mat-table [dataSource]="dataSource | async">

关于javascript - Angular : No Error but not showing data in Material Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com