React-table 和跳转到第一页/最后一页。

huangapple go评论81阅读模式
英文:

React-table and go to first/last page

问题

我正在尝试通过重写PreviousComponentNextComponent来向React-Table添加“转到”第一页/最后一页按钮,但似乎这两个组件都与前一页/后一页的功能行为相关联。

是否有其他方法将这两个按钮添加到默认分页组件中?

英文:

I'm trying to add "go to" first/last page buttons to the React-Table by overwriting PreviousComponent and NextComponent but it looks like both components are connected to the previous/next page functionality behaviours.

Is there any other way to add those two buttons to the default pagination component?

答案1

得分: 3

以下是翻译好的代码部分:

import React from "react";
import styled from "styled-components";
import { useTable, usePagination } from "react-table";

import makeData from "./makeData";

const Styles = styled.div`
  padding: 1rem;

  table {
    border-spacing: 0;
    border: 1px solid black;

    tr {
      :last-child {
        td {
          border-bottom: 0;
        }
      }
    }

    th,
    td {
      margin: 0;
      padding: 0.5rem;
      border-bottom: 1px solid black;
      border-right: 1px solid black;

      :last-child {
        border-right: 0;
      }
    }
  }

  .pagination {
    padding: 0.5rem;
  }
`;

function Table({ columns, data }) {
  // Use the state and functions returned from useTable to build your UI
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    prepareRow,
    page, // Instead of using 'rows', we'll use page,
    // which has only the rows for the active page

    // The rest of these things are super handy, too ;)
    canPreviousPage,
    canNextPage,
    pageOptions,
    pageCount,
    gotoPage,
    nextPage,
    previousPage,
    setPageSize,
    state: { pageIndex, pageSize }
  } = useTable(
    {
      columns,
      data,
      initialState: { pageIndex: 2 }
    },
    usePagination
  );

  // Render the UI for your table
  return (
    <>
      <table {...getTableProps()}>
        <thead>
          {headerGroups.map(headerGroup => (
            <tr {...headerGroup.getHeaderGroupProps()}>
              {headerGroup.headers.map(column => (
                <th {...column.getHeaderProps()}>{column.render("Header")}</th>
              ))}
            </tr>
          ))}
        </thead>
        <tbody {...getTableBodyProps()}>
          {page.map((row, i) => {
            prepareRow(row);
            return (
              <tr {...row.getRowProps()}>
                {row.cells.map(cell => {
                  return (
                    <td {...cell.getCellProps()}>{cell.render("Cell")}</td>
                  );
                })}
              </tr>
            );
          })}
        </tbody>
      </table>
      {/* 
        Pagination can be built however you'd like. 
        This is just a very basic UI implementation:
      */}
      <div className="pagination">
        <button onClick={() => gotoPage(0)} disabled={!canPreviousPage}>
          {"First"}
        </button>{" "}
        <button onClick={() => previousPage()} disabled={!canPreviousPage}>
          {"<"}
        </button>{" "}
        <button onClick={() => nextPage()} disabled={!canNextPage}>
          {">"}
        </button>{" "}
        <button onClick={() => gotoPage(pageCount - 1)} disabled={!canNextPage}>
          {"Last"}
        </button>{" "}
        <span>
          Page{" "}
          <strong>
            {pageIndex + 1} of {pageOptions.length}
          </strong>{" "}
        </span>
        <span>
          | Go to page:{" "}
          <input
            type="number"
            defaultValue={pageIndex + 1}
            onChange={e => {
              const page = e.target.value ? Number(e.target.value) - 1 : 0;
              gotoPage(page);
            }}
            style={{ width: "100px" }}
          />
        </span>{" "}
        <select
          value={pageSize}
          onChange={e => {
            setPageSize(Number(e.target.value));
          }}
        >
          {[10, 20, 30, 40, 50].map(pageSize => (
            <option key={pageSize} value={pageSize}>
              Show {pageSize}
            </option>
          ))}
        </select>
      </div>
    </>
  );
}

function App() {
  const columns = React.useMemo(
    () => [
      {
        Header: "Name",
        columns: [
          {
            Header: "First Name",
            accessor: "firstName"
          },
          {
            Header: "Last Name",
            accessor: "lastName"
          }
        ]
      },
      {
        Header: "Info",
        columns: [
          {
            Header: "Age",
            accessor: "age"
          },
          {
            Header: "Visits",
            accessor: "visits"
          },
          {
            Header: "Status",
            accessor: "status"
          },
          {
            Header: "Profile Progress",
            accessor: "progress"
          }
        ]
      }
    ],
    []
  );

  const data = React.useMemo(() => makeData(100000), []);

  return (
    <Styles>
      <Table columns={columns} data={data} />
    </Styles>
  );
}

export default App;

如果需要进一步的翻译或有其他问题,请告诉我。

英文:

You can customise the text as required. I have mocked the data, you can pass the actual data as required

See the code

App.js

