简体   繁体   English

反应表动态页面大小,但有大小限制和分页

[英]React table dynamic page size but with size limit and pagination

I am using React Table and i need to set the table rows dynamically depending on the length of my data.我正在使用 React Table,我需要根据数据的长度动态设置表格行。 this is what i got:这就是我得到的:

let pgSize = (data.length > 10) ? 5 : data.length;


<ReactTable
    data={data} 
    PaginationComponent={Pagination}
    columns={[
        {
            columns: [
            //column defs
            ]
        }
    ]}
    defaultPageSize={10}
    pageSize={pgSize}
    className="-striped -highlight"
/>

i need the rows to be dynamic but if i set the pagesize to the length of the data.我需要行是动态的,但如果我将 pagesize 设置为数据的长度。 the pagination gets removed and this would be a problem if i have 100 rows of data.分页被删除,如果我有 100 行数据,这将是一个问题。 i need a maximum of 10 as the default page size.我需要最多 10 个作为默认页面大小。 i cant seem to get the logic of doing this.我似乎无法理解这样做的逻辑。

Thanks for the help!谢谢您的帮助!

Nathan's answer works just fine, but there's no need to dynamically calculate a pageSize to solve this. Nathan 的回答工作得很好,但没有必要动态计算 pageSize 来解决这个问题。 The simpler method is to define the minRows prop on the react-table component.更简单的方法是在 react-table 组件上定义 minRows 属性。 By default it is undefined, and react-table will add as many empty padding rows to fill your page as it needs to.默认情况下它是未定义的,并且 react-table 会根据需要添加尽可能多的空填充行来填充您的页面。 Defining this limits the number of padding rows created, so you can set it to either 0 or 1 to achieve what you're after, depending on if you want a padding row rendered or not when there are no rows to display.定义它会限制创建的填充行的数量,因此您可以将其设置为 0 或 1 以实现您所追求的目标,具体取决于您是否希望在没有行显示时呈现填充行。

<ReactTable minRows={1} columns={columns} data={data} />

Check out React-Table's sample table .查看React-Table 的示例表

I modified their code a bit to make it work for your situation.我稍微修改了他们的代码以使其适合您的情况。 Copy and paste this code in their editor to see the output.将此代码复制并粘贴到他们的编辑器中以查看输出。

In the constructor, you can change the makeData(20) to whatever amount of data you want.在构造函数中,您可以将makeData(20)更改为您想要的任何数据量。

Notice that I completely removed the defaultPageSize and am handling it through your ternary operator.请注意,我完全删除了 defaultPageSize 并通过您的三元运算符处理它。 Your table would grow up to 10 (default size), but at 11, would shrink back down to only 5 rows.您的表会增长到 10(默认大小),但在 11 时,会缩小到只有 5 行。

 import React from "react"; import { render } from "react-dom"; import { makeData, Logo, Tips } from "./Utils"; // Import React Table import ReactTable from "react-table"; import "react-table/react-table.css"; class App extends React.Component { constructor() { super(); this.state = { data: makeData(20) }; } render() { const { data } = this.state; return ( <div> <ReactTable data={data} pageSize={(data.length > 10) ? 10 : data.length} columns={[ { Header: "Name", columns: [ { Header: "First Name", accessor: "firstName" }, { Header: "Last Name", id: "lastName", accessor: d => d.lastName } ] }, { Header: "Info", columns: [ { Header: "Age", accessor: "age" }, { Header: "Status", accessor: "status" } ] }, { Header: 'Stats', columns: [ { Header: "Visits", accessor: "visits" } ] } ]} className="-striped -highlight" /> <br /> <Tips /> <Logo /> </div> ); } } render(<App />, document.getElementById("root"));

In order to update ReactTable page size after initial render, use the pageSize attribute:为了在初始渲染后更新 ReactTable 页面大小,请使用pageSize属性:

<ReactTable 
  minRows={0}
  data={data} 
  defaultPageSize={initialSize}
  pageSize={updatedSize}
/>

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

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