1

I'm working with angular 5 as front end, and spring boot as back end, in clients.component.html I get list of clients that exists in my database mysql.

What I want to do is to show only few information and when a user clicks on button called 'user details' he can gets the other information that appears in a ngx bootstrap modal .

This is clients.component.html :

  <!--/.col-->
<div class="col-lg-12">
  <div class="card">
    <div class="card-header">
      <i class="fa fa-align-justify"></i> Information des clients
    </div>
    <div class="card-body">
      <table class="table table-striped">
        <thead>
        <tr>
          <th>Number</th>
          <th>first name</th>
          <th>last name</th>
          <th></th>
          <th></th>
        </tr>
        </thead>
        <tbody>

        <tr *ngFor="let c of pageClients?.content">
          <td>{{c.id}}</td>
          <td>{{c.firstname}}</td>
          <td>{{c.lastname}}</td>
          <td><a (click)="delete(c)">delete</a> <a (click)="Edit(c.id)">Edit</a></td>
          <td><button type="button" class="btn btn-primary relative waves-light" data-toggle="modal"  (click)="getSpecifitClientDetails(c.id)">
            user details
          </button>
          </td>
        </tr>

        </tbody>
      </table>

      <ul class="pagination">
        <li class="page-item" [ngClass]="{'active':i==currentPage}" *ngFor="let p of pages ; let i=index">
          <a class="page-link" (click)="gotoPage(i)">{{i}}</a>
        </li>
      </ul>
    </div>
  </div>
</div>


<div bsModal #largeModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal title</h4>
        <button type="button" class="close" (click)="largeModal.hide()" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
         <div *ngFor="let p of pageClient?.content">
         <p>{{p.id}}</p>
         <p>{{p.firstname}}</p>
         <p>{{p.lastname}}</p>
         <p>{{p.tel}}</p>
         <p>{{p.email}}</p>
            </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" (click)="largeModal.hide()">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

This is client.component.ts

import { Component, OnInit , ViewChild  } from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {ClientService} from '../../../../../service/client.service';
import {Clients} from '../../Models/Clients';
import { ModalDirective } from 'ngx-bootstrap/modal';


@Component({
  selector: 'app-clients',
  templateUrl: './clients.component.html',
  styleUrls: ['./clients.component.scss']
})
export class ClientsComponent implements OnInit {

  pageClients:any;
  pageClient:any; //Specific user
  pages:Array<number>;
  currentPage:number=0;
  page:number=0;
  size:number=5;
  @ViewChild('largeModal') largeModal;
  userId:number;

  constructor(private router:Router,
              private clientService:ClientService,
              private route: ActivatedRoute) { }

  ngOnInit() {
    this.doSearch();
  }
getSpecifitClientDetails(id:number){
    this.clientService.getClient(id)
      .subscribe((data:any)=>{
        this.pageClient = data;
        this.largeModalShow();
      },err=>{
        console.log('this is error');
      })
  }


public largeModalHide(): void {
      this.largeModal.hide();    
    }
  }
 public largeModalShow(): void {
      this.largeModal.show();    
    }
  }



doSearch(){
        this.clientService.getClients(this.currentPage,this.size)
          .subscribe((data:any)=>{
            this.pageClients=data;
            this.pages=new Array(data.totalPages);
            console.log(this.pageClients);
          },err=>{
            console.log('this is error');
          })
      }


      gotoPage(i:number){
        this.currentPage = i;
        this.doSearch();
      }

      Edit(id:number){
        this.router.navigate(['edit',id], { relativeTo: this.route });
      }

      delete(p:Clients){
        let confirm = window.confirm('are you sur ');
        if(confirm==true){
          this.clientService.deleteClient(p.id)
            .subscribe(data=>{
              this.pageClients.content.splice(
                this.pageClients.content.indexOf(p),1
              );
            },err=>{
              console.log(err);
            })
        }
      }


    }

