简体   繁体   English

Material-Table 中的自定义分页

[英]Custom Pagination in Material-Table

I want to override the default pagination in Material-Table to look different, but keep the same default elements there.我想覆盖 Material-Table 中的默认分页以使其看起来不同,但保留相同的默认元素。 My override allows me to move between pages in the table, but I cannot change how many rows appear in the table.我的覆盖允许我在表格中的页面之间移动,但我无法更改表格中出现的行数。 I cannot skip to the last or first page either.我也不能跳到最后一页或第一页。 I want to keep the exact same options as what is there by default, but change how they look.我想保留与默认选项完全相同的选项,但要更改它们的外观。

All of my Icon, Grid, Typography, etc, imports are @material-ui/core/ or @material-ui/icons/我所有的图标、网格、排版等,导入都是@material-ui/core/@material-ui/icons/

    function IntakeList() {
        const CustomPaginationComponent = (props) => {
            const { page, rowsPerPage, count, onChangePage } = props;
            let from = rowsPerPage * page + 1;
            let to = rowsPerPage * (page + 1);
            if (to > count) {
                to = count;
            }
            return (
                <td>
                    <Grid container alignItems="center" style={{ paddingTop: 8 }}>
                        <Grid item>
                            <IconButton disabled={page === 0} onClick={(e) => onChangePage(e, page - 1)}>
                                <SkipPreviousIcon fontSize="small" color={page === 0 ? "disabled" : "primary"} />
                                <Typography>Prev</Typography>
                            </IconButton>
                        </Grid>
                        <Grid item>
                            <Typography variant="caption" style={{ color: "black" }}>
                                {from}-{to} of {count}
                            </Typography>
                        </Grid>
                        <Grid item>
                            <IconButton disabled={to >= count} onClick={(e) => onChangePage(e, page + 1)}>
                                <Typography>Next</Typography>
                                <SkipNextIcon fontSize="small" color={to < count ? "primary" : "disabled"} />
                            </IconButton>
                        </Grid>
                    </Grid>
                </td>
            );
        };

        return (
            <MaterialTable
                title="Title"
                data={data}
                columns={columns}
                options={{
                    pageSize: 10,
                    pageSizeOptions: [10, 15, 25, 50, 100],
    
                }}
                components={{
                    Pagination: (props) => {
                       return <CustomPaginationComponent {...props} />;
                    },
                }}
            />
        );
    }

What you have done so far is right.你到目前为止所做的都是对的。 You just simply have to code for the "skip to last page", "skip to first page" and "select row options" just like you did for "next" and "previous" page.您只需为“跳到最后一页”、“跳到第一页”和“选择行选项”进行编码,就像为“下一页”和“上一页”所做的一样。

https://codesandbox.io/s/solution-q1cmer?file=/src/App.js https://codesandbox.io/s/solution-q1cmer?file=/src/App.js

When you console log the props of pagination you will get this当你控制台记录分页的道具时,你会得到这个

在此处输入图像描述

Using the rowsPerPageOptions , onChangeRowsPerPage and some of the code you have coded I was able to code the number of rows per page.使用rowsPerPageOptionsonChangeRowsPerPage和您编写的一些代码,我能够编写每页的行数。

To be honest I don't think you have to code to next page and previous page functionality.老实说,我认为您不必编写next pageprevious page功能的代码。 It's already in props.它已经在道具中了。 You simply has to declare them in the right place.您只需在正确的位置声明它们。

Happy Coding快乐编码

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

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