Angular 6 display data with HttpClient, RxJS and GroupBy - json

I'm using Ruby on Rails API app for the backend and Angular 6 for the front. this is the json respond I get from the backend:
loclhost3000/courses.json
[
{
id:1,
title:"Introduction",
author:"Dana Inbar",
segments:[
]
},
{
id:2,
title:"Master the ELN",
author:"Dana Inbar",
segments:[
]
},
{
id:3,
title:"Master the Inventory",
author:"Dana Inbar",
segments:[
{
id:1,
unit_id:1,
unit_title:"Introduction",
name:"Lesson 1: Introduction to the inventory module- looking at one collection",
data:"www.video01.com/vid.avi"
},
{
id:2,
unit_id:2,
unit_title:"Inventory Customisation",
name:"Lesson 2: Setting up custom collections",
data:"www.video02.com/vid.avi"
},
{
id:3,
unit_id:2,
unit_title:"Inventory Customisation",
name:"Lesson 3: Adding a custom field",
data:"www.video03.com/vid.avi"
},
{
id:4,
unit_id:2,
unit_title:"Inventory Customisation",
name:"Lesson 4: Creating derived collections",
data:"www.video04.com/vid.avi"
},
{
id:5,
unit_id:2,
unit_title:"Inventory Customisation",
name:"Lesson 5: Using repositories",
data:"www.video05.com/vid.avi"
},
{
id:6,
unit_id:2,
unit_title:"Inventory Customisation",
name:"Quiz",
data:"'[ { "
question 1":"___",
"answers":{
"1":"____",
"2":"____",
"3":"____"
},
"correct_answer":"2"
},
{
"question 2":"___",
"answers":{
"1":"____",
"2":"____"
},
"correct_answer":"1"
}
}
] ' "
}
]
}
]
I have Course model which has many Segments, and Segment is Video or Quiz.
I have course-list, course-detail and course-play components.
I have problem with the course-detail and I want the course-detail page to look like that: course-detail image
I thought I could do functions in course.service which use groupBy with unit_id and unit_title and do two ngFor (or ngFor group) but because I'm new to angular I don't know how is best to implement this.
I'm adding some files from the program which can help:
./courses/course.module
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { RouterModule, Routes } from '#angular/router';
import { MatSidenavModule } from '#angular/material/sidenav';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { CourseListComponent } from './course-list/course-list.component';
import { CourseDetailComponent } from './course-detail/course-detail.component';
import { CourseService } from './course.service';
import { CoursePlayComponent } from './course-play/course-play.component';
const coursesRoutes: Routes = [
{ path: 'courses', component: CourseListComponent },
{ path: 'courses/:id', component: CourseDetailComponent },
{ path: 'courses/:id/:segment_id', component: CoursePlayComponent }
]
#NgModule({
imports: [
CommonModule,
MatSidenavModule,
BrowserAnimationsModule,
RouterModule.forChild(
coursesRoutes
)
],
declarations: [
CourseListComponent,
CourseDetailComponent,
CoursePlayComponent
],
providers: [
CourseService
]
})
export class CourseModule { }
./courses/course
export interface ICourse {
course_id: number;
title: string;
autor: string;
segments: ISegment[];
}
export interface ISegment {
segment_id: number;
unit_id: number;
unit_title: string;
name: string;
type: string;
data: string;
}
./courses/course.service
import { Injectable } from '#angular/core';
import { HttpClient } from '#angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError, retry, groupBy, Filter } from 'rxjs/operators';
import { Course } from './course';
// Inject Data from Rails app to Angular app
#Injectable()
export class CourseService{
constructor(private http: HttpClient) { }
private url = 'http://localhost:3000/courses';
private courseUrl = 'http://localhost:3000/courses.json';
// Handle Any Kind of Errors
private handleError(error: HttpErrorResponse) {
// A client-side or network error occured. Handle it accordingly.
if (error.error instanceof ErrorEvent) {
console.error('An error occured:', error.error.message);
}
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong.
else {
console.error(
'Backend returned code ${error.status}, ' +
'body was ${error.error}');
}
// return an Observable with a user-facing error error message
return throwError(
'Something bad happend; please try again later.');
}
// Get All Courses from Rails API App
getCourses(): Observable<ICourse[]> {
const coursesUrl = `${this.url}` + '.json';
return this.http.get<ICourse[]>(coursesUrl)
.pipe(
catchError(this.handleError)
);
}
// Get Single Course by id. will 404 if id not found
getCourse(id: number): Observable<ICourse> {
const detailUrl = `${this.url}/${id}` + '.json';
return this.http.get<ICourse[]>(detailUrl)
.pipe(
catchError(this.handleError)
);
}
}
./courses/course-detail/course-detail.component
import { Component, OnInit } from '#angular/core';
import { ActivatedRoute, Router } from '#angular/router';
import { Course } from '../course';
import { CourseService } from '../course.service';
#Component({
selector: 'lg-course-detail',
templateUrl: './course-detail.component.html',
styleUrls: ['./course-detail.component.sass']
})
export class CourseDetailComponent implements OnInit {
course: ICourse;
errorMessage: string;
constructor(private courseService: CourseService,
private route: ActivatedRoute,
private router: Router) {
}
ngOnInit() {
const id = +this.route.snapshot.paramMap.get('id');
this.getCourse(id);
}
// Get course detail by id
getCourse(id: number) {
this.courseService.getCourse(id).subscribe(
course => this.course = course,
error => this.errorMessage = <any>error);
}
onBack(): void {
this.router.navigate(['/courses']);
}
}
./courses/course-detail/course-detail.html
<div id="main" *ngIf="course">
<div class="row" id="image">
<div class="col-lg-8">
<br>
<img src="./assets/images/lg-white.png" class="d-inline-block align-top" alt="">
</div>
</div>
<div class="row" id="header">
<div class="container text-center">
<br>
<h1>{{course.title}}</h1>
<br>
</div>
</div>
<div class="row justify-content-lg-center" id="progress">
<div class="container text-center">
<div class="progress">
<div class="progress-bar bg-white"></div>
</div>
<td>Your Progress</td>
<br><br><br>
</div>
</div>
<div class="row" id="body">
<div class="container">
<br>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#Curiculum" role="tab" data-toggle="tab">Curiculum</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#About" role="tab" data-toggle="tab">About this course</a>
</li>
</ul>
<br>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="Curiculum">
<h1>Course Overview</h1>
<br>
<ul *ngFor="let segment of course.segmentsByUnitId">
<ul>
<li id="title">Unit {{segment.unit_id}}: {{segment.unit_title}}</li>
<li>{{segment.name}}</li>
</ul>
</ul>
</div>
<div role="tabpanel" class="tab-pane fade" id="About">
<h1>Course Topics:</h1>
</div>
</div>
<br>
</div>
</div>
</div>
./courses/course-detail/course-detail.sass
$color: #FFFFFF
$bg-col: #5c0099
$prog-size: 10px
#image
background-color: $bg-col
color: $color
#header
background-color: $bg-col
color: $color
#body
background-color: $color
height: 100vh
max-width: initial
display: flex
#title
font-weight: bold
#progress
background-color: $bg-col
color: $color
font-size: $prog-size
.progress
height: 10px
left: 30%
.progress-bar
width: 10%
height: 20px
background-color: transparent

