0

Helloo, i want to push object to array element data and to show in table, but when i do it nothing happend, my table is empty. I am new to this and i am learning, where i made a mistake?

I have app component and one more dialog component, when i insert information into dialog, on button add, on close dialog, i send that data and push into data element array.

App component:

    export interface PeriodicElement {
      videoname: string;
      url: string;
      author: string;
      description: string;
    }

    const ELEMENT_DATA: PeriodicElement[] = [];

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })

    export class AppComponent {
      title = 'Zadatak';

      displayedColumns: string[] = ['videoname', 'author', 'description', 'url' ];
      dataSource = ELEMENT_DATA;

      constructor(public dialog: MatDialog, private changeDetectorRefs: ChangeDetectorRef) {}


      openDialog(): void {
      const dialogRef = this.dialog.open(AddVideoFormComponent);
      dialogRef.afterClosed().subscribe(data => {
      ELEMENT_DATA.push(data);
      });
      }
    }

Dialog component:

export class AddVideoFormComponent implements OnInit {
  videoForm: FormGroup;

  constructor(public dialogRef: MatDialogRef<AddVideoFormComponent>) { }

  ngOnInit() {
    this.videoForm = new FormGroup({
      videoname : new FormControl('', Validators.required),
      url : new FormControl('', Validators.required),
      author : new FormControl('', Validators.required),
      description : new FormControl('', Validators.required),
    });
  }

  onSubmit() {
    this.dialogRef.close(this.videoForm.value);
  }
}
  • Can you please share stackblitz example? – prathameshk73 Mar 24 '20 at 14:19
  • https://stackoverflow.com/questions/47581267/how-to-add-data-dynamically-to-mat-table-datasource/56948608#56948608 – Eliseo Mar 24 '20 at 14:44
  • In this example there is only 1 component, i don't understand how to connect add() with button in other component? Thanks. I did it like this but still don't work: openDialog(): void { const dialogRef = this.dialog.open(AddVideoFormComponent); dialogRef.afterClosed().subscribe(data => { this.dataSource.data.push(ELEMENT_DATA[this.index++]); this.table.renderRows(); }); } – Mladen Nikolić Mar 24 '20 at 15:03
  • no, your push mush be `this.dataSource.push(data);`. In my e.g. push an element from an array, you add an element from the data you filled in the Dialog box – Eliseo Mar 24 '20 at 15:20

1 Answers1

1

just add a reference variable in your table (yes, the #table)

<table #table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
  ...
</table>

Add viewChild in your app.component

import {MatTable} from '@angular/material/table' //<--you need import MatTable

@ViewChild('table', { static: true,read:MatTable }) table

In your funcion openDialog

 openDialog(): void {
      const dialogRef = this.dialog.open(AddVideoFormComponent);
      dialogRef.afterClosed().subscribe(data => {
        this.dataSource.push(data); //<--make the push on dataSource
        this.table.renderRows()  //<--say to Angular that render the rows
      });
  }
Eliseo
  • 50,109
  • 4
  • 29
  • 67