英文:
Scroll to an selected item in scrollable mat-table angular
问题
我有一个带有最大高度的角度材质表,所以你可以在其中滚动。还有一个带有可单击的点的雷达。雷达在做什么并不重要,只有点很重要。
你可以单击材质表中的项目,然后特定的点会变红,或者你可以单击一个点,然后材质表中的项目会有背景颜色。
如果单击了一个点,但在材质表中对应的项目不可见,它应该滚动到材质表中的特定项目。
材质表代码:
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8" *ngIf="!isLoading">
<ng-container matColumnDef="name">
<mat-cell
*matCellDef="let law" (click)="onSelect(law)"
[ngClass]="{'highlight': selectedRowIndex == law.id}"
><b>{{law.name}}</b>
</mat-cell>
</ng-container>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
另外,被选中的项目会获得“highlight”类。
我使用以下代码来获取元素:
let element = this.elem.nativeElement.querySelectorAll('.highlight');
所以我尝试用以下方式滚动到它:
element.scrollIntoView({block: 'center'});
和
element.prototype.scrollIntoView();
但这两种方法都不起作用。
你有什么办法可以滚动到材质表中的特定项目吗?
英文:
So I have an angular mat-table with a max-height, so you can scroll in it.
There is also a radar with dots that you can click. It doesn't matter what the radar is doing, only the dots are important.
You can either click on an item in the mat-table and the specific dot gets red, or you can click on a dot and the item in the mat-table gets a background-color.
If a dot gets clicked and the corresponding item in mat-table isn't visible, it should scroll to that specific item in the mat-table.
Code of mat-table:
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8" *ngIf="!isLoading">
<ng-container matColumnDef="name">
<mat-cell
*matCellDef="let law" (click)="onSelect(law)"
[ngClass]="{'highlight': selectedRowIndex == law.id}"
><b>{{law.name}}</b>
</mat-cell>
</ng-container>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Also, the item which is selected gets the class highlight.
I'm getting the element with:
let element = this.elem.nativeElement.querySelectorAll('.highlight');
So I tried to scroll to it with:
element.scrollIntoView({block: 'center'});
and
element.prototype.scrollIntoView();
but neither of this is working.
Any ideas on how I can scroll to the specific item in the mat-table?
答案1
得分: 0
很难说出你的代码问题在哪,除非你提供一个关于你的问题的 StackBlitz 示例。
无论如何,滚动到特定行应该很容易。
在你的模板中,在行元素上定义一个id。你也可以为整行添加样式以突出显示所选行:
<tr mat-row
*matRowDef="let row; columns: displayedColumns;" [id]="row.position"
[ngClass]="{'selected-row': row.position === selectedRowId}">
</tr>
在你的 TypeScript 文件中,查询类型为 MatRow
的 @ViewChildren
:
@ViewChildren(MatRow, {read: ElementRef}) rows!: QueryList<ElementRef<HTMLTableRowElement>>;
这将找到所有带有 MatRow
指令的元素,并读取它们的 ElementRef
,允许你访问它们的 nativeElement
。
然后,你可以通过查找具有相应id的元素并调用 scrollIntoView()
来滚动到特定的行:
private scrollToIndex(id: number): void {
this.selectedRowId = id;
let elem = this.rows.find(row => row.nativeElement.id === id.toString());
elem?.nativeElement.scrollIntoView({block: 'center', behavior: 'smooth'});
}
这里是一个工作的 StackBlitz。我在一个带有 overflow-y: auto
和 max-width
的 div
中包装了表格,但这并不是必需的 - 它只是为了强制在高分辨率显示器上显示滚动条。
(注意:由于您要求只翻译代码部分,因此已省略代码中的HTML和TypeScript标签。)
英文:
It's hard to say what's wrong with your code without you providing a stackblitz example of your issue.
Anyway, scrolling to a specific row should be easy.
In your template define an id on the row element. You can also style the whole row to highlight the selected row:
<tr mat-row
*matRowDef="let row; columns: displayedColumns;" [id]="row.position"
[ngClass]="{'selected-row': row.position === selectedRowId}">
</tr>
In your typescript file, query the @ViewChildren
of type MatRow
:
@ViewChildren(MatRow, {read: ElementRef}) rows!: QueryList<ElementRef<HTMLTableRowElement>>;
This will find all elements with the MatRow
directive and read their ElementRef
, which allows you accessing the nativeElement
.
Then you can scroll into specific row by looking up proper element based on it's id and calling scrollIntoView()
on it:
private scrollToIndex(id: number): void {
this.selectedRowId = id;
let elem = this.rows.find(row => row.nativeElement.id === id.toString());
elem?.nativeElement.scrollIntoView({block: 'center', behavior: 'smooth'});
}
Here's a working stackblitz. I've wrapped the table in a div with overflow-y: auto
and max-width
, but it's not necessary for it to work - it was only to force the scrollbar visibility on higher resolution displays.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论