You can do it by using the below groupby pipes.
GroupBy pipe returns an object of a key/value pair.
https://github.com/danrevah/ngx-pipes#groupby
The installation process for ngx-pipes: https://github.com/danrevah/ngx-pipes#installation
Below is how I implemented it for my data:
My data:
tsks = [{
CompletedFlag: true,
ProgressFlag: false,
DueDate:"2014-10-02T13:12:37.027",
Signup: "2014-10-04T11:51:10.137",
ObMilestone: 1,
ObTask: 119,
AssignedToId: 0,
Company: "ABC Corp",
Description: "Lorem Ipsum",
Favourite: null,
FirstName: "Libin",
LastName: "J",
MilestoneName: "Blue",
OnboarderStatus: "Live",
Partner : null
},
{
CompletedFlag: true,
ProgressFlag: false,
DueDate:"2014-10-02T13:12:37.027",
Signup: "2014-10-04T11:51:10.137",
ObMilestone: 1,
ObTask: 119,
AssignedToId: 0,
Company: "ABC Corp",
Description: "Lorem Ipsum",
Favourite: null,
FirstName: "Libin",
LastName: "J",
MilestoneName: "Blue",
OnboarderStatus: "Live",
Partner : null
}];
My code to groupby "MilestoneName"
<div class="row" *ngFor="let task of tsks | groupBy: 'MilestoneName' | pairs">
<div>Group milestone name: {{task[0]}}</div>
<div class="row" *ngFor="let t of task[1]">
<div>Task: {{t.FirstName}}</div>
</div>
</div>

