滚动到可滚动的 Angular mat-table 中的选定项目。

huangapple go评论55阅读模式
英文:

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.

Mat-table

Code of mat-table:

&lt;mat-table #table [dataSource]=&quot;dataSource&quot; class=&quot;mat-elevation-z8&quot; *ngIf=&quot;!isLoading&quot;&gt;
              &lt;ng-container matColumnDef=&quot;name&quot;&gt;
                &lt;mat-cell 
                  *matCellDef=&quot;let law&quot; (click)=&quot;onSelect(law)&quot;
                  [ngClass]=&quot;{&#39;highlight&#39;: selectedRowIndex == law.id}&quot;
                  &gt;&lt;b&gt;{{law.name}}&lt;/b&gt;
                &lt;/mat-cell&gt;
              &lt;/ng-container&gt;
              &lt;mat-row *matRowDef=&quot;let row; columns: displayedColumns;&quot;&gt;&lt;/mat-row&gt;
&lt;/mat-table&gt;

Also, the item which is selected gets the class highlight.

I'm getting the element with:

let element = this.elem.nativeElement.querySelectorAll(&#39;.highlight&#39;);

So I tried to scroll to it with:
element.scrollIntoView({block: &#39;center&#39;});
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: automax-widthdiv 中包装了表格,但这并不是必需的 - 它只是为了强制在高分辨率显示器上显示滚动条。

(注意:由于您要求只翻译代码部分,因此已省略代码中的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:

&lt;tr mat-row 
    *matRowDef=&quot;let row; columns: displayedColumns;&quot; [id]=&quot;row.position&quot;
    [ngClass]=&quot;{&#39;selected-row&#39;: row.position === selectedRowId}&quot;&gt;
&lt;/tr&gt;

In your typescript file, query the @ViewChildren of type MatRow:

@ViewChildren(MatRow, {read: ElementRef}) rows!: QueryList&lt;ElementRef&lt;HTMLTableRowElement&gt;&gt;;

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 =&gt; row.nativeElement.id === id.toString());

    elem?.nativeElement.scrollIntoView({block: &#39;center&#39;, behavior: &#39;smooth&#39;});
}

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.

huangapple
  • 本文由 发表于 2023年3月7日 17:57:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定