【问题标题】:Dynamically choose number of lines per page in the pagination section在分页部分动态选择每页的行数
【发布时间】:2018-08-30 14:35:26
【问题描述】:

我想在我的 List 组件的每个分页部分中添加更改 perPage 选项值的功能。 我创建了一个基于 @material-ui/core's TablePagination component 的 CustomPagination 组件来允许这样做,但出现了 2 个问题:

  • 显示的范围位于第二页而不是第一页。点击上一页按钮无法访问第一页

  • 如果我更改 perPage 下拉列表的值:没有任何反应,正确调用了 onChangeRowsPerPage 方法,但 setPerPage 函数似乎什么都不做。另一方面,如果我单击下一页,那么表格会正确显示,其中包含与所选选项相对应的行数,这很奇怪。仍然无法访问表格的第一页

下面是我的 PostList.js 组件的代码:

import { withStyles } from '@material-ui/core/styles';
import React from 'react';
import {
  Datagrid,
  List,
  Responsive,
  ShowButton,
  SimpleList,
  TextField
} from 'react-admin';

import Button from '@material-ui/core/Button';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import ChevronRight from '@material-ui/icons/ChevronRight';
import FlatButton from 'material-ui/FlatButton';
import { Toolbar, ToolbarGroup } from 'material-ui/Toolbar';
import TablePagination from '@material-ui/core/TablePagination';

const CustomPagination = ({ page, perPage, total, setPage, setPerPage 
}) => {
const nbPages = Math.ceil(total / perPage) || 1;

const handleChangeRowsPerPage = event => {
    perPage = event.target.value;
    setPerPage(perPage);
};

const handleChangePage = (event, page) => {
    page < nbPages && page > 0 && setPage(page);
};

return (
    nbPages > 1 && (
        <TablePagination
            component="span"
            count={total}
            rowsPerPage={perPage}
            page={page}
            backIconButtonProps={{
                'aria-label': 'Previous Page'
            }}
            nextIconButtonProps={{
                'aria-label': 'Next Page'
            }}
            onChangePage={handleChangePage}
            labelRowsPerPage="Lignes par page"
            rowsPerPageOptions={[2, 5, 10, 50, 100]}
            onChangeRowsPerPage={handleChangeRowsPerPage}
        />
      )
    );
};

const styles = theme => ({
  title: {
    maxWidth: '20em',
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap'
  }
});

const PostList = withStyles(styles)(({ classes, ...props }) => (
  <List
    {...props}
    sort={{ field: 'published_at', order: 'DESC' }}
    perPage={2}
    pagination={<CustomPagination />}
  >
    <Responsive
        small={
            <SimpleList
                linkType="show"
                primaryText={record => record.title}
            />
        }
        medium={
            <Datagrid>
                <TextField source="id" />
                <TextField source="title" cellClassName={classes.title} />
                <ShowButton />
            </Datagrid>
        }
    />
  </List>
));

export default PostList;

还有codesandbox here

【问题讨论】:

    标签: javascript react-admin


    【解决方案1】:

    这将在即将推出的2.3 版本中默认提供。有关详细信息,请参阅此拉取请求:https://github.com/marmelab/react-admin/pull/2173

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多