Related

How to display mock data in html in Angular

I have created mock service file and I want to display in my html but not really sure how to make it display properly so I'll be really appreciated If I can get any help or suggestion.
<div class="container2">
<div class="header" style="height: 400px">
<div class="content3">
<div>
<h1 class="kpi-title">90,346</h1>. // I'm trying to remove this hard code html
<p class="kpi-des">Users Right Now</p> // and display it from my mock data file.
</div>
<div>
<h1 class="kpi-title">250++l</h1>
<p class="kpi-des">Saved</p>
</div>
<div>
<h1 class="kpi-title">$34.5 mill</h1>
<p class="kpi-des">New User per Week</p>
</div>
</div>
</div>
</div>
TS
import { ProductService } from '../../data/product-suite.service';
export class MaxisProductSuiteComponent {
productService: ProductService[];
ngOnIT(){
}
product-suite.service.ts
export class ProductService {
productsuite: ProductSuite[] = [
{
id: 1,
title: '90,346',
description: 'Users',
},
{
id: 2,
title: '$34.5 mill',
description: 'Saved',
},
{
id: 3,
title: '250++',
description: 'New User per Week',
},
];
}
Please find the below code for your solutions:
create a json file assets folder with name output.json.
{
"result" : [
{
"id": 1,
"title": "90,346",
"description": "Users at Ford"
},
{
"id": 2,
"title": "$34.5 mill",
"description": "Saved for Ford"
},
{
"id": 3,
"title": "250++",
"description": "New User per Week"
},
{
"id": 4,
"title": "64%",
"description": "Users At Ford"
}
]
}
in service file write below code:
import { observable, Observable } from "rxjs";
import { MaxisProductSuite } from "src/Model/model";
import { HttpClient } from '#angular/common/http';
import { Injectable } from "#angular/core";
#Injectable()
export class MaxisProductService {
constructor(private http: HttpClient){}
getAllMaxisps():Observable<MaxisProductSuite> {
return this.http.get<MaxisProductSuite>("./assets/output.json");
}
}
then component file add below code:
import { DOCUMENT } from '#angular/common';
import { Component, Inject, OnInit } from '#angular/core';
import { MaxisProductSuite } from 'src/Model/model';
import { MaxisProductService } from 'src/service/MaxisProductService';
#Component({
selector: 'app-temp',
templateUrl: './temp.component.html',
styleUrls: ['./temp.component.scss']
})
export class TempComponent implements OnInit {
maxisps: MaxisProductSuite[];
public resultData:MaxisProductSuite=null;
constructor(#Inject(DOCUMENT) private document: Document, private service : MaxisProductService) {}
ngOnInit() {
this.service.getAllMaxisps().subscribe((res:MaxisProductSuite) => {
console.log(res);
this.resultData =res;
});
}
}
then HTMl file add below code:
<div *ngFor="let item of resultData?.result">
<div class="header" style="height: 400px">
<h1>{{item.id}}</h1>
<h2>{{item.title}}</h2>
<h3>{{item.description}}</h3>
</div>
add below model in model file
export interface MaxisProductSuite {
result : Result[]
}
export interface Result{
id?: number;
title: string;
description: string;
}
I hope it will help you to get the solution.
happy to HELP!
make your service in module provider or make it injectable "root".
inject the service in your component you want to display data in constructor as a dependency injection.
assign your component variable array with productService.
in your HTML loop about your data array using *ngFor=" let product of products".
use you product value in the interpolation {{ product.id }}.

Cannot read property 'setRowData' of undefined / Issue with HTML template?

Am maintaining an AG-GRID re-usable template with all necessary methods (clear filer/ CSV download/ Autofit..etc options) --- Base Template.
I have another AG-Grid template , which uses "Base Template" (thru dependency injection) and populate grid with a row of data based on searchString value. (Sample file below)
import { Component, OnInit, } from '#angular/core';
import { GridOptions, GridApi, } from "ag-grid-community";
import { ReportersgridComponent } from '../../commonpages/reportersgrid/reportersgrid.component'
#Component({
selector: 'app-reporters',
templateUrl: './reporters.component.html',
styleUrls: ['./reporters.component.scss']
})
export class ReportersComponent implements OnInit {
private reporterGrid: GridOptions;
constructor(public reportersGrid: ReportersgridComponent, ) {
this.reporterGrid = <GridOptions>{};
this.reporterGrid.enableSorting = true;
this.reporterGrid.enableFilter = true;
this.reporterGrid.enableColResize = true;
this.reporterGrid.columnDefs = this.reportersGrid.createColumnDefs();
this.reporterGrid.rowData = this.reportersGrid.createRowData();
}
ngOnInit() {
}
//Search Function
performSearch() {
let searchString = "";
this.populateFiteredReporter(searchString);
// this.reporterGrid.api.setRowData(reporterGrid.rowData)
}
populateFiteredReporter(searchString) {
this.reporterGrid.rowData = [
{ fullName: 'fName,mName,lName2', address: "address2", country: "country2", postcode: "postcode2", phone: "phone", email: "email", qualification: "MBBS", institution: "institution", department: "department" },
];
var str = JSON.stringify(this.reporterGrid.rowData);
console.log('data:' + str);
this.reporterGrid.api.setRowData(this.reporterGrid.rowData);
//this.reportersGrid.populateFiteredReporter(searchString);
}
}
In HTML of the above file, I am using "Base Template" s SELECTOR AS HTML TAG. (
<app-reportersgrid></app-reportersgrid>
) to display the grid portion.
Above gives error -> Cannot read property 'setRowData' of undefined.
Please note that if I replace Base Template's selector with base template's FULL HTML portion for ag-grid (which has (gridReady)="onGridReady($event)"), page works fine.
Can I get help to stick back to my original idea of keeping base template intact? (Note that all base template functions like export to csv, autofit etc works fine - those are coded in the base template along with OnGridReadty().)
Thanks in Advance..
ASJ.
22/10/2019 /* Template of reporters.component.html*/
<div>
<div class="col-md-12">
<div class="card">
<div class="card-header text-uppercase font-weight-bold">
Search
</div>
<div class="card-body">
<div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<span>Name <i>(First/Middle/Last)</i></span>
<input type="text" class="form-control" [(ngModel)]="reporterName">
</div>
</div>
</div>
</div>
</div>
<div class="card-footer">
<div class="row">
<div class="col-6 col-sm-4 col-md-2 col-xl mb-3 mb-xl-0">
<button type="button" class="btn btn-normal btn-primary" (click)="performSearch()" style="width: 100px; ">Search</button>
<button type="button" class="btn btn-normal btn-light" style="width: 100px;">Reset</button>
</div>
</div>
</div>
</div>
</div>
<app-reportersgrid></app-reportersgrid>
</div>
Base template - HTML of the AG-GRID
<div class="centered-content">
<div>
<ag-grid-angular #agGrid style="width: 100%; height: 358px;" class="ag-theme-balham"
[gridOptions]="reporterGrid" [enableSorting]="true" enableFilter [sideBar]="sideBar"
enableColResize [pagination]="true" [paginationPageSize]=10
rowDragManaged=true
(gridReady)="onGridReady($event)">
<ag-grid-column headerName="Name" field="fullName"></ag-grid-column>
<ag-grid-column headerName="Address" field="address" [width]="150"></ag-grid-column>
<ag-grid-column headerName="Country" field="country"></ag-grid-column>
<ag-grid-column headerName="Postcode" field="postCode"></ag-grid-column>
<ag-grid-column headerName="Phone" field="phone"></ag-grid-column>
<ag-grid-column headerName="Email" field="email"></ag-grid-column>
<ag-grid-column headerName="Qualification" field="qualification"></ag-grid-column>
<ag-grid-column headerName="Institution" field="institution"></ag-grid-column>
<ag-grid-column headerName="Department" field="department" [cellRenderer]="countryCellRenderer">
</ag-grid-column>
</ag-grid-angular>
</div>
</div>
/ReportersGridcomponent.ts file/
import { Component, OnInit } from '#angular/core';
//import { HttpClient, HttpErrorResponse } from '#angular/common/http';
import {GridOptions, GridApi, Grid} from "ag-grid-community";
#Component({
selector: 'app-reportersgrid',
templateUrl: './reportersgrid.component.html',
styleUrls: ['./reportersgrid.component.scss']
})
export class ReportersgridComponent implements OnInit {
private reporterGrid: GridOptions;
private gridApi:GridApi;
private gridColumnApi;
filterName: string | null;
constructor( ) {
this.reporterGrid = <GridOptions>{};
this.reporterGrid.enableSorting = true;
this.reporterGrid.enableFilter = true;
this.reporterGrid.enableColResize = true;
this.reporterGrid.columnDefs = this.createColumnDefs();
this.reporterGrid.rowData = this.createRowData();
}
ngOnInit() {
}
createColumnDefs() {
this.reporterGrid.columnDefs = [
{
headerName: "Name",
field: "fullName",
width:100,
},
{
headerName: "Address",
field: "address",
},
{
headerName: "Country",
field: "country",
},
{
headerName: "Postcode",
field: "postCode",
},
{
headerName: "Phone",
field: "phone",
},
{
headerName: "Email",
field: "email",
},
{
headerName: "Qualification",
field: "qualification",
},
{
headerName: "Institution",
field: "institution",
},
{
headerName: "Department",
field: "department",
}
];
return this.reporterGrid.columnDefs
}
createRowData(){
this.reporterGrid.rowData = [
// {fullName: 'fName,mName,lName',address:"address1",country: "country",postcode: "postcode",phone: "phone",email:"email",qualification:"MBBS",institution:"institution",department:"department"},
];
return this.reporterGrid.rowData;
}
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.autoSizeAll()
}
autoSizeAll() {
var allColumnIds = [];
this.gridColumnApi.getAllColumns().forEach(function(column) {
allColumnIds.push(column.colId);
});
this.gridColumnApi.autoSizeColumns(allColumnIds);
}
onCSVExport() {
this.reporterGrid.api.exportDataAsCsv();
}
onSearchTextChange(newData: string) {
this.reporterGrid.api.setQuickFilter(newData);
}
clearFilters() {
if (this.gridApi.isQuickFilterPresent())
{
this.gridApi.setQuickFilter('');
this.filterName="";
}
}
// populateFiteredReporter(searchString){
// this.reporterGrid.rowData = [
// {fullName: 'fName,mName,lName2',address:"address2",country: "country2",postcode: "postcode2",phone: "phone",email:"email",qualification:"MBBS",institution:"institution",department:"department"},
// ];
// var str= JSON.stringify(this.reporterGrid.rowData);
// console.log('data:'+str);
// this.reporterGrid.api.setRowData(this.reporterGrid.rowData);
// //return this.reporterGrid.rowData;
// }
}
The first thing you need to do is set your grid component to accept grid options from its parent:
#Input() public reporterGrid: GridOptions;
Here is an abbreviated version of what your grid component should look like. I suggest using properties or fields, instead of using methods to create your rows and columns. Just a suggestion.
export class ReportersgridComponent implements OnInit {
#Input() public reporterGrid: GridOptions;
private columnDefs = [
// put your column def json here
];
private rowDefs = [
// Put your row def json here
];
constructor() {
}
ngOnInit() {
this.reporterGrid.columnDefs = this.columnDefs;
this.reporterGrid.rowData = this.rowDefs;
}
// rest of your file
}
And your reporters component ts file should look like this:
export class ReportersComponent implements OnInit {
private reporterGridOptions: GridOptions;
constructor() {
this.reporterGridOptions = {
enableSorting: true,
enableFilter: true,
enableColResize: true,
};
}
ngOnInit() {
}
// other code...
}
In your reporter component html, you need to pass in this value through the element like this:
<app-reportersgrid [reporterGrid]="reporterGridOptions"></app-reportersgrid>
Another suggestion, I would take some time to properly name things, adjust casing, and format your code so it is easier to read. I hope this helps get you heading in the right direction. Basically you want to pass your options object to the grid component, so it can have access to it.
Also, you do not want to inject a component through a constructor. It will not be the same instance as the component in the template. Components are initialized and destroyed as they are rendered and removed from the view, and do not exist as singletons like services to.

