useSortBy
useSortBy
is the hook that implements row sorting. It also supports multi-sort (keyboard required).
getSortByToggleProps()
, then click any of those elements.shift
while clicking on any of those same elements that have the props from getSortByToggleProps()
attached.toggleSortBy
method located on the instance or each individual column.The following options are supported via the main options object passed to useTable(options)
initialState.sortBy: Array<Object<id: columnId, desc: Bool = true>>
id
key with the corresponding column ID to sort by. An optional desc
key (which defaults to false
) may be set to true
to indicate a descending sorting directionfor that column, otherwise, it will be assumed to be ascending. This information is stored in state since the table is allowed to manipulate the filter through user interaction.manualSortBy: Bool
disableSortBy: Bool
defaultCanSort: Bool
false
true
, all columns will be sortable, regardless if they have a valid accessor
disableMultiSort: Bool
isMultiSortEvent: Function
event
as argument.maxMultiSortColCount: Number
[A, B, C]
and then clicking D
for sorting should result in table sorted by [B, C , D]
disableSortRemove: Bool
disableMultiRemove: Bool
orderByFn: Function
sortTypes: Object<sortKey: sortType>
autoResetSortBy: Boolean
true
true
, the sortBy
state will automatically reset if any of the following conditions are met:data
is changedfalse
The following options are supported on any Column
object passed to the columns
options in useTable()
defaultCanSort: Bool
false
true
, this column will be sortable, regardless if it has a valid accessor
disableSortBy: Bool
false
true
, the sorting for this column will be disabledsortDescFirst: Bool
false
true
, the first sort direction for this column will be descending instead of ascendingsortInverted: Bool
false
true
, the underlying sorting direction will be inverted, but the UI will not.sortType: String | Function(rowA: <Row>, rowB: <Row>, columnId: String, desc: Bool)
react-table
will take care of the rest.basic
, datetime
, alphanumeric
. Defaults to alphanumeric
.string
is passed, the function with that name located on either the custom sortTypes
option or the built-in sorting types object will be used.function
is passed, it will be used.The following values are provided to the table instance
:
rows: Array<Row>
preSortedRows: Array<Row>
toggleSortBy: Function(ColumnId: String, descending: Bool, isMulti: Bool) => void
setSortBy: Function(sortBy: Array<Object<id: columnId, desc: Bool = true>>) => void
The following properties are available on every Column
object returned by the table instance.
canSort: Bool
toggleSortBy: Function(descending, multi) => void
instance
-level toggleSortBy
, however, passing a columnId is not required since it is located on a Column
object already.getSortByToggleProps: Function(props) => props
getSortByToggleProps
hook to extend its functionality.clearSortBy: Function() => void
isSorted: Boolean
sortedIndex: Int
sortBy
array from state that corresponds to this column.-1
isSortedDesc: Bool
true
, the column is sorted descending
false
, the column is sorted ascending
undefined
, the column is not currently being sorted.The best JavaScript newsletter! Delivered every Monday to over 76,000 devs.