hello I need to retrieve the values entered by a form after the post method so I inserted at the service level this code :
list:Client[];
constructor(private http:HttpClient) { }
postClient(formData:Client){
return this.http.post(this.rootURL+'/Clients/',formData);
}
putClient(formData:Client){
return this.http.put(this.rootURL+'/Clients/'+formData.Engagement,formData);
}
getClient(formData:Client){
return this.http.get(this.rootURL+'/Clients/GetClientByName/'+formData.Engagement);
}
and at the component level like this :
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
and in the HTML code this:
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr *ngFor="let clt of client">
<td >{{clt.Clientname}}</td>
<td >{{clt.Engagement}}</td>
<td >{{clt.ERP}}</td>
and I can’t get the values by ID with the get I don’t know what is the problem I have neither result or error message
I think your http service(this.rootURL+'/Clients/GetClientByName/) return Client[] not Client.
So, You should cast like this.
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client[]}
)
But, your response is fixed
{"Engagement":"56789","Clientname":"ClLIENT","ERP":"ERP"}
at component level, no need to edit
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{this.client = res as Client}
)
}
you should edit html file.
<table class="table table-hover">
<tr>
<th class="tname">Client</th>
<th class="tname">Enagement</th>
<th class="tname">ERP</th>
</tr>
<tr>
<td >{{client.Clientname}}</td>
<td >{{client.Engagement}}</td>
<td >{{client.ERP}}</td>
</tr>
---------------------------
or need to use ngFor loop, edit component level. and no need to edit component level.
client: Client[] = [];
getClient(form:NgForm){
this.clientservice.getClient(form.value).subscribe(
res =>{
const cli = res as Client;
this.client.length = 0;
Array.prototype.push.apply(this.client, cli)
}
)
}
Related
I'm working in Spring Boot and I have a problem rendering with Thymeleaf a table with different lines. First must be a String, and the subsequent lines must be the data saved in a list of objects.
situation of the problem:
I have a list of objects, this object has two attributes, one is a list of Strings, and the other one is a list of different objects. I don't know how to render in Thymeleaf in a table the first attribute of a string list in a line, and on the next lines of the table render the second list of attribute object.
details of the object:
public class objetosDeServiciosAD {
private String Servicio;
private LinkedList<usuarioAD> listaUsuariosAD;
public String getServicio() {
return Servicio;
}
public void setServicio(String servicio) {
Servicio = servicio;
}
public LinkedList<usuarioAD> getListaUsuariosAD() {
return listaUsuariosAD;
}
public void setListaUsuariosAD(LinkedList<usuarioAD> listaUsuariosAD) {
this.listaUsuariosAD = listaUsuariosAD;
}
#Override
public String toString() {
return "objetosDeServiciosAD [Servicio=" + Servicio + ", listaUsuariosAD=" + listaUsuariosAD + "]";
}
}
objetos_Servicios is a list of objects with two atributes, one is servicio
this object has a second attibute which is a list of objects, this is listaUsuariosAD.
This is my code in Thymeleaf:
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Usuario</th>
<th scope="col">Teléfono</th>
<th scope="col">mail</th>
<th scope="col">Descripción</th>
</tr>
</thead>
<tbody>
<tr th:each="servicio : ${objetos_Servicios}">
<td th:text="${servicio.servicio}"></td>
<tr th:each=" listaeusuario : ${servicio.listaUsuariosAD}">
<tr th:each ="usuarios : ${listaeusuario}">
<td th:text = "${usuarios.usuario}"></td>
<td th:text = "${usuarios.telefono}"></td>
<td th:text = "${usuarios.mail}"></td>
<td th:text = "${usuarios.descripion}"></td>
</tr>
</tr>
</tbody>
</table>
The code will look something like this:
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Usuario</th>
<th scope="col">Teléfono</th>
<th scope="col">mail</th>
<th scope="col">Descripción</th>
</tr>
</thead>
<tbody>
<th:block th:each="servicio : ${objetos_Servicios}">
<tr>
<td th:text="${servicio.servicio}" />
</tr>
<tr th:each = "lista : ${servicio.getListaUsuariosAD()}">
<td th:text="${lista.usuario}"></td>
<td th:text="${lista.telefono}"></td>
<td th:text="${lista.mail}"></td>
<td th:text="${lista.Descripcion}"></td>
</tr>
</th:block>
</tbody>
</table>
You can use a th:block tag to loop over a larger block of code (that contains the header <tr /> and the rows <tr />).
I recommend changing the naming standards you are using, so that all your class names begin with an upper-case letter - for example: ObjetosDeServiciosAD instead of objetosDeServiciosAD. This is standard in Java - and not doing this can be confusing for other people who read your code.
So, your class becomes:
import java.util.List;
public class ObjetosDeServiciosAD {
private String servicio;
private List<UsuarioAD> listaUsuariosAD;
public String getServicio() {
return servicio;
}
public void setServicio(String servicio) {
this.servicio = servicio;
}
public List<UsuarioAD> getListaUsuariosAD() {
return listaUsuariosAD;
}
public void setListaUsuariosAD(List<UsuarioAD> listaUsuariosAD) {
this.listaUsuariosAD = listaUsuariosAD;
}
}
I also replaced LinkedList with List, since you do not appear to need a linked list here (if you actually do, you can revert that change).
Then, for your Thymeleaf template, you can use Thymeleaf's <th:block> tag to structure your iteration loops:
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th scope="col">Usuario</th>
<th scope="col">Teléfono</th>
<th scope="col">mail</th>
<th scope="col">Descripción</th>
</tr>
</thead>
<tbody>
<th:block th:each="servicio : ${objetos_Servicios}">
<tr>
<td th:text="${servicio.servicio}" />
<td></td>
<td></td>
<td></td>
</tr>
<tr th:each = "lista : ${servicio.listaUsuariosAD}">
<td th:text="${lista.usuario}"></td>
<td th:text="${lista.telefono}"></td>
<td th:text="${lista.mail}"></td>
<td th:text="${lista.descripcion}"></td>
</tr>
</th:block>
</tbody>
</table>
In the above code, I also replaced ${servicio.getListaUsuariosAD()} with the simpler ${servicio.listaUsuariosAD}, since you do not need to explicitly call the method, here.
I also added three empty <td></td> cells to ensure each row is complete, for the row displaying the servicio text.
So I have this, and what I want to get is the ID or Key from each user.
For example: WxA2XLigx7V1bOxm5WNSnVkgtOu1
And I'm currently showing this so far:
This is my current code that shows the table
firebase.database().ref('Users/').on('value',(data)=>{
let Users = data.val();
document.getElementById('tablaUsers').innerHTML+='';
for (const user in Users){
document.getElementById('tablaUsers').innerHTML+=`
<tr>
<td>${Users[user].Key}</td>
<td>${Users[user].email}</td>
<td>${Users[user].name}</td>
</tr>
`;
}
And this is the code from the html
<table class="mdl-data-table mdl-js-data-table">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">ID</th>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">Email</th>
<th class="mdl-data-table__cell--non-numeric" role="columnheader" scope="col">Nombre</th>
</tr>
</thead>
<tbody id="tablaUsers">
<tr>
<td class="mdl-data-table__cell--non-numeric"></td>
<td class="mdl-data-table__cell--non-numeric"></td>
<td class="mdl-data-table__cell--non-numeric"></td>
</tr>
</tbody>
</table>
As you see my
<td>${Users[user].Key}</td>
Is not working, it's just a placeholder. It may be a simple problem but I can´t get it or how to do it, hope anyone can help.
You should loop on the JavaScript object returned by the val() method, as follows:
firebase
.database()
.ref('users/')
.on('value', (data) => {
var obj = data.val();
Object.keys(obj).forEach((key) => {
console.log('key: ' + key);
console.log('mail: ' + obj[key].mail);
console.log('name: ' + obj[key].name);
});
});
I want to populate my grid with the data which i am receiving from API
below is my API response which i am getting
[
{
"intUserId": 1109,
"vcrFirstName": "sdvlbs",
"vcrLastName": "dsbvsdfn",
"vcrLoginName": "!##DBASDI?\"|}",
"vcrPassword": "eclerx#123",
"vcrStatus": "InActive",
"vcrIsClient": "eClerx",
"vcrEmail": "sdvlbs.dsbvsdfn#eClerx.com",
"vcrRole": "Analyst,Auditor,Team Lead,Manager",
"Agreements_Selected": "ISDA,CSA,Repo,SLA,PBA"
}
]
But at the time of passing this data to HTML by grid is still blank
Below is my .ts code
arrBirds:Array<string> = [];
userdata(){
this.http.get(this.url).subscribe((res)=>{
this.arrBirds = Array.of(res.json()) ;
console.log(this.arrBirds)
});
}
Below is my HTML
<table cellpadding="0" width="100%" cellspacing="0" border="0" class="search_grid_view table-striped">
<thead>
<tr>
<th style="min-width: 100px;">Login Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let char of arrBirds">
<td >
{{char.vcrPassword}}
</td>
<td >
{{char.vcrStatus}}
</td>
<td >
{{char.Agreements_Selected}}
</td>
<td >
{{char.vcrEmail}}
</td>
</tr>
</tbody>
</table>
i am trying to iterate the data which i am receiving from API response
Below is my API code
public HttpResponseMessage GetCheckBoxes()
{
HttpResponseMessage response;
UserDB userDB = new UserDB();
DataSet ds = userDB.UserGridDetails();
string json = JsonConvert.SerializeObject(ds.Tables[0], Formatting.Indented);
//response = Request.CreateResponse(HttpStatusCode.OK, "[" + json + "]");
response = Request.CreateResponse(HttpStatusCode.OK, json);
return response;
}
my grid doesn't populate with the data.
i am struggling since last 5 days.
I think html file is not in sync with http response
<tr *ngFor="let char of arrBirds">
<td > {{char?.vcrPassword}}</td>
<td > {{char?.vcrStatus}} </td>
<td > {{char?.Agreements_Selected}}</td>
<td > {{char?.vcrEmail}} </td>
</tr>
Change this in tr,it will work.
If it still don't work then import ChangeDetectorRef from angular/core
inject it in constructor and use detectchanges method in ChangeDetectorRef
import { Component, Input, ChangeDetectionStrategy, ChangeDetectorRef } from '#angular/core';
constructor(private ref: ChangeDetectorRef) {}
userdata(){
this.http.get(this.url).subscribe((res)=>{
this.arrBirds = Array.of(res.json()) ;
this.ref.detectChanges();
console.log(this.arrBirds)
});
}
Hope It helps.
By parsing the JSON data it is working now.
userdata(){
this.http.get(this.url).subscribe((res)=>{
this.arrBirds = JSON.parse(res.json()) as Application[];
console.log(this.arrBirds)
});
}
I had succeed on creating a table and implement datatable on my angular 5 project
html code for the table:
<table class="table table-hover mt-3" datatable [dtOptions]="dtOptionsContent" [dtTrigger]="dtTriggerContent">
<thead>
<tr>
<th scope="col">Content Type</th>
<th scope="col">User Occupation</th>
<th scope="col">Content Name</th>
<th scope="col">Carrot</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let reward of rewards" >
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{getRewardType(reward.type)}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{getRoleType(reward.role)}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{reward.name}}</td>
<td data-toggle="modal" data-target="#seeDetail" (click)="onSelect(reward)">{{reward.carrot}}</td>
</tr>
</tbody>
</table>
.ts code for the datatable settings:
dtOptionsContent: DataTables.Settings = {};
dtTriggerContent: Subject<any> = new Subject();
#ViewChild(DataTableDirective)
dtElementContent: DataTableDirective;
.ts for get rewards:
getRewards(): void
{
this.adminService.getRewards().subscribe(rewards=> {
this.rewards = rewards;
this.dtTriggerContent.next();
});
}
The sorting, pagination, and searching are functional and when i add/delete a content to the table , the table is updated.
However, after updated, when i tried the sorting and pagination, the table content revert to the pre-added/deleted list.
I've tried destroy and rerender the table. This is the .ts code for adding content:
addSocial(role: number, rewardName: string, carrot: number): void
{
this.dtElementContent.dtInstance.then((dtInstance: DataTables.Api) => {
let currentCarrot = 0;
if(!isNaN(carrot))
{
//Setting for rewardTemp goes here
this.adminService.addReward(this.rewardTemp)
.subscribe(reward => {
dtInstance.destroy();
this.rewards.push(reward);
this.dtTriggerContent.next();
});
//Success Notification goes here
}
else
//Error Notification goes here
});
}
while it succeed to sorting the updated content after the list was updated, it spawn warning alert after rerender.
The alert contains "DataTables warning: table id=DataTables_Table_1 - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3"
What is wrong??
I want to create HTML table like following
# Class Method A b c d
1 User get 10 20 30 40
set 40 30 20 10
find 40 30 20 10
2 Profile get 10 20 30 40
set 40 30 20 10
find 40 30 20 10
I have the following structure
export class Profiler {
constructor(
public classz: string,
public methodProfilers: {[key: string]: MethodProfiler;}
) {}
}
export class MethodProfiler {
constructor(
public count: number,
public totalTime: number,
public lastTotalTime: number,
public maxTime: number,
public avgTime: number,
public avgMemory: number
) {}
}
Is it possible to create such Html table using angular 4 *ngfor ? I am getting list of Profiler from back end.
getProfilerKeys(methodProfilers: Map<string, MethodProfiler>) {
return Object.keys(methodProfilers);
}
<div class="table-responsive">
<table class="table table-sm table-bordered">
<thead class="thead-default">
<tr>
<th class="text-center">Classz</th>
<th class="text-center">Method</th>
<th class="text-center">Count</th>
<th class="text-center">TotalTime</th>
<th class="text-center">LastTotalTime</th>
<th class="text-center">MaxTime</th>
<th class="text-center">AvgTime</th>
<th class="text-center">AvgMemory</th>
</tr>
</thead>
<tbody>
<tr class="text-center" *ngFor="let profiler of page.content;">
<td>{{profiler.classz}}</td>
<td>
<table>
<tr *ngFor="let key of getProfilerKeys(profiler.methodProfilers);">
<td>{{key}}</td>
</tr>
</table>
</td>
<td>
<table>
<tr *ngFor="let key of getProfilerKeys(profiler.methodProfilers);">
<td *ngFor="let subkey of getProfilerKeys(profiler.methodProfilers[key]);">{{profiler.methodProfilers[key][subkey]}}</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</div>
It is more easy to achieve IMHO if you expand out the *ngFor into its <ng-template> equivalent.
Then create one td for each Profile and set the rowspan attribute on the td to be the number of "ProfilerKeys" in the array.
I use the function isNewIndex() to detect when the outer loop (Profiler objects) changes its index to draw another row-spanned td.
import { Component } from '#angular/core';
import { Profiler, MethodProfiler } from './profile';
#Component({
selector: 'my-app',
template: `
<table class="table table-sm table-bordered" width="100%">
<thead class="thead-default">
<tr>
<th class="text-center">#</th>
<th class="text-center">Class</th>
<th class="text-center">Method</th>
<th class="text-center">Count</th>
<th class="text-center">TotalTime</th>
<th class="text-center">LastTotalTime</th>
<th class="text-center">MaxTime</th>
<th class="text-center">AvgTime</th>
<th class="text-center">AvgMemory</th>
</tr>
</thead>
<tbody>
<ng-template ngFor let-profiler [ngForOf]="profilers" let-i="index">
<ng-template ngFor let-method [ngForOf]="getProfilerKeys(profiler.methodProfilers)">
<tr class="text-center">
<td *ngIf="isNewIndex(i)" [attr.rowspan]="getProfilerKeys(profiler.methodProfilers).length">{{i + 1}}</td>
<td *ngIf="isNewIndex(i, true)" [attr.rowspan]="getProfilerKeys(profiler.methodProfilers).length">{{profiler.classz}}</td>
<td>{{method}}</td>
<td>{{profiler.methodProfilers[method].count}}</td>
<td>{{profiler.methodProfilers[method].totalTime}}</td>
<td>{{profiler.methodProfilers[method].lastTotalTime}}</td>
<td>{{profiler.methodProfilers[method].maxTime}}</td>
<td>{{profiler.methodProfilers[method].avgTime}}</td>
<td>{{profiler.methodProfilers[method].avgMemory}}</td>
</tr>
</ng-template>
</ng-template>
</tbody>
</table>
`
})
export class AppComponent {
lastIndex = -1;
getProfilerKeys(methodProfilers: Map<string, MethodProfiler>) {
return Object.keys(methodProfilers);
}
// on last call to this function per row pass true for the updateIndex param
isNewIndex(thisIndex: number, updateIndex : boolean) {
if (thisIndex === this.lastIndex) return false;
if (updateIndex === true) this.lastIndex = thisIndex;
return true;
}
profilers = [
new Profiler('User',
{
'get' : new MethodProfiler(10,20,30,40,50,60),
'set' : new MethodProfiler(1,2,3,4,5,6)
}
),
new Profiler('Profile',
{
'get' : new MethodProfiler(60,50,40,30,20,10),
'set' : new MethodProfiler(6,5,4,3,2,1)
}
)
];
}
Demo: https://plnkr.co/edit/B5YYn9Jxx9nQCzxlr2VS?p=preview