5

i have some data bind in table and on click of any specific i want to show current clicked object more related data in to another component(child component)

for example the data I'm taking from this link: http://jsonplaceholder.typicode.com/users

HTML Code:

<table>
  <thead>
    <th>
      Id
    </th>
    <th>
      name
    </th>
    <th>
      username
    </th>
    <th>
      email
    </th>
    <th>
      street
    </th>
    <th>
      suite
    </th>
    <th>
      zipcode
    </th>
    <th>
      phone
    </th>
    <th>
      website
    </th>
    </thead>
  <tbody>
    <tr *ngFor="let data of httpdata"> 
      <td>{{data.id}}
      </td> 
      <td>{{data.name}}
      </td> 
      <td>{{data.username}}
      </td> 
      <td>{{data.email}}
      </td> 
      <td>{{data.address.street}}
      </td> 
      <td>{{data.address.city}}
      </td> 
      <td>{{data.address.suite}}
      </td> 
      <td>{{data.address.zipcode}}
      </td> 
      <td>{{data.phone}}
      </td> 
      <td>{{data.website}}
      </td> 
      <td>
        <a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to 
        </a> 
      </td> 
    </tr>
  </tbody>
</table>

if you see there is one go to button i have in table when i click on any specific data it should show me complete info about current clicked but in my case i want to bind the data in another component when i click on go to for specific that all td data should be display in new component(child).

simple i want to track click event for selected data in child component . and the table is rendered in parent component.

if you can see in this screenshot i can bind the data in same component ,

attached is the data table I have.

