admin管理员组文章数量:1026664
I have a table with a body size fixed as explained in the Material UI. I would like to listen to the scroll event of the table in order to load more rows.
Which is the best approach to listen to this scroll?
I have a table with a body size fixed as explained in the Material UI. I would like to listen to the scroll event of the table in order to load more rows.
Which is the best approach to listen to this scroll?
Share Improve this question asked Nov 28, 2016 at 19:39 juan garciajuan garcia 1,5263 gold badges27 silver badges59 bronze badges 02 Answers
Reset to default 2Tried the first solution which didn't work with my implementation.
I got this working using [email protected] and [email protected]:
Create a table element and set the ref to a name:
<Table>
...
<TableBody ref="table-body">
...
</TableBody>
</Table>
In ponentDidMount, find the DOMNode using ReactDOM.findDOMNode:
ponentDidMount() {
let tableBodyNode = ReactDOM.findDOMNode(this.refs["table-body"]).parentNode.parentNode;
tableBodyNode.addEventListener('scroll', (e) => {
console.log(e);
});
}
This will give you the scroll event of the table.
This is not straightforward and perhaps material-ui's Table is not the best suited for your requirements. You may want to take a look at some infinite-scrolling ponents, like react-infinite or react-list.
That being said, I experimented a bit and came up with this method of intercepting the scroll event in material-ui's TableBody.
First, capture a reference to the scrollable div that your table's body is contained in (its grandparent element in this case):
<Table height={200}>
...
<TableBody
ref={ref => { this.viewport = ReactDOM.findDOMNode(ref).parentNode.parentNode; } }>
...
then, in ponentDidMount(), add an event listener for the onscroll event to the scrollable div:
ponentDidMount() {
this.viewport.addEventListener('scroll', (e) => {
console.log(e);
});
}
I have a table with a body size fixed as explained in the Material UI. I would like to listen to the scroll event of the table in order to load more rows.
Which is the best approach to listen to this scroll?
I have a table with a body size fixed as explained in the Material UI. I would like to listen to the scroll event of the table in order to load more rows.
Which is the best approach to listen to this scroll?
Share Improve this question asked Nov 28, 2016 at 19:39 juan garciajuan garcia 1,5263 gold badges27 silver badges59 bronze badges 02 Answers
Reset to default 2Tried the first solution which didn't work with my implementation.
I got this working using [email protected] and [email protected]:
Create a table element and set the ref to a name:
<Table>
...
<TableBody ref="table-body">
...
</TableBody>
</Table>
In ponentDidMount, find the DOMNode using ReactDOM.findDOMNode:
ponentDidMount() {
let tableBodyNode = ReactDOM.findDOMNode(this.refs["table-body"]).parentNode.parentNode;
tableBodyNode.addEventListener('scroll', (e) => {
console.log(e);
});
}
This will give you the scroll event of the table.
This is not straightforward and perhaps material-ui's Table is not the best suited for your requirements. You may want to take a look at some infinite-scrolling ponents, like react-infinite or react-list.
That being said, I experimented a bit and came up with this method of intercepting the scroll event in material-ui's TableBody.
First, capture a reference to the scrollable div that your table's body is contained in (its grandparent element in this case):
<Table height={200}>
...
<TableBody
ref={ref => { this.viewport = ReactDOM.findDOMNode(ref).parentNode.parentNode; } }>
...
then, in ponentDidMount(), add an event listener for the onscroll event to the scrollable div:
ponentDidMount() {
this.viewport.addEventListener('scroll', (e) => {
console.log(e);
});
}
本文标签: javascriptMaterial UI listen scroll on table (React)Stack Overflow
版权声明:本文标题:javascript - Material UI listen scroll on table (React) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745650102a2161264.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论