如何在 Material-Table 触发分页时重置垂直滚动条位置?

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

How to reset vertical scroll bar position in Material-Table when pagination is triggered?

问题

在我在Material-Table中向上或向下滚动,然后更改页面时,垂直滚动条不会重置到表格顶部。相反,滚动条仍然保持在更改页面之前的位置。这会导致一些使用表格的人感到困惑。

文档中与滚动条相关的唯一属性是doubleHorizontalScroll,它会强制表格的水平滚动条大小加倍。

是否有一种方法可以在更改页面时重置垂直滚动条?

这是我的表格:

<MaterialTable
    data={data}
    columns={columns}
    icons={tableIcons}
    options={{
        columnsButton: true,
        pageSize: 25,
        pageSizeOptions: [5, 10, 15, 25, 50, 100],
        padding: "dense",
        filtering: true,
        doubleHorizontalScroll: true,
        maxBodyHeight: "45rem",
        searchFieldStyle: {...},
        headerStyle: {...},
        rowStyle: (data, index) => {...},
    }}
    actions={[...]}
    components={{
        Toolbar: (props) => <CustomToolbarComponent {...props} />,
    }}
/>

如何在 Material-Table 触发分页时重置垂直滚动条位置?

英文:

When I scroll up or down in my Material-Table, and then change page, the vertical scroll bar is not reset to the top of the table. Instead the scroll bar remains where it was before changing page. This is causing some confusion with people who are using the table.

The only scroll bar related props in the documentation for Material-Table is doubleHorizontalScroll, which forces the tables horizontal scroll bar to double in size.

Is there any way to reset the vertical scroll bar when the page is changed?

This is my table:

&lt;MaterialTable
    data={data}
    columns={columns}
    icons={tableIcons}
    options={{
        columnsButton: true,
        pageSize: 25,
        pageSizeOptions: [5, 10, 15, 25, 50, 100],
        padding: &quot;dense&quot;,
        filtering: true,
        doubleHorizontalScroll: true,
        maxBodyHeight: &quot;45rem&quot;,
        searchFieldStyle: {...},
        headerStyle: {...},
        rowStyle: (data, index) =&gt; {...},
    }}
    // I tried using this from a previous question but it results in an error.
	// onChangePage={() =&gt; {
		// tableRef.current.tableContainerDiv.current.scroll(0, 0);
	// }}
    actions={[...]}
    components={{
        Toolbar: (props) =&gt; &lt;CustomToolbarComponent {...props} /&gt;,
    }}
/&gt;

如何在 Material-Table 触发分页时重置垂直滚动条位置?

答案1

得分: 0

以下是已翻译的内容:

"The following fixed this issue for me.

import React, { useRef } from &quot;react&quot;;

const tableRef = useRef();
...
&lt;MaterialTable
    tableRef={tableRef}
    data={data}
    columns={columns}
    icons={tableIcons}
    options={{
        columnsButton: true,
        pageSize: 25,
        pageSizeOptions: [5, 10, 15, 25, 50, 100],
        padding: &quot;dense&quot;,
        filtering: true,
        doubleHorizontalScroll: true,
        maxBodyHeight: &quot;45rem&quot;,
        searchFieldStyle: {...},
        headerStyle: {...},
        rowStyle: (data, index) =&gt; {...},
    }}
    onChangePage={() =&gt; {
        tableRef.current.tableContainerDiv.current.scroll(0, 0);
    }}
    actions={[...]}
    components={{
        Toolbar: (props) =&gt; &lt;CustomToolbarComponent {...props} /&gt;,
    }}
/&gt;
`"

<details>
<summary>英文:</summary>

The following fixed this issue for me.

import React, { useRef } from "react";

const tableRef = useRef();
...
<MaterialTable
tableRef={tableRef}
data={data}
columns={columns}
icons={tableIcons}
options={{
columnsButton: true,
pageSize: 25,
pageSizeOptions: [5, 10, 15, 25, 50, 100],
padding: "dense",
filtering: true,
doubleHorizontalScroll: true,
maxBodyHeight: "45rem",
searchFieldStyle: {...},
headerStyle: {...},
rowStyle: (data, index) => {...},
}}
onChangePage={() => {
tableRef.current.tableContainerDiv.current.scroll(0, 0);
}}
actions={[...]}
components={{
Toolbar: (props) => <CustomToolbarComponent {...props} />,
}}
/>


</details>



huangapple
  • 本文由 发表于 2023年3月9日 18:50:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683571.html
匿名

发表评论

匿名网友

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

确定