3

I can get checkboxes within the table to emit changes on check/uncheck, but am having troubles reciprocating when clicking on map pins to toggle checkbox states.

my table with map

Here's my table:

      <mat-table [dataSource]="dataSource">
        <ng-container matColumnDef="number">
          <mat-header-cell *matHeaderCellDef> Number </mat-header-cell>
          <mat-cell *matCellDef="let stock">
            // #myCheckbox needs to be unique
            <mat-checkbox #myCheckbox [checked] (change)="selectStock(stock, $event)"></mat-checkbox> <a href="">{{ stock.number }}</a>
          </mat-cell>
        </ng-container>
        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;" (click)="clickRow(row._id,$event)"></mat-row>
      </mat-table> 

Then from my map, when you click on a pin, run some function

  (click)="someFunction(myCheckbox)"

In my class

    @ViewChild('myCheckbox') private myCheckbox: MatCheckbox;

    someFunction(myCheckbox){
         if (stock.isChecked) {
            this.myCheckbox.checked = false;
            } else {
            this.myCheckbox.checked = true;
         }
    }        

This is the example I'm working off of but it's applying the same #id to each checkbox so only the first checkbox gets toggled (I'm assuming I need unique a unique id for each checkbox?)

1
  • Is there any chance you still have your selectStock function code? Commented Oct 6, 2021 at 22:45

2 Answers 2

8

After more searching on the site, I was able to fix the issue using ViewChildren

Good Stuff

Sign up to request clarification or add additional context in comments.

2 Comments

go for myCheckboxes[index].checked = item.isChecked; , saves some lines of code :)
but this does not reflect on UI.. it still shows checked in the box
0

A full, working example is following. In the class implementation TypeScript-File:

import { Component, OnInit, Inject, EventEmitter, Output, OnDestroy, QueryList, ViewChildren } from '@angular/core';

...    

export class YourClassName implements OnInit, OnDestroy {

    ...

    selectables : Array<string> = ['ah','be','ce'];
    @ViewChildren('checkboxMultiple') private checkboxesMultiple : QueryList<any>;

    ...

    selectionChange( event ) : void {

        // get index from template loop or so. In this case the value
        // of the mat-checkbox is an Array with the value as first and
        // the loop index as second entry
        let index = event.source.value[1];
        let checkboxesArray = this.checkboxesMultiple.toArray();
        checkboxesArray[index].checked = false;

        // You could also loop over all checkboxes and check/uncheck
        // them based on your logic
    }
}

And inside your template file:

...

<mat-checkbox #checkboxMultiple
        *ngFor="let selectable of selectables; let i = index"
        (change)="selectionChange($event)"
        [value]="[selectable, i]">
        {{ selectable }}&nbsp;&nbsp;
</mat-checkbox>

I found that you have to use the toArray() method of QueryList object to get the actual array of checkboxes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.