the table where i have bind the data

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Amit Golhar
  • 799
  • 4
  • 8
  • 21
  • Can you provide stackblitz example? – Prashant Pimpale Dec 10 '18 at 10:23
  • As I understand, on click on particular Item you want to display that user's date in another component, right? – Prashant Pimpale Dec 10 '18 at 10:24
  • Yeah right, for example on my first component i'm going to show only two columns like name and username. and rest of the things i want to display in another component when user will click on any specific or go to button – Amit Golhar Dec 10 '18 at 10:26
  • Need HTML code to work with, can you post it in the question? – Prashant Pimpale Dec 10 '18 at 10:34
  • sure will do :) thanks in advance – Amit Golhar Dec 10 '18 at 10:36
  • {{data.id}} {{data.name}} {{data.username}} {{data.email}} {{data.address.street}} {{data.address.city}} {{data.address.suite}} {{data.address.zipcode}} {{data.phone}} {{data.website}} Go to – Amit Golhar Dec 10 '18 at 10:43
  • User:{{selectedUser.id }}

    Name: {{selectedUser.name}}

    Email: {{selectedUser.email}}

    Phone: {{selectedUser.phone}}

    Website: {{selectedUser.website}}

    – Amit Golhar Dec 10 '18 at 10:43
  • sorry i don't see question edit option so posting it here only :( – Amit Golhar Dec 10 '18 at 10:44
  • export class ConflictComponent implements OnInit { private httpdata: any; public selectedUser = this.httpdata; constructor(public myserviceservice: MyserviceService) { } ngOnInit() { this.getUsers(); } getUsers() { this.myserviceservice.getUsers().subscribe(data => { this.httpdata = data ; }, err => console.error(err), () => console.log('done loading users') ); } onSelect(data): void { this.selectedUser = data; } .TS - parent – Amit Golhar Dec 10 '18 at 10:46
  • Is it ok if your child component opened as Dialog Box? – Prashant Pimpale Dec 10 '18 at 11:25
  • No, it should be open in second component component and second component will have one back button to get back to the table component – Amit Golhar Dec 10 '18 at 11:51
  • I have the solution without another component! that means, Without child component! – Prashant Pimpale Dec 10 '18 at 12:27
  • Please tell . But make sure I don’t have to render current clicked object in same component. I want to show it separately because there are lot of information I have to show that’s why I’m taking it on separate component – Amit Golhar Dec 10 '18 at 12:31
  • Have added an answer with description! – Prashant Pimpale Dec 10 '18 at 13:25

2 Answers2

3

You can use the @Input and @Output decorator to achieve the required output:

Changes:

In parent Component:

HTML Code:

<div>
  <table *ngIf="isVisible === true">
    <tr>
      <th>
        Id
      </th>
      <th>
        name
      </th>
      <th>
        username
      </th>
      <th>
        email
      </th>
    </tr>
    <tr *ngFor="let data of userInformation">
      <td>{{data.id}}
      </td>
      <td>{{data.name}}
      </td>
      <td>{{data.username}}
      </td>
      <td>{{data.email}}
      </td>
      <td>
        <a (click)="onSelect(data)">Go to
        </a>
      </td>
    </tr>
  </table>

  <div *ngIf="isVisible === false">
    <app-test-child [userInfo]="clickedUser" (notify)="backToList($event)"></app-test-child>
  </div>
</div>

TS Code:

Local variables:

userInformation: any;
isVisible : boolean = true;
clickedUser: any;

Two functions in the parent component:

onSelect(data)
{
   this.isVisible = false;
   this.clickedUser = data;
}

backToList(flag) {
  this.isVisible = flag;
  console.log(flag)
}

In Child Component:

HTML code:

<table>
  <tr>
    <th>
      Id
    </th>
    <th>
      name
    </th>
    <th>
      username
    </th>
    <th>
      email
    </th>
  </tr>
  <tr>
    <td>{{clickedUser.id}}
    </td>
    <td>{{clickedUser.name}}
    </td>
    <td>{{clickedUser.username}}
    </td>
    <td>{{clickedUser.email}}
    </td>
    <td>
      <a (click)="backToList()">Back
      </a>
    </td>
  </tr>
</table>

TS Code:

import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';


@Input() userInfo: any;
@Output() notify: EventEmitter<any> = new EventEmitter<any>();

clickedUser: any;

constructor() { }

ngOnInit() {
  this.clickedUser = this.userInfo;
}

backToList() {
  var flag = true;
  this.notify.emit(flag);
}
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
  • 1
    thanks @prashant-pimpale, seems useful solution , let me try this way – Amit Golhar Dec 10 '18 at 13:26
  • 1
    Yeah @prashant it's it works for me with some small modification!!!!!! Thanks a lot – Amit Golhar Dec 11 '18 at 06:50
  • Hello @Prashant Pimpale, my requirement is almost same as like this question, as i need to get Id from my parent component to child component on when i click on the button of parent component. But i am getting this error `Template parse errors: Can't bind to 'editId' since it isn't a known property of 'button'.` Can you pls help ? stackblitz.com/edit/angular-duduth – TMA Mar 23 '20 at 12:40
  • @aananddham have you posted question for this? – Prashant Pimpale Mar 23 '20 at 12:43
  • @aananddham need explanation, do you id of that button to available in the parent? – Prashant Pimpale Mar 23 '20 at 12:47
  • Yes it is available in the parent component. https://stackblitz.com/edit/angular-duduth Pls review a comment that i have mentioned on the buttonclick event `editDetails()` – TMA Mar 23 '20 at 12:48
  • @aananddham You can't! to work above code you have to use `` for you must have to read documentation – Prashant Pimpale Mar 23 '20 at 12:51
  • Perfect... can you pls provide me a exact link so that i can get better explanation of documentation. ? – TMA Mar 23 '20 at 12:54
  • @aananddham yes I will, but I am afraid, about your requirement – Prashant Pimpale Mar 23 '20 at 12:55
  • @aananddham I will request you to open a new question with Expected behaviour – Prashant Pimpale Mar 23 '20 at 12:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210160/discussion-between-aananddham-and-prashant-pimpale). – TMA Mar 23 '20 at 12:57
2

try this

In HTML update

<a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to 
        </a> 

to

<a (click)="onSelect(data)">Go to 
            </a> 

In Parent Component

import { Router } from '@angular/router';

export class ParentComponent implements OnInit {
   constructor(private router: Router) {
   }
   onSelect(data) {
      this.router.config.find(r => r.component == ChildComponent).data = data;
      this.router.navigate(["/conflict-details/conflict"]);
   }
}

In Child Component

import { ActivatedRoute } from '@angular/router';

export class ChildComponent implements OnInit {
   SentItem : any;
   constructor(private router: ActivatedRoute) { }
   ngOnInit() {
      this.router.data.subscribe(r=>this.SentItem =r);
  }
}
Mostafa Sayed
  • 256
  • 1
  • 10