Want to load a table with checkbox already checked with the initial values that come from a local storage variable. I already put the initial data in the selectionModel:
this.servChecked = [
{tipo: 'Servicio', descripcion: 'Visit', cantidad: '1', codsrv: '11123'},
{tipo: 'Equipo', descripcion: 'RPV', cantidad:'1', codsrv: '11124'}
];
this.selection = new SelectionModel<ServiciosAdicionales>(true, this.servChecked);
It does load the array in the selection.selected but the checkboxs are not checked.
TS:
this.servChecked = JSON.parse(localStorage.getItem("servAdicionales"));
this.infoCompleta = JSON.parse(localStorage.getItem("infoParaServiciosAdicionales"));
this.tipoServicio = this.route.snapshot.paramMap.get("tipoServicio");
let listEquipos = [];
let listServicios = [];
this.datosServicios.forEach(element => {
switch(element.tipo) {
case '1':
element.tipo = 'Servicio'
listServicios.push(element);
break;
case '2':
element.tipo = 'Equipo'
listEquipos.push(element);
break;
}
});
this.dataSource = new MatTableDataSource<ServiciosAdicionales>(this.datosServicios);
this.dataSourceEquipo = new MatTableDataSource<ServiciosAdicionales>(listEquipos);
this.dataSourceServicio = new MatTableDataSource<ServiciosAdicionales>(listServicios);
this.selection = new SelectionModel<ServiciosAdicionales>(true, this.servChecked);
I want the checkboxs to be checked when initial values are loaded.
The object you are trying to push into the selection model is from the array dataosServicios. But the selection model looks for the object which will get rendered in the table. This will be the actual row object which is different from the array.
After initializing the dataSource, try the following code:
this.selection = new SelectionModel<ServiciosAdicionales>(true, this.dataSourceEquipo.data[0]);
The above code will select the first row in the table.
To answer your question get the data from localStorage and then create the dataSource for the table. Map the object from localStorage to the dataSource.data and then add dataSource.data objects to the selectionModel array.