mui / material-ui

Material UI: Comprehensive React component library that implements Google's Material Design. Free forever.
https://mui.com/material-ui/
MIT License
93.74k stars 32.24k forks source link

Table scroll to top of new page #21793

Closed cl4k closed 4 years ago

cl4k commented 4 years ago

Hi Guys, first of all thanks for material-UI, it's really useful! I'm stuck on this since yesterday ... I don't found the way to be on the top of new page when you click on next, I always stay at the bottom whatever I do.

I already check on StackOverflow and GitHub, I found this issue which seems to be close: #9186 And I can't find anything on your documentation (I think we should add it haha )

I supposed using ref and callback is the right way, I already try to implement it. However, I'm always stuck at the having the last element and I can't scrollTop to the one of the page

I based my code on Custom pagination actions

Here is an example of my code

function DisplayList(props) {
   var rows = [];
   const data = props.data;
   const tableRef = React.createRef();
   const searchData = props.searchData;
   const setHoverAddress = props.setHoverAddress;

   const classes = useStyles1();

   const [page, setPage] = useState(0);
   const [rowsPerPage, setRowsPerPage] = useState(5);

   const handleChangePage = (event, newPage) => {
       setPage(newPage);   
       if(tableRef.current) {tableRef.current.scrollTop = 0;}
   };

   const handleChangeRowsPerPage = (event) => {
       setRowsPerPage(parseInt(event.target.value, 10));
       setPage(0);
   };

   data.map((result, index) => { // WARNING : slice here which limits the number of results: .slice(0, 5)
       const volulme = Math.round(result.volulme);
       const volulme2 = Math.round(result.volulme2);

       rows.push(
       <div id={index}>
           <ListItem 
           alignItems="flex-start"
           onMouseEnter={e => {
               console.log(index);
           }}
           >
               <Grid container direction="row" spacing={1}>
                   <Grid item xs={5}>

                   {/* <Stage width={150} height={150}>
                       <Layer>
                       <Shape
                           sceneFunc={(context, shape) => {
                           context.beginPath();
                           context.moveTo(20, 10);
                           context.lineTo(120, 80);
                           context.lineTo(120, 140);
                           context.lineTo(22, 140);
                           context.closePath();
                           // (!) Konva specific method, it is very important
                           context.fillStrokeShape(shape);
                           }}
                           fill="#00D2FF"
                           stroke="black"
                           strokeWidth={2}
                       />
                       </Layer>
                   </Stage> */}

                   </Grid>
                   <Grid item xs={7}>
                   <ListItemText
                   primary={
                   }
                   secondary={
                       <React.Fragment>
                       <Typography
                           component="span"
                           variant="body2"
                           display="inline"
                           color="textPrimary"
                       >
                           Solid2 : {volulme2}
                       </Typography>
                       </React.Fragment>
                   }
                   />
                   <ListItemText
                   secondary={
                       <React.Fragment>
                       <Typography
                           component="span"
                           variant="body2"
                           display="inline"
                           color="textPrimary"
                       >
                           Solid : {volulme}
                       </Typography>
                       </React.Fragment>
                   }
                   />
                   <FormControlLabel
                   control={
                       <Checkbox icon={<FavoriteBorder />} 
                       checkedIcon={<Favorite />} 
                       color="primary"
                       onClick={(e) => {
                           if (e.target.checked) {
                               addFavourite(parc_id, 1)
                           } else {
                               removeFavourite(parc_id, 1)
                           }
                       }}
                       name="checkedH" />
                   }
                       label="Enregistrer"
                   />
                   </Grid>
               </Grid>
           </ListItem>
       </div>
       )
   })

   return (
           <Table className={classes.table} aria-label="custom pagination table">
               <TableBody>
               {(rowsPerPage > 0
                   ? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
                   : rows
               ).map((row) => (
                   <TableRow key={index}>
                       <TableCell component="th" scope="row">
                         <div ref={tableRef}>
                           {row}
                          </div>
                       </TableCell>
                   </TableRow>
               ))}
               </TableBody>
               <TableFooter>
                   <TableRow>
                       <TablePagination
                       rowsPerPageOptions={[5, 10, 25, { label: 'All', value: -1 }]}
                       colSpan={3}
                       count={rows.length}
                       rowsPerPage={rowsPerPage}
                       page={page}
                       SelectProps={{
                           inputProps: { 'aria-label': 'rows per page' },
                           native: true,
                       }}
                       onChangePage={handleChangePage}
                       onChangeRowsPerPage={handleChangeRowsPerPage}
                       ActionsComponent={TablePaginationActions}
                       />
                   </TableRow>
               </TableFooter>
           </Table>
   )
}

Thank you for your time and your answer! Have a nice day Louis

support[bot] commented 4 years ago

👋 Thanks for using Material-UI!

We use GitHub issues exclusively as a bug and feature requests tracker, however, this issue appears to be a support request.

For support, please check out https://material-ui.com/getting-started/support/. Thanks!

If you have a question on StackOverflow, you are welcome to link to it here, it might help others. If your issue is subsequently confirmed as a bug, and the report follows the issue template, it can be reopened.

ducthien1490 commented 4 years ago

Experienced same issue when using DateTimePicker When pop-up opens , the page automatically scroll to top

oliviertassinari commented 4 years ago

@ducthien1490 If you have a reprodution with the latest versions (alpha) please open a new issue about it with the link to the live reproduction, it will help, thanks.

ducthien1490 commented 4 years ago

@oliviertassinari : I accidentially fixed it by set overflow: auto to body. I'll upgrade to alpha version soon and let you know if there're any further issue Thanks!