mardi 7 juillet 2020

Not able to deselect or unchecked single row using function in angular material mat table selection and angular 2

use case is I want to select item only if its valid selection.

this.selection.toggle(row); or this.selection.deselect(row); are able to remove item from this.selection.selected array but these are not removing visual selection from view.

    HTML Code:     
<ng-container matColumnDef="select">
        </mat-header-cell>
            <mat-cell *matCellDef="let row" style="flex-grow: 0.4;">
                  <mat-checkbox *ngIf="isVisible(row.statusId)" (click)="$event.stopPropagation()"
                     (change)="$event ? formDataChecked(row) : null" [checked]="isChecked(row)">
                   </mat-checkbox>
            </mat-cell>
    </ng-container>
    
    
    TS Code:
            isChecked(row) {
            this.selection.isSelected(row);
        }
    
        formDataChecked(row) {
            this.selection.toggle(row);
            this.validId = this.selection.selected.length === 1 ? row.statusId : this.validId;
    
            if (this.selection.selected.length > 1) {
                let index: number = this.selection.selected.findIndex(f => f.statusId !== this.validId);
                if(index > -1) {
                    this.isChecked(row);
                    this.selection.deselect(row);
                    this.ams.showAlert("Invalid Selection!", 'Please select appropriate item!');
    
                    // this.selection.toggle(row);
                }
            }
        }



Aucun commentaire:

Enregistrer un commentaire