Ionic - Dynamic tab creation from JSON response - json

I am very new to Ionic and Angular. Basically, I need to create tabs dynamically based on JSON response from my own server.
My JSON response
{
"category_id": 1,
"parent_id": 0,
"name": "Root Catalog",
"position": 0,
"level": 0,
"children": [
{
"category_id": 2,
"parent_id": 1,
"name": "Default Category",
"is_active": 1,
"position": 1,
"level": 1,
"children": [
{
"category_id": 5,
"parent_id": 2,
"name": "Masala Mix",
"is_active": 1,
"position": 1,
"level": 2,
"children": []
},
{
"category_id": 3,
"parent_id": 2,
"name": "Pure spices",
"is_active": 1,
"position": 2,
"level": 2,
"children": []
},
{
"category_id": 6,
"parent_id": 2,
"name": "Pickles",
"is_active": 1,
"position": 3,
"level": 2,
"children": []
},
]}]}
My output should be with the creation of 3 tabs with a name as "Masala Mix", "Pure Spices" and "Pickles". But what I get now is only one tab creation with the name "Default category"
<ion-tabs >
<ion-tab [root]="tab3Root" tabTitle="Contact" tabIcon="contacts"></ion-tab>
<ion-tab *ngFor="let rew of people" [root]="rew.root" [tabTitle]="rew.name">
</ion-tab>
<ion-tab *ngFor="let cat of rew?.children" [tabTitle]="cat.name"></ion-tab>
</ion-tabs>
TypeScript File
loadPeople() {
this.peopleSearch.load()
.then(data1 => {
this.people = data1.children;
console.log("Tabs : "+this.people);
}
);
}
Browser console output : Tabs : [object Object]
HTTP provider
export class PeopleSearch {
data1: any;
constructor(public http: Http) {
console.log('Hello PeopleSearch Provider');
}
load() {
if (this.data1) {
return Promise.resolve(this.data1);
}
// Dont have the data yet
return new Promise(resolve => {
this.http.get('http://happyos.in/cat.php')
.map(res => res.json())
.subscribe(data => {
this.data1 = data;
resolve(this.data1);
});
});
}
}
Output

Related

Angular 7 - How to display array inside of an object in HTML

