I am trying to highlight table row when user scroll mouse. By default I am showing first row highlighted. I am using Hostlistener with mousewheel event to do this. Table rows are highlighing on scroll but it is moving very fast. I want to make it smooth. Here is my code:
app.component.html
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns; let i= index"
[ngClass]="{'highlightTableColor': selectedRowIndex == i}"></mat-row>
</mat-table>
app.component.ts
import { Component, HostListener } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
selectedRowIndex: number = 0;
@HostListener('mousewheel', ['$event'])
handleScroll(event) {
console.log('adadsdasda', event);
this.selectedRowIndex++;
}
}
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 4, name: 'Beryllium Beryllium BerylliumBeryllium Beryllium Beryllium Beryllium Beryllium Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 7, name: 'Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
{position: 11, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 12, name: 'Helium Helium Helium Helium Helium Helium', weight: 4.0026, symbol: 'He'},
{position: 13, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 14, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 15, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 16, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 17, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 18, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 19, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 20, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
{position: 21, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
{position: 22, name: 'Helium', weight: 4.0026, symbol: 'He'},
{position: 23, name: 'Lithium', weight: 6.941, symbol: 'Li'},
{position: 24, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 25, name: 'Boron', weight: 10.811, symbol: 'B'},
{position: 26, name: 'Carbon', weight: 12.0107, symbol: 'C'},
{position: 27, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
{position: 28, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
{position: 29, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
{position: 30, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];
app.component.scss
mat-table {
width: 100%;
height: 300px;
max-height: 300px;
overflow: scroll
}
mat-row.highlightTableColor {
background-color: #e2e2e2 !important;
}
you can check code here. Height of each row can be different depends on data.
One Important thing - I can not do it using css only because on some event I need that currently selected row also, I have to send it to B/E. Any help is appreciated. Thanks