Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add an initial selection for Angular Material Table SelectionModel?

The Angular Material documentation gives a nice example for how to add selection to a table (Table Selection docs). They even provide a Stackblitz to try it out.

I found in the code for the SelectionModel constructor that the first argument is whether there can be multiple selections made (true) or not (false). The second argument is an array of initially selected values.

In the demo, they don't have any initially selected values, so the second argument in their constructor (line 36) is an empty array ([]).

I want to change it so that there is an initially selected value, so I changed line 36 to:

selection = new SelectionModel<PeriodicElement>(true, [{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}]);

This changes the checkbox in the header to an indeterminate state (as expected), but does not cause the row in the table to be selected. Am I setting the initial value incorrectly, or what am I missing here? How can I set an initially selected value?

like image 791
Tim Avatar asked Sep 04 '18 20:09

Tim


People also ask

What is SelectionModel angular?

SelectionModel is a utility for powering selection of one or more options from a list. This model is used in components such as the selection list, table selections and chip lists.

What is MatTableDataSource?

MatTableDataSource. Data source that accepts a client-side data array and includes native support of filtering, sorting (using MatSort), and pagination (using MatPaginator). Allows for sort customization by overriding sortingDataAccessor, which defines how data properties are accessed.


3 Answers

Tricky one. You need to initialize the selection by extracting that particular PeriodicElement object from your dataSource input, and passing it to the constructor.

In this particular case, you could code

selection = new SelectionModel<PeriodicElement>(true, [this.dataSource.data[1]);

It's because of the way SelectionModel checks for active selections.

In your table markup you have

<mat-checkbox ... [checked]="selection.isSelected(row)"></mat-checkbox>

You expect this binding to mark the corresponding row as checked. But the method isSelected(row) won't recognize the object passed in here as being selected, because this is not the object your selection received in its constructor.

"row" points to an object from the actual MatTableDataSource input:

dataSource = new MatTableDataSource<PeriodicElement>(ELEMENT_DATA);

But the selection initialization:

selection = new SelectionModel<PeriodicElement>(true, [{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'}]);

happens with a new object you create on the fly. Your selection remembers THIS object as a selected one.

When angular evaluates the bindings in the markup, SelectionModel internally checks for object identity. It's going to look for the object that "row" points to in the internal set of selected objects.

Compare to lines 99-101 and 16 from the SelectionModel source code:

isSelected(value: T): boolean {
  return this._selection.has(value);
}

and

private _selection = new Set<T>();
like image 130
Dvinubius Avatar answered Oct 17 '22 01:10

Dvinubius


I was facing the same issue, I used dataSource to set the initial value manually in ngOnInit()

ngOnInit() {
    this.dataSource.data.forEach(row => {
      if (row.symbol == "H") this.selection.select(row);
    });
  }
like image 36
Daneshwaran Avatar answered Oct 17 '22 02:10

Daneshwaran


If you do the following, it works too

selection = new SelectionModel<PeriodicElement>(true, [ELEMENT_DATA[1]])

To select all you can do

selection = new SelectionModel<PeriodicElement>(true, [...ELEMENT_DATA])

I hope the answer is helpful

like image 2
Ramesh Maharjan Avatar answered Oct 17 '22 03:10

Ramesh Maharjan