I am developing an application with angular and I need to display some data that has the following structure:
{
"id": 33,
"arg": 7,
"date": "2022-01-31",
"User": {
"Name": "Cristian",
"Group": {
"NGroup": "Group 1"
}
},
"Sport": {
"NSport": "Sport 1"
}
},
The information should be displayed in this way
id | arg | date | Name (User) | NGroup (Group) | NSport (Sport)
For this I was trying to use keyvalue, the problem with this is that when it brings the User object, it shows me an "item" called [object Object] that corresponds to group and there is no way to hide it. This is the current code:
<tbody>
<tr *ngFor="let item of listItems">
<td><div>{{ item.id }}</div></td>
<td><div>{{ item.arg}}</div></td>
<td><div>{{ item.date }}</div></td>
<td *ngFor="let key of item.User | keyvalue "><div>{{ key.value }}</div></td>
<td *ngFor="let key of item.User | keyvalue"><div *ngFor="let key2 of key.value | keyvalue" >{{ key2.value }}</div></td>
<td *ngFor="let kv of item.Sports | keyvalue"><div>{{kv.value}}</div></td>
</tr>
</tbody>
The output of this is:
id | arg | date | [Object,Object] | Name(User) | NGroup (Group) | NSport(sport)
The question is : How could I show only some items of the object and not all the content
Thanks!!
You can filter out the items to display using something like this:
<table>
<tr *ngFor="let item of listItems">
<td><div>{{ item.id }}</div></td>
<td><div>{{ item.arg}}</div></td>
<td><div>{{ item.date }}</div></td>
<ng-container *ngFor="let key of item.User | keyvalue ">
<td *ngIf="isInteresting(key.value)">{{ key.value }}</td>
</ng-container>
<td *ngFor="let key of item.User | keyvalue"><div *ngFor="let key2 of
key.value | keyvalue" >{{ key2.value }}</div></td>
<td *ngFor="let kv of item.Sport | keyvalue"><div>{{kv.value}}</div></td>
</tr>
</table>
and in the component:
isInteresting(val): boolean { return typeof val !== 'object'; }
This may be dodging the question, but typically I try to keep component template files simple. It helps keep business logic contained inside your TS file which also enables for better testing scenarios.
Since you know the desired output id | arg | date | Name (User) | NGroup (Group) | NSport (Sport) you should use the component's TS file to create that interface for the view. A simple .map() should do the job.
this.viewItems = this.listItems.map(item=>{
const { id, arg, date, User, Sport } = item;
const name = User.Name;
const nGroup = User.Group.NGroup;
const nSport = Sport.NSport;
return { id, arg, date, name, nGroup, nSport };
});
Now you only need one *ngFor loop for rendering your data.
<tbody>
<tr *ngFor="let item of viewItems">
<td><div>{{ item.id }}</div></td>
<td><div>{{ item.arg}}</div></td>
<td><div>{{ item.date }}</div></td>
<td><div>{{ key.name }}</div></td>
<td><div>{{ key.nGroup }}</div></td>
<td><div>{{ key.nSport }}</div></td>
</tr>
</tbody>
Note: While it's already defined in your source data, I would avoid naming objects with uppercase letters (User and Sport). This naming style should be reserved for defining classes.
Related
How can I get and display each element from the product json data type in laravel.
"category" => accessories
"product" => "["eyewear", "watches", "shoes"]"
Data has been saved correctly using
$casts = [
'product' => 'array'
];
When I try to display, I get the list like show down
Category :
accessories | Product : ["eyewear", "watches", "shoes"]
Is it possible to get result like
Category : accessories | Product: eyewear *****
Category : accessories | Product: watches *****
Category : accessories | Product: shoes *****
Thanks for your help
Provided the JSON has indeed been successfully converted to an array (in this case to a 1D array), you can get the value for each index like you normally would with any other array:
{{ $product[0] }}
If you want to create some sort of list out of it, you can just loop through it using foreach:
#foreach($product as $p)
{{ "Category: " . $category }}
{{ "Product: " . $p }}
#endforeach
If I understand correctly you already have some query results stored in $items, where you have stored values for category_id and product (based on your original post I assume you've already converted the JSON to an array using the $casts property inside your model, alternatively you could use PHP function json_decode()). If you want to loop through $items, it would probably look like this:
#foreach($items as $item)
#foreach($item['product'] as $product)
{{ "Category: ".$item['category_id']." | Product: ".$product }}
</br>
#endforeach
#endforeach
The code above should output something like this:
Category: 1 | Product: eyewear
Category: 1 | Product: watches
Category: 1 | Product: shoes
Category: 2 | Product: a
Category: 2 | Product: b
Category: 2 | Product: c
In a table I want to display the following items in a single Row. Can someone help me out loop the JSON so I can rearrange the items as shown below.
StudentId | CourseTitle | TextbookName
23 | Biology | Biology Today
JSON
{
"studentId": 23,
"course": [{
"courseTitle": "Biology",
"textBook": 1,
"TextBooks": {
"textbookId": 1,
"textbookName": "Biology Today"
}
}, ... ]
}
CODE
<table class="table">
<tbody>
<tr *ngFor="let student of this.listOfStudents">
<td >{{student ?.studentId}}</td>
</tr>
</tbody>
</table>
If I got it right, try following:
<table class="table">
<tbody>
<ng-container *ngFor="let student of this.listOfStudents">
<tr *ngFor="let course of student.course">
<td>{{ student.studentId }} | {{ course.courseTitle }} | {{ course.TextBooks.textbookName }}</td>
</tr>
</ng-container>
</tbody>
</table>
simple question: How do I show a list of records that have todays date?
my HTML:
<table class='table hovertable'>
<tbody class="mytable">
<tr *ngFor="let timeRecord of timeRecords">
<th>
{{timeRecord.date | date:'dd.\u00A0'}}{{timeRecord.date | date:'MMMM' | convertToSlovakMonthsPipe}}
</th>
<td>
{{timeRecord.duration}} h
</td>
<td>
{{timeRecord.allocation}}
</td>
<td>
{{timeRecord.comment}}
</td>
</tr>
</tbody>
</table>
and my typescript that gets all records:
getTimeRecords() {
this.timeRecordService.getTimeRecords()
.subscribe(
(time: ITimeRecord[]) => {
this.timeRecords = time;
}
)
}
this.timeRecords = time.filter(t => t.getTime() == Date.now().getTime() );
I have a json field in my database name is permissions {"read": true, "create": true}. Now, I want to show this in my blade view. I had try this way but it's show an error.
#foreach($users as $user)
<tr>
<td>{{$user->roles->permissions}}</td>
</tr>
#endforeach
show this Error Message.
Property [permissions] does not exist on this collection instance.
User Model
public function roles()
{
return $this->belongsToMany(Role::class,'user_roles');
}
I tested this scenario and was OK . i hope usefull for you :
my route :
Route::get('/test', function (Request $request) {
$users=\App\User::with('roles')->get();
return view('welcome',compact('users'));
});
define roles method inside user model :
public function roles()
{
return $this->belongsToMany(Role::class,'user_roles');;
}
and get permissions in view :
#foreach($users as $user)
#foreach($user->roles as $role)
<tr>
<td>{{$role}}</td>
</tr>
#endforea
#endforeach
I test with these tables
user_roles
user_id | role_id
-----------------
1 | 2
1 | 2
roles:
id | permissions
-----------------
1 | {"read": true, "create": true}
2 | {"read": true, "create": false}
users:
id | name | email | password | remember_token | created_at|updated_at
----------------------------------------------------------------------
1 | alihossein|ali#gmail.com|XXXXX|UIWKK67ei5CCuiv1OXilKY2aRkTfSqGLpqJch0F9YmenGSorsQGHVvWiX6kP| 2018-05-28 22:25:14 | 2018-05-28 22:25:14
I created the following tables below to represent the hierarchy of a specific job. Then I created some CRUD functions in java to "createJob", "getJob", "updateJob" and "deleteJob" in these tables.
Now I would like to implement the data I get back from these operations on a user interface. I'm looking for the simplest UI where I could probably have the job listed in a dropdown list and then based on my selection I can choose the job type then based on that selection choose the job specification. These CRUD functions can be performed on each dropdown. To add a new Job, rename it, etc.
What's the simplest way to do this? I've been looking around for some APIs because I believe this would be something that's common. There is probably a more descriptive term for these types of drop downs which would help my search.
EDIT After further research, what I am looking for is an "Editable Cascading drop down list".
EDIT 2 I was able to see some good examples using Angular to create the cascading drop down list. I was able to create the app.component.ts and app.component.html below. I can get the dropdowns to display but they are not cascading based on the Job selected. What am I doing wrong in my code? Also, soon as this is working, how can I prepare the code to make use of the java functions I created earlier rather than the hard coded values I have?
+---------+---------+
| Id | Job |
+---------+---------+
| 1 |Developer |
|
| 2 |QA Tester |
+---------+---------+---------+-----+
| Id | Job_Type | Job_Id
+---------+---------+---------+-----+
| 1 |Java Developer | 1 |
| 2 |JS Developer | 1 |
| 3 |FrontEnd Tester | 2 |
| 4 |Backend Tester | 2 |
| 5 |Php Developer | 1 |
+---------+---------+---------+----+-----------+
| Id | Job_Specification | Job_Type_Id
+---------+---------+---------+----+-----------+
| 1 |Spring Developer | 1 |
| 2 |Angular Developer | 2 |
| 3 |UI Tester | 3 |
| 4 |Mobile Tester | 3 |
| 5 |Hibernate Developer | 1 |
APP.COMPONENT.TS
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Roles!';
selectedRole= 0;
selectedProviderType = 0;
providerType = [];
classification = [];
onSelectRole(role_id: number) {
this.selectedRole = role_id;
this.selectedProviderType = 0;
this.classification = [];
this.providerType = this.getProviderType().filter((item) => {
return item.role_id === Number(role_id)
});
}
onSelectProviderType(providerType_id: number) {
this.selectedProviderType = providerType_id;
this.classification = this.getClassification().filter((item) => {
return item.providerType_id === Number(providerType_id)
});
}
getRoles() {
return [
{ id: 1, name: 'Developer' },
{ id: 2, name: 'QA Tester' },
];
}
getProviderType() {
return [
{ id: 1, role_id: 1, name: 'Java Developer' },
{ id: 2, role_id: 1, name: 'JS Developer' },
{ id: 3, role_id: 2, name: 'FrontEnd Tester' },
{ id: 4, role_id: 2, name: 'Backend Tester' },
{ id: 5, role_id: 3, name: 'Php Developer' },
]
}
getClassification() {
return [
{ id: 1, providerType_id: 1, name: 'Spring Developer' },
{ id: 2, providerType_id: 1, name: 'Angular Developer ' },
{ id: 3, providerType_id: 1, name: 'UI Tester' },
{ id: 4, providerType_id: 1, name: 'Mobile Tester' },
{ id: 5, providerType_id: 2, name: 'Hibernate Developer' },
]
}
}
APP.COMPONENT.HTML
</div>
<div class="form-group">
<label class="control-label" for="Role">Role:</label>
<select *ngIf="getRoles()" [(ngModel)]="selectedRole" (change)="onSelectRole($event.target.value)" class="form-control input-lg" id="role">
<option value="0">Select Role</option>
<option *ngFor="let role of getRoles()" value= {{role.id}}>{{role.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="ProviderType">Provider Type:</label>
<select *ngIf="getProviderType()" [(ngModel)]="selectedProviderType" (change)="onSelectProviderType($event.target.value)" class="form-control input-lg" id="providerType">
<option value="0">Select Provider Type</option>
<option *ngFor="let providerType of providerTypes" value= {{providerType.id}}>{{providerType.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="Classification">Classification:</label>
<select class="form-control input-lg" id="classification">
<option *ngIf="!selectedProviderType" value="0">Select Classification</option>
<option *ngFor="let classification of classifications" value= {{classification.id}}>{{classification.name}}</option>
</select>
</div>
<div class="form-group">
<label class="control-label" for="Specification">Specification:</label>
<select class="form-control input-lg" id="city">
<option *ngIf="!selectedClassification" value="0">Select Specification</option>
<option *ngFor="let specification of specifications" value= {{specification.id}}>{{specification.name}}</option>
</select>
</div>