0

I have this example JSON response from my backend that using Spring Boot. the "category" can be 1 or 2. 1 stand for Notification and 2 stands for FAQ

{
"pageNumber": 0,
"size": 5,
"totalPages": 39,
"content": [
    {
        "notifBaseId": {
            "notifId": "11115"
        },
        "notifLngCd": "en",
        "title": "Test",
        "ctt": lorem ipsum",
        "category": "2",
    },

based on the json response this is the angular model that i created using ng g model command

export class NotifBase {
notifID : string;
notifLngCd : string;
title : string;
ctt : string;
category : string;
}

This is the 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 : [];
}

This is the 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);
}

}

and at the HTML im using swimlane ngx-datatable to show my all my data.

<!-- 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>

with my current code, my data displayed correctly. one thing that I don't know is how can I map/display my category value instead of displaying/rendering 1 or 2 in the browser, I would like to display them as their name (notification or FAQ)

1 Answer 1

2

You can modify mapping your response to receive what you want, I'd do something like that:

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)
            });
        }),         
    );
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your help Actually I'm new to code especially javascript framework, typescript, and such but I'm eager to learn. Now I able to display what I wanted but I get one new problem which is the "...item" that shows a red curly underline on my VSCode. After I'm ng serve, the CLI shows this error : error TS2698: Spread types may only be created from object types. ...item, but on the browser, my app runs fine, every rows is shown and shows no error/warning at the console
@ChristopherJordan I assume that type you defined is not correct. I'd like to know what you've defined in GetResponse and NotifBase types.
actually i just defined GetResponse as an interface in the service.ts file like the code above and NotifBase as an angular model. (I've added the model that I created above).
@ChristopherJordan I mean I'd like to see these files. Could you upload your app or at least this part of it on some code hosting service i.e. stackblitz?
here is my stackblizt
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.