How to post an object to an array in a json using Angular 6

So I'm testing Angular 6 functionality out for fun to learn it and running a json-server to load a db.json to a localhost server to acquire via service calls which you can see here
{
"customers": {
"testingTitle": "Testing Title",
"trainData":[
{
"id": 1,
"name": "Test Name 1",
"email": "customer001#email.com",
"tel": "0526252525"
},
{
"id": 2,
"name": "Test Name 2",
"email": "customer002#email.com",
"tel": "0527252525"
},
{
"id": 3,
"name": "Customer003",
"email": "customer003#email.com",
"tel": "0528252525"
},
{
"id": 4,
"name": "123",
"email": "123",
"tel": "123"
}
]
}
I have a test.service.ts as followed which picks up the service:
import { Injectable } from '#angular/core';
import {HttpClient, HttpResponse, HttpErrorResponse, HttpHeaders, HttpParams} from '#angular/common/http';
import { Observable } from 'rxjs/Rx';
import { catchError, map } from 'rxjs/operators';
import 'rxjs/add/observable/throw';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
class Test {
testingTitle: string;
trainData:[
{
id : number;
name: string;
email: string;
tel: string;
}
];
}
#Injectable({providedIn: 'root'})
export class TestService {
constructor(private http: HttpClient) {}
public getAllTests(): Observable<Test[]>{
const params = new HttpParams().set('_page', "*").set('_limit', "*");
return this.http.get<Test[]>("http://localhost:3000/customers", {params}).pipe(map(res => res));
}
public postTests(object) {
return this.http.post("http://localhost:3000/customers", object).subscribe(data => {console.log("POST Request is successful ", data);},error => {console.log("Error", error);});
}
}
I have my test.ts which controls my calls etc.
import { Component, OnInit } from '#angular/core';
import { HttpClient } from "#angular/common/http";
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import {FormBuilder, FormControl, FormGroup} from "#angular/forms";
import {TestService} from "./test.service";
class Customer {
id : number;
name: string;
email: string;
tel: string;
}
#Component({
selector: 'sample-template',
templateUrl: './test.component.html'})
export class TestComponent implements OnInit {
testForm: FormGroup;
testForm2: FormGroup;
public test: any;
name: string = '';
email: string = '';
tel: string = '';
public id: any;
constructor(private httpClient:HttpClient, private fb: FormBuilder, private TestService: TestService) {}
loadTasks(): void{
this.TestService.getAllTests().subscribe(response => {this.test = response;
console.log(this.test)})
}
ngOnInit() {
let trainData = [];
this.loadTasks();
this.testForm = this.fb.group({
testCd: 'Select'
});
this.testForm2 = this.fb.group({
id: this.id,
name: this.name,
email: this.email,
tel: this.tel
})
}
changeDropdown(formControl: FormControl, option: string): void {
formControl.patchValue(option);
console.log(option);
}
submitForm(){
let last:any = this.test[this.test.length-1];
this.id = last.id+1;
console.log(this.id);
this.testForm2.value.id = this.id;
console.log(this.testForm2.value);
this.TestService.postTests(this.testForm2.value);
}
}
And my html page which includes the following:
<label class="modelo-label">{{test?.testingTitle}}</label>
<form [formGroup]="testForm">
<div class="dropdown modelo-dropdown">
<label for="testCd" class="modelo-label">Testing</label>
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown" role="button" id="testCd" aria-haspopup="true" aria-expanded="true">{{testForm.get('testCd').value}}</button>
<div class="dropdown-menu modelo-dropdown-menu" aria-labelledby="testCd">
<a class="dropdown-item" *ngFor="let tests of test?.trainData; let i = index" id="tests.name" (click)="changeDropdown(testForm.get('testCd'), tests.name)">{{tests.name}}</a>
</div>
</div>
<form [formGroup]="testForm2" (ngSubmit)="submitForm()">
<div class="row">
<div class="col-12 col-sm-4 group">
<input type="text" id="name" formControlName="name" class="modelo-text-input"
[ngClass]="{'ng-not-empty' : testForm2.get('name').value.length !== 0}">
<label for="name">Name</label>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-4 group">
<input type="text" id="email" formControlName="email" class="modelo-text-input"
[ngClass]="{'ng-not-empty' : testForm2.get('email').value.length !== 0}">
<label for="email">Email</label>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-4 group">
<input type="text" id="tel" formControlName="tel" class="modelo-text-input"
[ngClass]="{'ng-not-empty' : testForm2.get('tel').value.length !== 0}">
<label for="tel">Telephone #</label>
</div>
</div>
<div class="col-1 group generateButton">
<button class="btn btn-primary" type="submit">Submit Info</button>
</div>
</form>
My Question is, I'm have everything set up for a post and what I'm trying to do is post testForm2.value to the json but under "trainData":[{}] that's within the JSON. I'm able to do so if I just drop all other objects inside the json and have just the array after "customers":... What exactly am I missing? I'm actually confusing myself right now and I may be overthinking this by alot. The post I have currently in this code works if I have just the array after "customers":.... so instead of me passing object which is the testForm2.value what else do I need to do? I hope this makes sense.
You have some strange things in your code. First :
In you API
return this.http.get<Test[]>("http://localhost:3000/customers", {params}).pipe(map(res => res));
I think what you want to do here is : (the pipe is useless you dont use it and it's not an array)
return this.http.get<Test>("http://localhost:3000/customers",{params});
In your component you want to push the update trainData list
submitForm(){
const lastTrainData = this.test.trainData[this.test.trainData.length-1];
const newTrainData = this.testForm2.value;
newTrainData.id = lastTrainData.id + 1;
this.test.trainData.push(newTrainData);
this.TestService.postTests(this.test);
}

Json data to be represented in formcontrol in angular 5

I am trying to bind the Json value which I get from my server to respective textboxes,but when I bind I see the value in textbox as [object object]
<h1>{{title}}</h1>
<h3>Catalog</h3>
<hr />
<fieldset>
<legend>About Tool</legend>
<form [formGroup]="CatalogForm" #formDir="ngForm" novalidate>
<div class="form-group row">
<!-- <div *ngIf="ItemName != null"></div>-->
<!--<div *ngFor="let c of CatalogForm;let i=index"></div>-->
<label class="control-label col-md-12">ItemName</label>
<div class="col-md-4"></div>
<input class="form-control" readonly="true" type="text" formControlName="Category" >
<pre>{{CatalogForm.value.Category | json }}</pre>
</div>
<br /><br/>
</form>
</form>
and my component code is
import { Component, OnInit } from '#angular/core';
import { Http, Headers } from '#angular/http';
import { FormsModule,NgForm, FormBuilder, FormGroup, Validators, FormControl } from '#angular/forms';
import { Router, ActivatedRoute } from '#angular/router';
import { CatalogComponent } from '../catalog/catalog.component';
import { CatalogService } from '../services/Catalog.service';
import { ContactService } from '../services/Contact.service';
import { URLService } from '../services/URL.service';
import { SupportService } from '../services/Support.service';
import { Catalog } from '../classes/Catalog';
import { Contact } from '../classes/Contact';
import { URL } from '../classes/URL';
import { Support } from '../classes/Support';
import { Category } from '../classes/Category';
#Component({
selector: 'app-catalog-form',
templateUrl: './catalog-form.component.html',
styleUrls: ['./catalog-form.component.scss']
})
export class CatalogFormComponent implements OnInit {
Catalogdata:Catalog;
Contacts:Contact[];
URLs:URL[];
Supportdata:Support[];
CatalogForm:FormGroup;
title: string = "";
id: number;
errorMessage: any;
constructor( private _fb: FormBuilder,private _avRoute: ActivatedRoute,
private catService: CatalogService,private conservice:ContactService,
private urlservice:URLService,private supservice:SupportService, private _router: Router) {
if (this._avRoute.snapshot.params["id"]) {
this.id = this._avRoute.snapshot.params["id"];
}
this.CatalogForm = this._fb.group({
Category: [''],
SubCategory:[''],
ItemName:[''],
Description:[''],
IAP_Number:[''],
/*ToolOwner:[''],
BusinessOwner:[''],
ProdURL:[''],
IncidentURL:[''],
RequestURL:[''],
SupportType:[''],
SupportValue:[''],
SupportLink:['']*/
})
}
ngOnInit() {
console.log("I am in form component",this.id);
if (this.id > 0) {
this.title = "View";
console.log("Title of form:",this.title) ;
this.catService.getCatalogDetails(this.id)
.subscribe( t =>
this.CatalogForm.patchValue
({Category:t,SubCategory:t,Description:t,ItemName:t,IAP_Number:t}) , err =>
console.log("Error messgae:",this.errorMessage)
);
}
console.log("Catalog and Category Details:",this.CatalogForm);
//.controls.Category.
//get(['cItem']));
}
cancel() {
this._router.navigate(['/home']);
}
//get ItemName() {return this.CatalogForm.get('Category').value; }
}
The <pre>{{CatalogForm.value.Category | json }}</pre>
gives me below data in JSON
[
{
"cats": {
"category_Id": 3,
"category_Name": "abc",
"sub_Category": "mmm"
},
"cItem": {
"catalog_Item_Id": 1,
"category_Id": 3,
"item_Name": "hsdd",
"description": "sadss",
"popularity_Flag": true,
"iaP_No": null,
"categoryID": {
"category_Id": 3,
"category_Name": "sds",
"sub_Category": "sad"
}
}
}
]
My problem is how to show the category_Name valuein a textbox.Here the json has category_name as abc and I want to bind that value to category_name textbox as its value. can you take a look?
Try:
<input
class="form-control"
readonly="true"
type="text"
formControlName="Category"
[ngModel]="CatalogForm.value.Category[0].cats.category_Name">
Use [(ngModel)] for 2 way binding

