I am getting JSON data and I am storing JSON data on my property.
this.ResultData_search=data.json().extras.ResultData
this.ResultData=[
{
"First_name": "xx",
"Email": "xxxx",
"Phone": "xxx",
"countryCode": "+91",
"order_datetime": "xxx",
"status": 11,
"DeviceType": 3,
"orderId": "59081a04c9ff6852a49dd32a",
"orderseqId": "E00002347",
"orderType": 1,
"CustomerID": "xx",
"pickAddress": "xx",
"dropAddress": "xx",
"pickLatitude": 17.4369414,
"dropLatitude": 17.43673,
"dropLongitude": 78.36710900000003,
"paymentType": 2,
"itemDescription": "",
"receiverName": "uday",
"receiverPhone": "xx",
"itemName": "sanjay",
"deliverycharge": "199",
"bookingType": 1
}
}]
I want to set all keys as table heading and data as table rows. I referred to the below linkshttps://stackoverflow.com/questions/40713580/angular2-table-with-dynamic-rows-and-columns
Link 2:Dynamically loading columns and data in to table in Angular 2
Any plunker will be very helpful to me.
The key solution is to convert the Object
ResultData
into an array of keys. Then you can easily iterate over it. You can get the keys of the object usingObject.keys(this.ResultData)
.component.ts
This link has an alternative way of using a pipe to convert a JSON object into an array.
Here is a working plunkr.
Regading the following part :
ResultData
is an array of items. For each item we will create a table row, and we will display the content of each item. That is why we repeat<tr>
for each itemres
in the arrayResultData
.Then, for each item
res
in theResultData
we will iterate over the array ofkeys
we prepared earlier in the ts code. For each itemkey
in thekeys
we will display the value of item as{{res[key]}}
.