so the attribute pageClients contains all the information of all the clients , but in the table I show only id, firstname, lastname, so I want to show the rest of these information of the specific client in the modal as email, phoneNumber etc ..

EDIT

The Modal is empty , but i did consol.log(this.pageClient) and i get the right result , any idea ?

This is what i get when i click on client number 3 , in fact it's not correct 100%

 {id: 3, username: "client2", password: "$2a$10$wmaJk/6uX0/iR1Itgey/AetV3WJotUiJKTNSXEV07ZfEa8o..yMXC", firstname: "useer", lastname: "sonoom", …}
cin
:
"ciin"
client
:
{id: 1, username: "admin", password: "$2a$10$UXzvwgqxM0HA5UyHu0Rx2OJD.jjPT1hZy.iRMhZ2UOCrLgkkaSqJe", firstname: "admii", lastname: "add", …}
contrats
:
[]
devlopper
:
null
email
:
"fat@gmail.com"
id
:
3
interventions
:
Array(0)
length
:
0
__proto__
:
Array(0)
nom
:
"sonoom"
password
:
"$2a$10$wmaJk/6uX0/iR1Itgey/AetV3WJotUiJKTNSXEV07ZfEa8o..yMXC"
prenom
:
"useer"
projects
:
[]
roles
:
[{…}]
tel
:
13153098
username
:
"client2"
dEs12ZER
  • 788
  • 4
  • 24
  • 51
  • You have This.largeModalShow() - I suspect it should be this.largeModalShow() – rrd May 10 '18 at 13:53
  • @rrd No there is no problem with that , the modal appears , it's just my mistake while i was editing the post – dEs12ZER May 10 '18 at 14:01

1 Answers1

-1

Ideally,You should have 2 api's. It's on you though, One for showing list and another for showing details of particular selected.

@ViewChild('largeModal') largeModal;
  public largeModalHide(): void {
      this.largeModal.hide();    
    }
  }
 public largeModalShow(): void {
      this.largeModal.show();    
    }
  }

on click on list or button, you call a method that gets you the details, once you get the details, initialize the modal inputs or form with details and call largeModalShow()

Try if pageClient is an array:

<div class="modal-body">
        <div *ngFor="let p of pageClient">
         <p>{{p.id}}</p>
         <p>{{p.firstname}}</p>
         <p>{{p.lastname}}</p>
         <p>{{p.tel}}</p>
         <p>{{p.email}}</p>
        </div>
      </div>

In Ts:

pageClients = new Array<any>();
Nabin Kumar Khatiwada
  • 1,546
  • 18
  • 19
  • I have added attribute pageClient , the method getSpecifitClientDetails(id:number) that returns details , can you please edit your post and explain to me more ? i mean what the button should call ? and how to show the details ? – dEs12ZER May 10 '18 at 12:00
  • on the (click) event,don't show large model directly, call back-end , get your details and show. e.g. (click)="getSpecifitClientDetails(c.id)" – Nabin Kumar Khatiwada May 10 '18 at 12:04
  • Back again , can you please see the code again , i've edited it as you said , i don't get any error but the modal is empty,i did console.log(this.pageClient) and i get the right result but nothing appears in modal – dEs12ZER May 10 '18 at 13:30
  • are you getting your values properly. Binding must have happened incorrectly. I am checking your code. – Nabin Kumar Khatiwada May 10 '18 at 14:21
  • yes you're right , i've edited the post again , where do the problem come? this is weird , when i click on user number 3 i get result of client number 3 and also number 2 , i'll try your solution – dEs12ZER May 10 '18 at 14:32
  • create array object and bind to your html element. I think that is the only problem. check my edit – Nabin Kumar Khatiwada May 10 '18 at 14:34
  • I did the same as you said , when i click on user details i get : RROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed , the line caused this problem is :
    – dEs12ZER May 10 '18 at 14:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170793/discussion-between-nabin-kumar-khatiwada-and-des12zer). – Nabin Kumar Khatiwada May 10 '18 at 14:43