[Fixed] Angular/PrimeNG open one modal dialog at index/value whenever clicking on that table value



<p-table [value]="SourcesEntities" styleClass="p-datatable-gridlines">
  <ng-template pTemplate="caption">

    <div class="container">
      <div class="row">
        <div class="col-xs-12">
  <ng-template pTemplate="header">
          <th>Entity Name</th>
          <th>Retrieval Frequency</th>
  <ng-template pTemplate="body" let-entity let-rowIndex="rowIndex">
            <button pButton
            icon="pi pi-external-link"></button>
      <!-- <p-dialog
      header="{{entity.Ename}} Attributes"
      [breakpoints]="{'960px': '75vw', '640px': '100vw'}"
      [style]="{width: '50vw'}"></p-dialog> -->

***TS code:***

import { Component, OnInit, OnDestroy } from '@angular/core';
import {SourcesService} from '../sources.service';
import { ActivatedRoute } from '@angular/router';

  selector: 'app-sources-entities',
  templateUrl: './sources-entities.component.html',
  styleUrls: ['./sources-entities.component.css']
export class SourcesEntitiesComponent implements OnInit {

  SourcesEntities = [];
  SourcesAttributes =  [];
  display: boolean = false;

    //this function should get the index from which entity is clicked
    //then return a dialog with the content of that index + its attributes!
    //ngFor somehow breaks the table
    console.log('entity clicked')
    this.display = true;

***Service.ts code:***

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { SourcesLogService } from './Sourceslog.service';

export class SourcesService {

  private  SourcesEntities = [
    {Ename: 'Employees', type:'DB Table', source: 'sourceA', frequency: 'RF'},
    {Ename: 'Product', type: 'Web Service', source: 'sourceB', frequency: 'RF'},
    {Ename: 'Movies', type: 'Source file', source: 'sourceC', frequency: 'RF'}
  //These are the attributes that should be shown when you click on an entity Ename. One for
  //each row. i.e. George for Employees

  private SourcesAttributes = [
    {name: 'George', type: 'sofware dev'},
    {name: 'PC', type:'gaming'},
    {name: 'Star-Wars: The Phantom Menace', type: 'Sci-fy'}

So in Summary I want when click on a value in Entity Name column to
pop a dialog just for that one entity cell/row that will consists of that entity’s attributes
Instead I am getting
multiple dialogs one for each row/entity name I have in my table. Hope
that helps, Let me know if I should make my question more clear or if
something doesnt make sense please

I have tried using *ngFor="" but messes up with my table somehow…Probably I have used it
on the wrong element or something. I am really new to angular so….

  [1]: https://i.stack.imgur.com/TMfb4.png  

You can see in the image my attributeData declared as

attributeData = [{
                   header: [],
                   data: [{name:''},{type:''}]

If I dont declare it that way wont work. But still that way even if it works on my html attributeData.data.name data is not recognized. Sorry is just kind of confusing for me:/

> Blockquote


  1. Update the showDialog() function by passing the header name & rowIndex to it.

    ** HTML Template **
    (click)="showDialog(entity.Ename, rowIndex)" 
    ** Component TS File **
    attributeData = {};
    showDialog(headerText, rowIndex) {
        this.attributeData['header'] = headerText;
        this.attributeData['data'] = this.SourcesAttributes[rowIndex];
        this.display = true;
  2. Place the p-dialog after the closing tag of p-table.

     header="{{attributeData.header}} Attributes"
     [breakpoints]="{'960px': '75vw', '640px': '100vw'}"
     [style]="{width: '50vw'}">
         Content Goes Here

Leave a Reply

(*) Required, Your email will not be published