import React from &quot;react&quot;;
import styled from &quot;styled-components&quot;;
import { useTable, usePagination } from &quot;react-table&quot;;
import makeData from &quot;./makeData&quot;;
const Styles = styled.div`
padding: 1rem;
table {
border-spacing: 0;
border: 1px solid black;
tr {
:last-child {
td {
border-bottom: 0;
}
}
}
th,
td {
margin: 0;
padding: 0.5rem;
border-bottom: 1px solid black;
border-right: 1px solid black;
:last-child {
border-right: 0;
}
}
}
.pagination {
padding: 0.5rem;
}
`;
function Table({ columns, data }) {
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
getTableBodyProps,
headerGroups,
prepareRow,
page, // Instead of using &#39;rows&#39;, we&#39;ll use page,
// which has only the rows for the active page
// The rest of these things are super handy, too ;)
canPreviousPage,
canNextPage,
pageOptions,
pageCount,
gotoPage,
nextPage,
previousPage,
setPageSize,
state: { pageIndex, pageSize }
} = useTable(
{
columns,
data,
initialState: { pageIndex: 2 }
},
usePagination
);
// Render the UI for your table
return (
&lt;&gt;
&lt;table {...getTableProps()}&gt;
&lt;thead&gt;
{headerGroups.map(headerGroup =&gt; (
&lt;tr {...headerGroup.getHeaderGroupProps()}&gt;
{headerGroup.headers.map(column =&gt; (
&lt;th {...column.getHeaderProps()}&gt;{column.render(&quot;Header&quot;)}&lt;/th&gt;
))}
&lt;/tr&gt;
))}
&lt;/thead&gt;
&lt;tbody {...getTableBodyProps()}&gt;
{page.map((row, i) =&gt; {
prepareRow(row);
return (
&lt;tr {...row.getRowProps()}&gt;
{row.cells.map(cell =&gt; {
return (
&lt;td {...cell.getCellProps()}&gt;{cell.render(&quot;Cell&quot;)}&lt;/td&gt;
);
})}
&lt;/tr&gt;
);
})}
&lt;/tbody&gt;
&lt;/table&gt;
{/* 
Pagination can be built however you&#39;d like. 
This is just a very basic UI implementation:
*/}
&lt;div className=&quot;pagination&quot;&gt;
&lt;button onClick={() =&gt; gotoPage(0)} disabled={!canPreviousPage}&gt;
{&quot;First&quot;}
&lt;/button&gt;{&quot; &quot;}
&lt;button onClick={() =&gt; previousPage()} disabled={!canPreviousPage}&gt;
{&quot;&lt;&quot;}
&lt;/button&gt;{&quot; &quot;}
&lt;button onClick={() =&gt; nextPage()} disabled={!canNextPage}&gt;
{&quot;&gt;&quot;}
&lt;/button&gt;{&quot; &quot;}
&lt;button onClick={() =&gt; gotoPage(pageCount - 1)} disabled={!canNextPage}&gt;
{&quot;Last&quot;}
&lt;/button&gt;{&quot; &quot;}
&lt;span&gt;
Page{&quot; &quot;}
&lt;strong&gt;
{pageIndex + 1} of {pageOptions.length}
&lt;/strong&gt;{&quot; &quot;}
&lt;/span&gt;
&lt;span&gt;
| Go to page:{&quot; &quot;}
&lt;input
type=&quot;number&quot;
defaultValue={pageIndex + 1}
onChange={e =&gt; {
const page = e.target.value ? Number(e.target.value) - 1 : 0;
gotoPage(page);
}}
style={{ width: &quot;100px&quot; }}
/&gt;
&lt;/span&gt;{&quot; &quot;}
&lt;select
value={pageSize}
onChange={e =&gt; {
setPageSize(Number(e.target.value));
}}
&gt;
{[10, 20, 30, 40, 50].map(pageSize =&gt; (
&lt;option key={pageSize} value={pageSize}&gt;
Show {pageSize}
&lt;/option&gt;
))}
&lt;/select&gt;
&lt;/div&gt;
&lt;/&gt;
);
}
function App() {
const columns = React.useMemo(
() =&gt; [
{
Header: &quot;Name&quot;,
columns: [
{
Header: &quot;First Name&quot;,
accessor: &quot;firstName&quot;
},
{
Header: &quot;Last Name&quot;,
accessor: &quot;lastName&quot;
}
]
},
{
Header: &quot;Info&quot;,
columns: [
{
Header: &quot;Age&quot;,
accessor: &quot;age&quot;
},
{
Header: &quot;Visits&quot;,
accessor: &quot;visits&quot;
},
{
Header: &quot;Status&quot;,
accessor: &quot;status&quot;
},
{
Header: &quot;Profile Progress&quot;,
accessor: &quot;progress&quot;
}
]
}
],
[]
);
const data = React.useMemo(() =&gt; makeData(100000), []);
return (
&lt;Styles&gt;
&lt;Table columns={columns} data={data} /&gt;
&lt;/Styles&gt;
);
}
export default App;

Working codesandbox

答案2

得分: 0

我已经覆盖了PaginationComponent,将默认代码修改为自定义的PaginationComponent,其中我添加了以下内容:

<div className="-first">
    <button
        disabled={!canPrevious}
        onClick={() => {
            if (!canPrevious) return
            this.changePage(0)
        }}
    > First </button>
</div>


<div className="-last">
    <button
        onClick={() => {
            if (!canNext) return
            this.changePage(pages)
        }}
        disabled={!canNext}
    > Last </button>
</div>

请注意,这是对你提供的代码的翻译。

英文:

For v6 I have ended up overwriting PaginationComponent

&lt;ReactTable 
....
PaginationComponent={PaginationComponent}
....
&gt;

with modified default code as custom PaginationComponent where I have added

            &lt;div className=&quot;-first&quot;&gt;
&lt;button
disabled={!canPrevious}
onClick={() =&gt; {
if (!canPrevious) return
this.changePage(0)
}}
&gt; First &lt;/button&gt;
&lt;/div&gt;

and

            &lt;div className=&quot;-last&quot;&gt;
&lt;button
onClick={() =&gt; {
if (!canNext) return
this.changePage(pages)
}}
disabled={!canNext}
&gt; Last &lt;/button&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2020年1月3日 22:46:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定