question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selection model in mat-tree with checkboxes example not matching displayed selection

See original GitHub issue

Bug, feature request, or proposal:

Bug in example code

What is the expected behavior?

Selection model in example should be consistent with selected items.

What is the current behavior?

When you select a parent tree item it shows up in the selection model, however if you select all of the items under a tree individually, the parent item will show as checked, but will not appear in the selection model.

What are the steps to reproduce?

see checkboxes example from documentation: https://material.angular.io/components/tree/examples or stackblitz clone: https://stackblitz.com/angular/nkmjydodvnp?file=app%2Ftree-checklist-example.html to reproduce, click checkbox for a parent tree and view selectionmodel.selected uncheck parent tree then check all child items, selection model will be different and not match display.

What is the use-case or motivation for changing an existing behavior?

To provide good examples with predictable behavior.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

angular material 6.02

Is there anything else we should know?

easily fixed by a small change to decendantsAllSelected method. see working fix: https://stackblitz.com/angular/nkmjydodvnp?file=app%2Ftree-checklist-example.ts code:

  descendantsAllSelected(node: TodoItemFlatNode): boolean {
    const descendants = this.treeControl.getDescendants(node);
    let allselected = descendants.every(child => this.checklistSelection.isSelected(child));
    if (allselected)
      this.checklistSelection.select(node);
    else
      this.checklistSelection.deselect(node);
    return allselected;
  }

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:10
  • Comments:13

github_iconTop GitHub Comments

3reactions
tipycalFlowcommented, Apr 15, 2020

This implementation seems to handle all cases:

descendantsAllSelected(node: TodoItemFlatNode): boolean {
    const descendants = this.treeControl.getDescendants(node);
    if (descendants.length === 0) {
      return this.checklistSelection.isSelected(node);
    }
    const allselected = descendants.every(child => this.checklistSelection.isSelected(child));
    if (allselected) {
      this.checklistSelection.select(node);
    } else {
      this.checklistSelection.deselect(node);
    }
    return allselected;
  }
2reactions
ShravanDurgacommented, Oct 29, 2018

Hi, i am using this but how to do two way binding.

I have saved the record from Add page, now I am building Edit page, so out of 20 check-boxes only 10 should be checked, which will come from API call.

Any idea how to do this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular material tree with checkboxes SelectionModel
as you can see I am selecting all the items on level to that is matching a given array with item_ids, it works...
Read more >
Tracking Selections with Checkboxes in Angular
A follow up web development tutorial showing how to work with Mat-Tree Angular menus and checkboxes. Learn more.
Read more >
NgxMatSelectSearch - ngx-mat-select-search - npm
Angular component providing an input field for searching / filtering MatSelect options of the Angular Material library.
Read more >
Autocomplete | Angular Material
Start by creating the autocomplete panel and the options displayed inside it. Each option should be defined by a mat-option tag.
Read more >
React Select component - Material UI - MUI
To properly label your Select input you need an extra element with an id that contains a label. That id needs to match...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found