Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6: retrieve data from SelectionModel

I have a component which has a mat-table with checkboxes using SelectionModel. Here is the code I have to retrieve the data selected.

fileSelect = new SelectionModel<FileInfo>(true, []);

This method is invoked in ngSubmit of the form.

sendFileInterrupt() {
 let selectedFileIds: string[];
    for (let item of this.fileSelect.selected) {
      console.log(item.fileId);
      selectedFileIds.push(item.fileId);
    }

The selected fileId is logged in console, but when trying to add it to the selectedFileIds array, I'm getting an error

Cannot read property 'push' of undefined

Do I have to instantiate or initialize the array before pushing data into it ?

like image 974
jijo Avatar asked Mar 05 '23 12:03

jijo


2 Answers

You need to initialize let selectedFileIds: string[] = [];

sendFileInterrupt() {
 let selectedFileIds: string[] = [];
    for (let item of this.fileSelect.selected) {
      console.log(item.fileId);
      selectedFileIds.push(item.fileId);
    }
like image 198
Suresh Kumar Ariya Avatar answered Mar 08 '23 17:03

Suresh Kumar Ariya


You don't need to iterate through this.fileSelect.selected, it is already an array of rows rows[];

like image 43
Kurkov Igor Avatar answered Mar 08 '23 19:03

Kurkov Igor