How to loop through nested JSON data

I am trying to loop through json data bellow, to so each element. I need to get down to the details data and then in side that loop through the f1,f2 into a div. I have tried using the index but that didn't work. Also I don't know how many f1,f2 there will be as it is returned from an api
JSON data
{
"data":[
{
"title": "test",
"image": "assets/imgs/test.png",
"date": "22/07/2018 - 19.00",
"location": "test",
"details": [
{
"f1":[
{
"FunctioName": "test",
"Time": "10:00:00"
}
],
"f2":[
{
"FunctioName": "test",
"Time": "11:00:00"
}
]
}
]
}
]
}
HTML
<div *ngFor="let item of card">
<div class="swiper-zoom-container">
<div class="out-card-box">
<h2>Date</h2>
<p>{{item.date}}</p>
<h2>Program</h2>
<div *ngFor="let details of item.details; let i = index">
</div>
</div>
</div>
</div>
TS
import { Component } from '#angular/core';
import { App } from 'ionic-angular';
import { DataService } from "../../services/data";
import { LoginPage } from "../login/login";
import { AngularFireAuth } from "angularfire2/auth";
import { Storage } from "#ionic/storage";
#Component({
selector: 'page-card',
templateUrl: 'card.html',
})
export class CardPage {
card:any;
constructor(private dataservice: DataService, private afAuth:AngularFireAuth, private app:App, private storage:Storage) {
this.dataservice.cardData().subscribe(
data => {
var jsonObj = JSON.parse(data["_body"]);
this.card = jsonObj.data;
console.log(jsonObj.data)
}
);
}
You can create an object which will hold the returned data from the api and you can just navigate the object values.
Example:
export class Class1 {
data: Class2[];
}
export class Class2 {
title: string;
image: string;
date: string;
location: string;
details: Class3[];
}
export class Class3 {
f1: Class4[];
f2: Class4[];
}
export class Class4 {
FunctioName: string;
Time: string
}
#Component({
selector: 'page-card',
templateUrl: 'card.html',
})
export class CardPage {
card:Class1;
constructor(private dataservice: DataService, private afAuth:AngularFireAuth, private app:App, private storage:Storage) {
this.dataservice.cardData().subscribe(
data => {
this.card = data;
}
);
}
then in your component template
<div *ngFor="let item of card.data">
<div class="swiper-zoom-container">
<div class="out-card-box">
<h2>Date</h2>
<p>{{item.date}}</p>
<h2>Program</h2>
<div *ngFor="let details of item.details; let i = index">
<!-- Print the remaining items here -->
</div>
</div>
</div>
</div>