使用 Angular 10 将 JSON 值映射到要在 HTML 中显示的新字符串
Mapping a JSON Value into a new string to display in HTML using Angular 10
我有这个示例 JSON 来自我的后端的响应,使用 Spring Boot.
“类别”可以是 1 或 2。
1代表Notification,2代表FAQ
{
"pageNumber": 0,
"size": 5,
"totalPages": 39,
"content": [
{
"notifBaseId": {
"notifId": "11115"
},
"notifLngCd": "en",
"title": "Test",
"ctt": lorem ipsum",
"category": "2",
},
根据 json 响应,这是我使用 ng g 模型命令创建的 angular 模型
export class NotifBase {
notifID : string;
notifLngCd : string;
title : string;
ctt : string;
category : string;
}
这是service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { NotifBase } from '../model/notif-base';
@Injectable({
providedIn: 'root'
})
export class NotifBaseService {
private URL = 'http://localhost:1001/api/notif/base';
constructor ( private httpClient : HttpClient ) { }
/**
* this is for default way for ngx-datatable
*
* */
getNotifBaseList() : Observable<NotifBase[]> {
return this.httpClient.get<GetResponse>(this.URL).pipe(
map(response => response.content),
);
}
}
/*
This is for mapping the JSON endpoint
*/
interface GetResponse {
content : [];
}
这是component.ts
export class NotifBaseComponent implements OnInit {
//Table Rows
notifRows: NotifBase[];
// table Column
notifColumns = [
{ name : 'NotifId', prop : 'notifBaseId.notifId'},
{ name : 'Languange', prop : 'notifLngCd'},
{ name : 'Notif title', prop : 'title'},
{ name : 'Content', prop : 'ctt'},
{ name : 'Category', prop : 'category},
];
selected = [];
SelectionType = SelectionType;
ColumnMode = ColumnMode;
constructor( private notifBaseService: NotifBaseService ){
}
ngOnInit(){
this.loadAll();
}
loadAll(){
this.notifBaseService.getNotifBaseList().subscribe(
data => {
this.notifRows = data;
}
);
}
/**
* This is for selected rows event
*/
onSelect({ selected }) {
console.log('Select Event', selected, this.selected);
}
onActivate(event) {
console.log('Activate Event', event);
}
}
并且在 HTML 我使用 swimlane ngx-datatable 来显示我所有的数据。
<!-- Ngx datatable -->
<ngx-datatable
class="material"
[rows]="notifRows"
[columnMode]="'standard'"
[columns]="notifColumns"
[headerHeight] ="50"
[footerHeight]="50"
[rowHeight]="50"
[scrollbarH]="true"
[scrollbarV]="true"
[selected]="selected"
[selectionType]="SelectionType.single"
>
</ngx-datatable>
使用我当前的代码,我的数据可以正确显示。
我不知道的一件事是如何 map/display 我的类别值
而不是浏览器中的 displaying/rendering 1 或 2,我想将它们显示为它们的名称(通知或常见问题解答)
您可以修改映射您的响应以接收您想要的内容,我会这样做:
getCategoryName(id: string): string {
if (id === '1') {
return 'Notification';
}
if (id === '2') {
return 'FAQ';
}
return 'Unknown';
}
getNotifBaseList() : Observable<NotifBase[]> {
return this.httpClient.get<GetResponse>(this.URL).pipe(
map(response => {
return response.content.map(item => ({
...item,
category: this.getCategoryName(item.category)
});
}),
);
}
我有这个示例 JSON 来自我的后端的响应,使用 Spring Boot. “类别”可以是 1 或 2。 1代表Notification,2代表FAQ
{
"pageNumber": 0,
"size": 5,
"totalPages": 39,
"content": [
{
"notifBaseId": {
"notifId": "11115"
},
"notifLngCd": "en",
"title": "Test",
"ctt": lorem ipsum",
"category": "2",
},
根据 json 响应,这是我使用 ng g 模型命令创建的 angular 模型
export class NotifBase {
notifID : string;
notifLngCd : string;
title : string;
ctt : string;
category : string;
}
这是service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { NotifBase } from '../model/notif-base';
@Injectable({
providedIn: 'root'
})
export class NotifBaseService {
private URL = 'http://localhost:1001/api/notif/base';
constructor ( private httpClient : HttpClient ) { }
/**
* this is for default way for ngx-datatable
*
* */
getNotifBaseList() : Observable<NotifBase[]> {
return this.httpClient.get<GetResponse>(this.URL).pipe(
map(response => response.content),
);
}
}
/*
This is for mapping the JSON endpoint
*/
interface GetResponse {
content : [];
}
这是component.ts
export class NotifBaseComponent implements OnInit {
//Table Rows
notifRows: NotifBase[];
// table Column
notifColumns = [
{ name : 'NotifId', prop : 'notifBaseId.notifId'},
{ name : 'Languange', prop : 'notifLngCd'},
{ name : 'Notif title', prop : 'title'},
{ name : 'Content', prop : 'ctt'},
{ name : 'Category', prop : 'category},
];
selected = [];
SelectionType = SelectionType;
ColumnMode = ColumnMode;
constructor( private notifBaseService: NotifBaseService ){
}
ngOnInit(){
this.loadAll();
}
loadAll(){
this.notifBaseService.getNotifBaseList().subscribe(
data => {
this.notifRows = data;
}
);
}
/**
* This is for selected rows event
*/
onSelect({ selected }) {
console.log('Select Event', selected, this.selected);
}
onActivate(event) {
console.log('Activate Event', event);
}
}
并且在 HTML 我使用 swimlane ngx-datatable 来显示我所有的数据。
<!-- Ngx datatable -->
<ngx-datatable
class="material"
[rows]="notifRows"
[columnMode]="'standard'"
[columns]="notifColumns"
[headerHeight] ="50"
[footerHeight]="50"
[rowHeight]="50"
[scrollbarH]="true"
[scrollbarV]="true"
[selected]="selected"
[selectionType]="SelectionType.single"
>
</ngx-datatable>
使用我当前的代码,我的数据可以正确显示。 我不知道的一件事是如何 map/display 我的类别值 而不是浏览器中的 displaying/rendering 1 或 2,我想将它们显示为它们的名称(通知或常见问题解答)
您可以修改映射您的响应以接收您想要的内容,我会这样做:
getCategoryName(id: string): string {
if (id === '1') {
return 'Notification';
}
if (id === '2') {
return 'FAQ';
}
return 'Unknown';
}
getNotifBaseList() : Observable<NotifBase[]> {
return this.httpClient.get<GetResponse>(this.URL).pipe(
map(response => {
return response.content.map(item => ({
...item,
category: this.getCategoryName(item.category)
});
}),
);
}