react-table/examples/sorting
Jason Clark d2acfc4235
New documentation site built with Docz (#2013)
* size-snapshot created?

* Added docz for documentation site

* Modified .gitignore to get rid of .docz internal stuff

* Update all doc links to point to proper paths with docz

* Removed .docz folder from Git

Co-authored-by: Jason Clark <jason.clark@tcnbroadcasting.com>
2020-03-25 14:29:56 -06:00
..
public fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00
src Remove service workers 2019-11-28 12:25:36 -07:00
.babelrc fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00
.env fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00
.eslintrc fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00
.gitignore New documentation site built with Docz (#2013) 2020-03-25 14:29:56 -06:00
.rescriptsrc.js fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00
package.json Upgrade examples to use "latest" 2019-12-03 12:55:01 -07:00
README.md New documentation site built with Docz (#2013) 2020-03-25 14:29:56 -06:00
yarn.lock fix(use-pagination): better controlled/manual use-pagination 2019-08-06 22:32:17 -06:00

Sorting

Automatic sorting can be accomplished by using the useSortBy plugin hook.

Guide

Start by importing the hook from react-table:

-import { useTable } from 'react-table'
+import { useTable, useSortBy } from 'react-table'

Next, add the useSortBy hook to your useTable hook and add the necessary UI pieces we need to make sorting work:

function MyTable() {
  const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable(
    {
      data,
      columns,
    },
+   useSortBy
  )

  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map(headerGroup => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map(column => (
-             <th {...column.getHeaderProps()}>
+             <th {...column.getHeaderProps(column.getSortByToggleProps())}>
                {column.render('Header')}
+               <span>
+                 {column.isSorted ? (column.isSortedDesc ? ' 🔽' : ' 🔼') : ''}
+               </span>
              </th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()}>
        {rows.map(
          (row, i) => {
            prepareRow(row);
            return (
              <tr {...row.getRowProps()}>
                {row.cells.map(cell => {
                  return <td {...cell.getCellProps()}>{cell.render('Cell')}</td>
                })}
              </tr>
            )}
        )}
      </tbody>
    </table>
  )
}

By default, the sorting will be alphanumeric. This can be changed in your column object. Other options include basic and datetime. Note that if you're planning on sorting numbers between 0 and 1, basic sorting will be more accurate.

More information can be found in the API Docs

const columns = React.useMemo(
    () => [
      {
        Header: 'Name',
        columns: [
          {
            Header: 'First Name',
            accessor: 'firstName',
          },
          {
            Header: 'Last Name',
            accessor: 'lastName',
          },
        ],
      },
      {
        Header: 'Info',
        columns: [
          {
            Header: 'Age',
            accessor: 'age',
+           sortType: 'basic'
          },
          {
            Header: 'Visits',
            accessor: 'visits',
+           sortType: 'basic'
          },
          {
            Header: 'Status',
            accessor: 'status',
          },
          {
            Header: 'Profile Progress',
            accessor: 'progress',
+           sortType: 'basic'
          },
        ],
      },
    ],
    []
  )```