Created
March 30, 2020 19:38
-
-
Save anacampesan/aaf842c47506ad5d87842e7c82196482 to your computer and use it in GitHub Desktop.
Column Drag & Drop Reordering - react-table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In your component, add this var and functions: | |
let columnBeingDragged = null; | |
const onDragStart = e => { | |
columnBeingDragged = e.target.dataset.columnIndex; | |
}; | |
const onDrop = e => { | |
e.preventDefault(); | |
const newPosition = e.target.dataset.columnIndex; | |
const currentCols = visibleColumns.map(c => c.id); | |
const colToBeMoved = currentCols.splice(columnBeingDragged, 1); | |
currentCols.splice(newPosition, 0, colToBeMoved[0]); | |
setColumnOrder(currentCols); | |
}; | |
// In the TH element, add the following props: | |
<th | |
{...column.getHeaderProps()} | |
{...column.getHeaderProps(column.getSortByToggleProps())} | |
// THESE | |
data-column-index={i} | |
draggable="true" | |
onDragStart={onDragStart} | |
onDragOver={e => e.preventDefault()} | |
onDrop={onDrop} | |
> | |
{column.render('Header')} | |
</th> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have a code sandbox example?