I am trying to display array inside of an object in HTML. My sample JSON is like below...
I would like to display Product > ProductCategoryRelations > Category.Name in a string comma separeted like "Category 1, Category 2"
[
{
"Id": 2,
"Name": "Product 1",
"ProductCategoryRelations": [
{
"Id": 3,
"ProductId": 2,
"CategoryId": 2,
"Active": true,
"Category": {
"Id": 2,
"ParentId": 1,
"Name": "Category 1"
}
},
{
"Id": 4,
"ProductId": 2,
"CategoryId": 2,
"Active": true,
"Category": {
"Id": 2,
"ParentId": 1,
"Name": "Category 2"
}
}
],
How can I put all categories name in a string comma seperated?
What I have so fas is like below but it doesnt worrk
<dd class="col-sm-9" *ngFor="let category of product.ProductCategoryRelations">
<span>{{category.Name}}</span>
</dd>
In javascript, you would need this:
product.ProductCategoryRelations
.map(r => r.Category.Name)
.join(',')
to put it in context:
let product = {
"Id": 2,
"Name": "Product 1",
"ProductCategoryRelations": [
{
"Id": 3,
"ProductId": 2,
"CategoryId": 2,
"Active": true,
"Category": {
"Id": 2,
"ParentId": 1,
"Name": "Category 1"
}
},
{
"Id": 4,
"ProductId": 2,
"CategoryId": 2,
"Active": true,
"Category": {
"Id": 2,
"ParentId": 1,
"Name": "Category 2"
}
}
],
};
console.log(
product.ProductCategoryRelations
.map(r => r.Category.Name)
.join(',')
);
Now you can use .join(',') in Angular template syntax, but you cannot use the .map() bit. So I suspect the easiest way would be to add a utility function to your component that does this for you:
getCategoryNames(product) {
return product.ProductCategoryRelations.map(r => r.Category.Name);
}
and then do it in the template like this:
{{getCategoryNames(product).join(',')}}
If you need to do the same thing in multiple places in your app across multiple components, then I would recommend writing your own custom pipe.

Getting error while parsing json response from a dynamic {System.RuntimeType} variable

I'm working on some code in which uses dynamic variables jsonResponse .
dynamic jsonResponse = JsonConvert.DeserializeObject(response);
This variable contains collection of hotel list in json format. From this collection I am getting roomlist collection in a new variable roomResponseList :
var roomResponseList = jsonResponse["hotels"]["hotels"][rooms].roomResponseList;
I am getting first room detail into **JObject responseRateKeys **:
foreach (var roomByResponse in roomResponseList)
{
JObject responseRateKeys = JObject.Parse(roomByResponse.ToString());
var boardNameListByResponse = responseRateKeys.AsJEnumerable().AsEnumerable()
.Select(t => t["rates"]["boardName"].ToString().Trim())
.Distinct()
.ToList();
}
But when I am trying to get any item list from JObject by using linq lambda, I am getting error,
"Cannot access child value on Newtonsoft.Json.Linq.JProperty."
Value of roomByResponse=
{ "code": "DBL.KG-NM", "name": "DOUBLE KING BED NON SMOKING", "rates": [ { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "186.04", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "149.63", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "93.02" }, { "offset": 2, "dailyNet": "93.02" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|1~1~0||N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "238.92", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 1, "adults": 1, "children": 0, "dailyRates": [ { "offset": 1, "dailyNet": "119.46" }, { "offset": 2, "dailyNet": "119.46" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|RO|IWH25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NRF", "rateType": "RECHECK", "net": "372.06", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "RO", "boardName": "ROOM ONLY", "cancellationPolicies": [ { "amount": "299.25", "from": "2017-07-14T03:29:00+05:30" } ], "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "186.03" }, { "offset": 2, "dailyNet": "186.03" } ] }, { "rateKey": "20171217|20171219|W|256|237403|DBL.KG-NM|ID_B2B_26|BB|IWB25|2~2~1|2|N#AFF5C93E36054661ADCBC14A78A532AE1007", "rateClass": "NOR", "rateType": "RECHECK", "net": "477.84", "allotment": 99, "paymentType": "AT_WEB", "packaging": false, "boardCode": "BB", "boardName": "BED AND BREAKFAST", "rooms": 2, "adults": 2, "children": 1, "childrenAges": "2", "dailyRates": [ { "offset": 1, "dailyNet": "238.92" }, { "offset": 2, "dailyNet": "238.92" } ] } ] }
Thank you
Pravesh Singh
change linq to
responseRateKeys["rates"].AsJEnumerable().Select(t=>t["boardName"]).Distinct().ToList()

Export / Import ECMASCRIPT 6 Class with JSON

This is all using ecmascript 6; Say I have a class
class Hero {
constructor(id,name){
this.id=id;
this.name=name;
}
}
And I want to have create an array of object of that type of class:
var HEROES = [
{ "id": 11, "name": "Mr. Nice" },
{ "id": 12, "name": "Narco" },
{ "id": 13, "name": "Bombasto" },
{ "id": 14, "name": "Celeritas" },
{ "id": 15, "name": "Magneta" },
{ "id": 16, "name": "RubberMan" },
{ "id": 17, "name": "Dynama" },
{ "id": 18, "name": "Dr IQ" },
{ "id": 19, "name": "Magma" },
{ "id": 20, "name": "Tornado" }
];
Is there a way to do this besides manually parsing the array and then doing New Hero() for each element? It would be nice to be able to do something like HEROES[] = hero.jsonExport() and then later var = hero.jsonImport(HEROES[i]). I've been looking around but without using Typescript this doesn't seem particularly easy. Am I missing something easy?
Thanks!
-Eric
Seems strange to want to import from a specific array instance - especially given that the order could change. It would probably be better to export a factory function which creates a new Hero based off the criteria:
class Hero {
constructor(id,name){
this.id=id;
this.name=name;
}
}
const HEROES = [
{ "id": 11, "name": "Mr. Nice" },
{ "id": 12, "name": "Narco" },
{ "id": 13, "name": "Bombasto" },
{ "id": 14, "name": "Celeritas" },
{ "id": 15, "name": "Magneta" },
{ "id": 16, "name": "RubberMan" },
{ "id": 17, "name": "Dynama" },
{ "id": 18, "name": "Dr IQ" },
{ "id": 19, "name": "Magma" },
{ "id": 20, "name": "Tornado" }
];
export default function createHero(index) {
const hero = HEROES[index];
return new Hero(hero.id,hero.name);
}
And the caller:
import createHero from "./hero";
const hero1 = createHero(1);

passing data value to ui-grid

I want to pass the data value ui-grid.I need to pass $scope.ll value to ui-grid.If I copy the data and assigned $scope.ll=[{}] Id and statesum_totalcount are working well. I need to pass $scope.ll to grid
My data in array are as
{
"ID": "3",
"stat_sum": {
"totcount": 3
},
"zip_stats": [
{
"zip": "560045",
"count": 1
},
{
"zip": "567657",
"count": 2
}
],
"qual_stats": [
{
"count": 1,
"qualification": "B.E."
},
{
"count": 2,
"qualification": "BE"
}
],
"prof_stats": [
{
"count": 1,
"profession": "Doctor"
},
{
"count": 2,
"profession": "Software Engineer"
}
],
"city_stats": [
{
"city": null,
"count": 2
},
{
"city": "Bangalore",
"count": 1
}
],
"state_stats": [
{
"count": 1,
"state": "Karnataka"
},
{
"count": 2,
"state": "Kerala"
}
],
"stats_info": [
{
"acount": 3,
"answer": "fgdfgd",
"question": "comment-about-me-"
},
{
"acount": 1,
"answer": "one",
"question": "radio-answer-"
},
{
"acount": 2,
"answer": "two",
"question": "radio-answer-"
},
{
"acount": 3,
"answer": "t-shirt",
"question": "select-any-dress-for-me-[]"
},
{
"acount": 3,
"answer": "no",
"question": "say-yes-or-no-"
},
{
"acount": 3,
"answer": "2015-09-25",
"question": "select-your-b.date-"
},
{
"acount": 3,
"answer": "24",
"question": "select-your-age-"
},
{
"acount": 3,
"answer": "2",
"question": "type-number-"
},
{
"acount": 3,
"answer": "false",
"question": "select-true-or-false-"
}
]
}
In controller
$timeout(function () {
console.log($scope.ll); //works fine
$rootScope.showspinner = false;
$scope.gridOptionsComplex = {
enableFiltering: true,
showGridFooter: true,
showColumnFooter: true,
columnDefs: [
{name: 'ID', width: 100, enableCellEdit: false,},
{name: 'stat_sum.totcount', width: 100, enableCellEdit: false,},
{name: 'zip_stats.zip', width: 100, enableCellEdit: false,},
{name: 'zip_stats.count', width: 100, enableCellEdit: false,},
{name: 'qual_stats.qualification', width: 100, enableCellEdit: false,},
{name: 'qual_stats.count', width: 100, enableCellEdit: false,},
],
data:$scope.ll
};
$scope.$apply(function () {
$scope.aut = true;
});
}, 500, false);
The same kind of problem I faced ,I am giving an example code how to solve this
{
"result": {
"fileNames": [
"Book1 (2).csv",
"address_sample (3).csv",
"Book1.csv"
],
"ids": [
1,
2,
3
]
},
"responseSuccess": "success",
"responseError": null,
"responseInfo": null,
"responseWarning": null,
"responseCode": 0
}
I have output like above format and also I have to Integrate with these in to ui-grid .
example controller side code once the control reached success part I am getting the output as the format of above , so lets see how to integrate with ui-grid,
In my controller I have $scope.gridsOptions like
$scope.gridsOptions = {
columnDefs : [
{
field : 'field',
name : 'Id'
},
{
field : 'filename',
name : 'FileName'
}]
}
I assumed the control came to the success part .success(function()) what ever validate could you please validate then,
.success(function(data){
$scope.resultValues =[];
// Here My PD(Problem Domain says iterate based On `ids`)
for (var i = 0; i < data.result.ids.length; i++) {
// Im getting each ID in $scope.fileId variable
$scope.fileId = data.result.ids[i];
// Here Im getting each fieldNames in $scope.fileName variable
$scope.fileName = data.result.fileNames[i];
//Then I am pushing those values in to fieldId
$scope.resultValues.push({
field : $scope.fileId,
filename : $scope.fileName
});
}
ListData.fileIdValues = angular.copy($scope.resultValues);
// I am pointed those to $scope.gridsOptions.data
$scope.gridsOptions.data = ListData.fileIdValues;
})
Please Note ListData.fileIdValues in my code it is angular service variable this.fileIdValues ={}. For my Problem Domain I need these values so I stored it in serviceand use it where ever I want.

reformating cakephp find threaded

My goal is to use a tree object to store invoices. The issue I'm running into is that it always puts a "Invoice" model wrapper around each subsequent child. I tried just getting the results and using the Set class to clean up the redundant "Invoice", "children" but I couldn't figure out how to get it to work. I'm using this in an API so basically what I want to be able to do is move up and down the array without having to constantly do invoice.children.invoice.children
In my controller I have:
$results = $this->Invoice->find('threaded', array(
'order' => array('lft ASC') // or array('id ASC')
));
$this->set(array(
'results' => $results,
'_serialize' => 'results'
));
What I'm getting is:
{
"success": true,
"data": [
{
"id": 1,
"parent_id": null,
"lft": 1,
"rght": 30,
"name": "Invoices",
"children": [
{
"Invoice": {
"id": 2,
"parent_id": 1,
"lft": 2,
"rght": 15,
"text": "invoice 001"
},
"children": [
{
"Invoice": {
"id": 3,
"parent_id": 2,
"lft": 3,
"rght": 8,
"text": "invoice_item"
},
"children": [
{
"Invoice": {
"id": 4,
"parent_id": 3,
"lft": 4,
"rght": 5,
"text": "invoice_sub_item"
},
"children": []
},
{
"Invoice": {
"id": 5,
"parent_id": 3,
"lft": 6,
"rght": 7,
"text": "subitem#2"
},
"children": []
}
]
}
]
}
]
}
]
}
Ideally I'd like to have
invoices.invoice.text = "invoice 001"
and
invoices.invoice.invoice_item[0].text = "invoice_item"
Obviously I'll need to add/take away from the array and resubmit with changes. Is this possible or am I just making things harder on myself?