简体   繁体   English

如何以编程方式重置React表中的滚动

[英]How to programmatically reset scroll in react-table

I want to be able to reset the scroll position to 0 when loading new data. 我希望能够在加载新数据时将滚动位置重置为0。 As of now when new data is passed as props, the scroll position remains. 截至目前,当新数据作为prop传递时,滚动位置仍然保留。 This causes a bit confusion for the users. 这给用户造成了一些混乱。

EDIT: The scroll is in the React-Table component, and not the page scroll. 编辑:滚动是在React-Table组件中,而不是页面滚动中。

yes you can reset programatically reset scroll to top use component did update lifecycle componentDidUpdate() { window.scrollTo(0,o); } 是的,您可以以编程方式重置滚动条,以重置使用率最高的组件,并且确实更新了生命周期componentDidUpdate() { window.scrollTo(0,o); } componentDidUpdate() { window.scrollTo(0,o); }

Found the solution. 找到了解决方案。 I used a perfect scrollbar component which I made a ref for with the passed object of the table, such that I could call this.scroll.scrollbar.scrollTo(0,0) which resets the table scroll. 我使用了一个完美的滚动条组件,该组件对表的传递对象进行了引用,因此可以调用this.scroll.scrollbar.scrollTo(0,0)来重置表滚动。

const PerfectScrollbarBody = (props, table) => (
   <div {...props} className={"rt-tbody"}>
      <Scrollbar ref={node => (table.scroll = node)}>{props.children}</Scrollbar>
   </div>
);

I think this can be worked simply like this. 我认为可以像这样简单地工作。

export function resetScrollInsideTable(indexTable) {
  let tableBody = document.getElementsByClassName('rt-tbody')[indexTable];
  tableBody.scrollTop = 0;
}

Assume you got 2 components of react-table in your DOM. 假设您在DOM中有2个表。 If you want to reset scroll of the first react-table, use this in your fetch data, or your button click query then show data. 如果要重置第一个反应表的滚动,请在获取数据时使用它,或者单击按钮查询然后显示数据。

resetScrollInsideTable(0)

And do the reset for the 2nd react-table 并重置第二个反应表

resetScrollInsideTable(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM