0

EDIT

Hi, let me clear my below issue.. I have edited all the source code below, firstly I have two json file (one is from payment and other is from sale) but after checked with the API again.. I can use only one JSON with all the data showing.

Please check my revised JSON report and all the source code for your reff, my concern is when I put "pzrgb2l1lc8w7dp5" (from the payment object) in html.component, the table will be show "sale_id" & "status" (from the payment object) and "firstname", "lastname" & "email" (from the customer object with same "sale_id").

EDIT

previously apologize if this question already exists, but it seems not yet because it has been a week I looked for a solution to my problem and have not found the answer.

I have managed to get JSON data from the HTTP service using angular, I have two JSON Urls and want to be made into one report and succeed. But I want to use input and buttons to get the JSON report, I haven't found the tutorial.

{
  "success": 1,
  "object": "list",
  "total_count": 2,
  "data": [
    {
      "object": "sale",
      "id": "j9cncjq0",
      "status": "Completed",
      "customer": {
        "object": "customer",
        "id": "uj56cbj3943sq1sg",
        "email": "iron.man@email.com",
        "firstname": "Iron",
        "lastname": "Man"
      },
      "payments": {
        "object": "list",
        "total_count": 1,
        "data": [
          {
            "object": "payment",
            "id": "pzrgb2l1lc8w7dp5",
            "sale_id": "j9cncjq0",
            "status": "COMPLETED",
          }
        ]
      }
    },
    {
      "object": "sale",
      "id": "sl8hcw26",
      "status": "Completed",
      "customer": {
        "object": "customer",
        "id": "upwvs7xqbc6zhwxh",
        "email": "black.widows@email.com",
        "firstname": "Black",
        "lastname": "Widows"
      },
      "payments": {
        "object": "list",
        "total_count": 1,
        "data": [
          {
            "object": "payment",
            "id": "pjd79f1yygqrm43q",
            "sale_id": "sl8hcw26",
            "status": "COMPLETED",
          }
        ]
      }
    }
  ]
}

Below is the json.service.ts code

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class JsonService {

  api_key = '1237bb46b22ee';

  private _urlSale: string = 'https://source-website-api/sales?apiKey='+this.api_key;

  constructor(private http: HttpClient) { }

  getSale() {
    return this.http.get(this._urlSale)
  }

}

Below is json.component.ts code

import { Component, OnInit } from '@angular/core';
import { JsonService } from '../../service/json.service';

@Component({
  selector: 'app-json',
  templateUrl: './json.component.html',
  styleUrls: ['./json.component.css']
})
export class JsonComponent implements OnInit {

  saleJSON: object;

  constructor(private _http: JsonService) { }

  ngOnInit() {
    this._http.getSale().subscribe(data => {
      this.saleJSON = data;
      console.log(this.saleJSON);
    })
  }
}

Below is json.component.html code

<h1>JSON Receive Data</h1>

<p>Payment Number</p>

<input type="text"> <br><br>
<button type="submit">Check</button>
<p></p>

<table style="width:70%">
    <tr>
      <th>Sale ID</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
      <th>Status</th>
    </tr>
    <tr align="center">
      <td> payment.sale_id </td>
      <td> customer.firstname </td>
      <td> customer.lastname </td>
      <td> customer.email </td>
      <td> payment.status </td>
    </tr>
  </table> 

<p>{{errorMsg}}</p>

I hope there is someone who can help me, thank you.

  • Can you try to refine the language.. it's not very clear what you need. – dorado Aug 31 '19 at 09:56
  • do you mean the language of code or my english language brother? because english is not my primary language sorry to make you confuse.. – Indra Gunawan Aug 31 '19 at 10:00
  • When you say 'I want to use input and buttons to get the JSON report', what does it mean? You want to fetch the data from API when user clicks on the button? Actually your problem is not quite clear. Please explain it a little bit. – MuhammadMohsan Aug 31 '19 at 10:16
  • What is the error you are getting . PLease provide the error log – Pushprajsinh Chudasama Aug 31 '19 at 10:31
  • I am sorry all to make you confuse, yes I want to get the data when I put the payment code from input type with click button, with the above code I didn't get an error (https://ibb.co/wg4nJrG) this is my screen result. I have success to get the data for two different API url. – Indra Gunawan Aug 31 '19 at 11:19
  • I think I understand, as you want to be able to type a value into the input box and use it for a check when you click the button? If this is correct I have added an example below of how to do it. – chrismclarke Aug 31 '19 at 11:41

1 Answers1

0

If you are referring to making the value from the input accessible to the button click function, there are a couple ways to do this. An easy option would be to create a variable for the input and bind to the html input element. This value can then be accessed from your click button function. E.g.

  export class JsonService {
    inputValue: string
  ...

  check(){
    console.log('running check',this.inputValue)
  }
<input type="text" [(ngModel)]="inputValue"> <br><br>
<button type="submit" (click)="check()">Check</button>

Alternatively you can cut out a little bit of code by giving the input html a local reference and refer to it in the button click function

  check(value){
    console.log('running check',value)
  }
<input type="text" #myInput> <br><br>
<button type="submit" (click)="check(myInput.value)">Check</button>
chrismclarke
  • 1,995
  • 10
  • 16
  • Hi Chris, thank you for your advice but it's not solve my issue, I have edited the question for better understanding, thanks – Indra Gunawan Sep 02 '19 at 13:32
  • Ok, yes your question is still confusing. So to make things clearer: 1. Will you be making one or two requests to your api for data? 2. Will they be made at the same time or does one follow the other? 3. If there are multiple requests can you please add a sample of data received from both (exactly how it looks from the api) – chrismclarke Sep 02 '19 at 13:51
  • In these types of situations with complicated code and many things going on it's most useful to create a working demo with the minimum information required. I have done this in the following stackblitz https://stackblitz.com/edit/angular-wve1xd, however do not know how to proceed without further understanding of the problem – chrismclarke Sep 02 '19 at 13:52
  • Hi Chris, sorry if you still confused :).. I was create a working demo in [link](https://stackblitz.com/edit/angular-dhra6k) and I can show the json in html, but I still need to do validate with input, so when I put the paymentID in input text, if the record found will be show "status complete". – Indra Gunawan Sep 04 '19 at 21:23
  • 1
    I am able to call my service, and retrieve the data from the JSON, but i am stuck on how to perform a search on my retrieved data against the value entered. – Indra Gunawan Sep 04 '19 at 22:12
  • Oh ok, much clearer now thanks, your problem is filtering/merging the data. This is usually a lot easier to do on the database (particularly if SQL), but still reasonable enough to do in javascript. Have updated the link: https://stackblitz.com/edit/angular-dhra6k – chrismclarke Sep 05 '19 at 11:27