简体   繁体   English

在添加新行时顶部滚动表

[英]Scroll table at top on adding a new row

I am new to the web Development. 我是web Development.新手web Development. Here I have a table, in that table, 这边有一张桌子

<tbody>
<tr>
<td>
</td>
</tr>
</tbody>

Now, it has a scrollbar as it has a fixed height. 现在,它具有固定高度的滚动条。 Now, I have 50 tr in that table, So, user is scrolling that table . 现在,我在该表中有50 tr ,因此,用户正在滚动该表。 Now, I have a button on click of that one more gets added it gets added in the first position . 现在,我有一个按钮,点击添加了一个按钮,它被添加到了first position SO, user has scrolled the table, and the also gets added. 这样,用户已经滚动了表格,并且也被添加了。 Now, what I want to do is as soon as row gets added , the scroll bar should be at top so that user will immediately see the newly added row. 现在,我要做的是添加行后, scroll bar should be at top以便用户立即看到新添加的行。

is there any way to do this ? 有什么办法吗?

You can use antd library made for react to implement it. 您可以使用antd库做出反应来实现它。

In it specifically, you can use prop validateFieldsAndScroll. 具体来说,您可以使用prop validateFieldsAndScroll。 For example in your case your handleSubmit function will be something like: 例如,在您的情况下,handleSubmit函数将类似于:

 handleSubmit = (e) => { e.preventDefault(); this.props.form.validateFieldsAndScroll((err, values) => { if (!err) { console.log('Received values of form: ', values); } }); } 

You can use the Element.scrollTo function ( https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo ) eg 您可以使用Element.scrollTo函数( https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTo )例如

document.querySelector('tbody').scrollTo(0, 0)

EDIT: 编辑:

The React way would be something like this: React的方式是这样的:

render() {
  return (
    <table>
      <tbody ref={ref=>this.tbody = ref}>
      ...

and then in an onClick handler: 然后在onClick处理程序中:

onClick() {
  // add the new row
  const rowsWithNewRow = ...
  this.setState({rows:rowsWithNewRow})
  this.tbody.scrollTo(0,0)
  ...

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

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