I am using a Primeng datatable to display editable entries from a database. When a row is clicked, a new component is displayed to allow editing of the associated data for the row. When a new row is clicked, the component will show the data for that row instead, and so on.
Obviously, this is exclusive- only one row can be selected at a time. I want to highlight the currently 'active' row in a different color, so when a new row is selected any previously hightlighted row should be reset to its default color.
At present I have the following in my template:
<p-dataTable [value]="rowData" [rowStyleClass]="highlightSelectedRow" [rowHover]="true" (onRowClick)="rowClick($event)">
<ng-template ngFor let-col [ngForOf]="cols">
<p-column [field]="col.field" [header]="col.header"></p-column>
</ng-template>
</p-dataTable>
...and in my component:
rowClick($event){
const rowData = $event.data;
rowData.selected = !rowData.selected;
// and do some other stuff...
}
highlightSelectedRow(rowData: any, rowIndex: number){
return (rowData.selected) ? 'selected' : '';
}
...CSS to style selected row:
.row.selected {
background-color: red;
}
This works insofar as highlighting the clicked row, but highlighted rows remain highlighted, and I'm wondering about the best way to reset them.
I guess I could loop through all the rows of data and find the row with selected property set to true and set it to false, but this seems a rather inefficient way of doing it, especially if I am dealing with many thousands of rows of data.
Am I missing a better, built-in way of doing this with Primeng?