Angular 7 - How to display array inside of an object in HTML - 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.

Related

How can I join models in ExtJS?

In ExtJS 6.02, I would like to join 2 or more models into a single store.
1 to 1 relationship:
Given users that can only be associated with a single user detail row in DB:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"usersDetails": [
{
"id": 1,
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"email": "amy#a.com",
"sex": "m"
}
]
I would like to have this on the store:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"name": "Hubert Farnsworth",
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"name": "Turanga Leela",
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"name": "Amy Wong",
"email": "amy#a.com",
"sex": "m"
}
]
1 to many relationship:
Given users that can have multiple posts in BD:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"posts": [
{
"id": 1,
"user": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"user": 1,
"title": "Post 2 title",
"body": "Post 2 body"
},
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
},
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
I would like to have a store containing:
"items": [
{
"id": 1,
"name": "Philip J. Fry",
"posts": [
{
"id": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"title": "Post 2 title",
"body": "Post 2 body"
}
]
},
{
"id": 2,
"name": "Hubert Farnsworth",
"posts": [
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
}
]
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3,
"posts": [
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
},
{
"id": 4,
"name": "Amy Wong",
"posts": []
}
]
Many to 1 relationship:
This is actually my actual issue, I have a DB table with multiple items that each can only match a single item in another table.
Should be same as 1 to many.
Many to many relationship:
Basicly same as 1 to many I guess?
I have seem this solution: https://fiddle.sencha.com/#fiddle/1hs9&view/editor it doesn't cover everything and maybe there's a better option.
According to Sencha support:
The concept of joining records doesn't really exist within ExtJS but
you can, by making use of renderers, access and print record
association data.
I have a simple fiddle of your example above showing you how you may
achieve this. Please note that dynamic loading of association (which
you have in your example) makes using renderers a bit tricky, but not
impossible, as the loading of the association info is asynchronous in
nature and the renderer cannot wait for the response. The work around
is to refresh the entire grid when all association stores have
finished loading.
https://fiddle.sencha.com/#fiddle/3d1v&view/editor

OData filter on array property to return empty array or array

I have a JSON object similar to this one:
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
},
{
"id": 2,
"name": "jane",
"orders": [{"id": 2, "customerId": 2, "itemId": 7}]
}
]
}
I'd like to query it like....
...odata/customers?$expand=orders&$filter=orders/any(order: order/itemId eq 6)
...and get...
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
},
{
"id": 2,
"name": "jane",
"orders": []
}
]
}
but what I get is...
{
"customers": [
{
"id": 1,
"name": "bob",
"orders": [{"id": 1, "customerId": 1, "itemId": 6}]
}
]
}
I want to get all the customers and I only want the filter to apply to the "orders".
What can I do to get this result?
Just got the same problem!
Solved by filtering my expand by the same condition in any().
applications?$expand=accounts($filter=idUser eq 5)&$filter=accounts/any(account: account/idUser eq 5)
Maybe to late but hope it will help someone else ;)

Laravel pagination json format

I am using laravel database pagination for an api. It gives the output as follows :
{
"current_page": 1,
"data": [
{
"id": 1,
"name": "Default"
},
{
"id": 2,
"name": "Default without Sidebar"
}
],
"first_page_url": "http://example.com/api/layouts?a=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://example.com/api/layouts?a=1",
"next_page_url": null,
"path": "http://example.com/api/layouts",
"per_page": 10,
"prev_page_url": null,
"to": 2,
"total": 2
}
I don't want or need first_page_url, last_page_url, etc, so it should just be:
{
"current_page": 1,
"data": [
{
"id": 1,
"name": "Default"
},
{
"id": 2,
"name": "Default without Sidebar"
}
],
"from": 1,
"last_page": 1,
"to": 2,
"total": 2
}
So, Is there a way, to format the output json so that i can remove or hide some extra info which i don't need .
You can do something like this:
$except = ['first_page_url', 'last_page_url'];
return array_except(<YourModel>::paginate(...)->toArray(), $except);

Export mysql data to JSON in a nested and recursivelty way starting from a table?

I have seen several examples to export MySQL tables to JSON, however such examples export data in an aggregated way. For example, take a database where you have two tables "Invoice head" and "Invoice details". "Invoice details" is a child of "Invoice head".The data in JSON is usually represented like:
{
"invoice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12"
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
}
],
"invoice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
},
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
],
}
I need to export data in a nested way:
{
"invice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12",
"invice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
}
]
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
"invice_details": [
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
]
}
]
}
For this I guess one needs to start in a table and recursively go through its childs for each record.
Does anybody knows if there is anything that does it? I don't want to reinvent the wheel.

Angular app reading from a nested json structure

I have an angular app which reads from a json that looks like the following:
[{
"category": {
"id": 40,
"name": "Category 1",
"parent": {
"id": 38,
"name": "Parent Category 1",
"parent": {
"id": 23,
"name": "Parent name",
"level": 1
},
"level": 2
},
"level": 3
},
"subcategory": {
"course_name": "Category Name 1",
"title": "Category Title",
}
},
{
"category": {
"id": 40,
"name": "Category 1",
"parent": {
"id": 38,
"name": "Parent Category 1",
"parent": {
"id": 23,
"name": "Parent name",
"level": 1
},
"level": 2
},
"level": 3
},
"subcategory": {
"course_name": "Category Name 2",
"title": "Category Title",
}
},
{
"category": {
"id": 40,
"name": "Category 1",
"parent": {
"id": 39,
"name": "Parent Category 2",
"parent": {
"id": 23,
"name": "Parent name",
"level": 1
},
"level": 2
},
"level": 3
},
"subcategory": {
"course_name": "Category Name 3",
"title": "Category Title",
}
}
]
I am trying to read this json and write it in the app such that the structure in the page looks like the following
Parent name
Parent Category 1
Category 1
Category Name 1, Category Title
Category name 2, Category Title
Parent Category 2
Category 1
Category Name 3, Category Title
How can i get this structure in angular? Here is the plunker of what i tried.
You need to summarize the data first. Then, you can repeat over the summaries to render parent level lists. Nested repeats can then conditionally display the children data.
The summary arrays (generated with a simple loop, or maybe a fancy library like lodash) might look like this:
$scope.parents = [{
"id": 23,
"name": "Parent name",
"level": 1
}]
$scope.parentCategories = [{
"id": 38,
"name": "Parent Category 1"
}, {
"id": 39,
"name": "Parent Category 2"
}]
$scope.categories = [{
"id": 40,
"name": "Category 1"
}]
Then you could use them like this:
<div ng-repeat="parent in parents">
<ul>
<li>{{parent.name}}
<ul ng-repeat="parentcategory in parentCategories">
<li>{{parentcategory.name}}
<ul ng-repeat="category in categories">
<li>{{category.name}}
<ul ng-repeat="program in programs"
ng-show="program.category.id == category.id">
<li ng-show="program.category.parent.id == parentcategory.id">
{{program.subcategory.course_name}} {{program.subcategory.title}}
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
If you want to try the code, here it is on Plunkr.