I'm trying to pass two strings in Angular Material. The HTML code in the Mat Dialog content is to create 2 cdk drop lists where the values can be exchanged between arrays.
I went through this answer for a similar problem and tried implenting it but it didn't work. How to pass data to dialog of angular material 2
This is my code for the dialog class as well as the class where the dialog class is called.
export class TableComponent implements OnInit {
displayedColumns: string[] = ['A', 'B', 'C'];
availableColumns: string[] = [];
openDialog() {
const dialogRef = this.dialog.open(ColumnsDialog, {
width: '500px',
height: '500px',
data: {
displayedColumns: this.displayedColumns,
availableColumns: this.availableColumns
},
});
}
@Component({
selector: 'columns-dialog',
templateUrl: 'columns-dialog.html',
changeDetection:ChangeDetectionStrategy.OnPush
})
export class ColumnsDialog {
constructor(public dialogRef: MatDialogRef<ColumnsDialog>,
@Inject(MAT_DIALOG_DATA) public data: any) {}
onCloseClick(): void {
this.dialogRef.close(true);
}
drop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data, event.previousIndex,
event.currentIndex);
} else {
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}
}
}
This is my code for the contents of the mat-dialog.
<mat-dialog-content>
<div >
<h2>Unselected Columns</h2>
<div
cdkDropList
#availableColumnsList="cdkDropList"
[cdkDropListData]="availableColumns"
[cdkDropListConnectedTo]="[displayedColumnsList]"
(cdkDropListDropped)="drop($event)">
<div *ngFor="let item of availableColumns" cdkDrag>{{item}}</div>
</div>
</div>
<div >
<h2>Selected Columns</h2>
<div
cdkDropList
#displayedColumnsList="cdkDropList"
[cdkDropListData]="displayedColumns"
[cdkDropListConnectedTo]="[availableColumnsList]"
(cdkDropListDropped)="drop($event)">
<div *ngFor="let item of displayedColumns" cdkDrag>{{item}}</div>
</div>
</div>
</mat-dialog-content>
When I pass the displayedColumns and availableColumns string, I want 2 cdk drop lists where the values of the array displayeColumns can be passed to available columns and vice versa. I was able to achieve this when I was using a dropdown menu for the button upon clicking which this dialog is opening now so I know the code for the cdk drop lists works. I think the issue is in passing data to the mat-dialog.
Also, I don't want the mat-dialog to create its own copy of displayedColumns and availableColumns for the display since I'm using these string arrays to change the displayed columns of a Angular Material Table.