mirror of
https://github.com/apache/superset.git
synced 2026-04-23 01:55:09 +00:00
feat: add drag and drop column rearrangement for table viz (#19381)
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
usePagination,
|
||||
useSortBy,
|
||||
useGlobalFilter,
|
||||
useColumnOrder,
|
||||
PluginHook,
|
||||
TableOptions,
|
||||
FilterType,
|
||||
@@ -64,6 +65,7 @@ export interface DataTableProps<D extends object> extends TableOptions<D> {
|
||||
sticky?: boolean;
|
||||
rowCount: number;
|
||||
wrapperRef?: MutableRefObject<HTMLDivElement>;
|
||||
onColumnOrderChange: () => void;
|
||||
}
|
||||
|
||||
export interface RenderHTMLCellProps extends HTMLProps<HTMLTableCellElement> {
|
||||
@@ -95,12 +97,14 @@ export default typedMemo(function DataTable<D extends object>({
|
||||
hooks,
|
||||
serverPagination,
|
||||
wrapperRef: userWrapperRef,
|
||||
onColumnOrderChange,
|
||||
...moreUseTableOptions
|
||||
}: DataTableProps<D>): JSX.Element {
|
||||
const tableHooks: PluginHook<D>[] = [
|
||||
useGlobalFilter,
|
||||
useSortBy,
|
||||
usePagination,
|
||||
useColumnOrder,
|
||||
doSticky ? useSticky : [],
|
||||
hooks || [],
|
||||
].flat();
|
||||
@@ -172,6 +176,8 @@ export default typedMemo(function DataTable<D extends object>({
|
||||
setGlobalFilter,
|
||||
setPageSize: setPageSize_,
|
||||
wrapStickyTable,
|
||||
setColumnOrder,
|
||||
allColumns,
|
||||
state: { pageIndex, pageSize, globalFilter: filterValue, sticky = {} },
|
||||
} = useTable<D>(
|
||||
{
|
||||
@@ -211,6 +217,33 @@ export default typedMemo(function DataTable<D extends object>({
|
||||
|
||||
const shouldRenderFooter = columns.some(x => !!x.Footer);
|
||||
|
||||
let columnBeingDragged = -1;
|
||||
|
||||
const onDragStart = (e: React.DragEvent) => {
|
||||
const el = e.target as HTMLTableCellElement;
|
||||
columnBeingDragged = allColumns.findIndex(
|
||||
col => col.id === el.dataset.columnName,
|
||||
);
|
||||
e.dataTransfer.setData('text/plain', `${columnBeingDragged}`);
|
||||
};
|
||||
|
||||
const onDrop = (e: React.DragEvent) => {
|
||||
const el = e.target as HTMLTableCellElement;
|
||||
const newPosition = allColumns.findIndex(
|
||||
col => col.id === el.dataset.columnName,
|
||||
);
|
||||
|
||||
if (newPosition !== -1) {
|
||||
const currentCols = allColumns.map(c => c.id);
|
||||
const colToBeMoved = currentCols.splice(columnBeingDragged, 1);
|
||||
currentCols.splice(newPosition, 0, colToBeMoved[0]);
|
||||
setColumnOrder(currentCols);
|
||||
// toggle value in TableChart to trigger column width recalc
|
||||
onColumnOrderChange();
|
||||
}
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
const renderTable = () => (
|
||||
<table {...getTableProps({ className: tableClassName })}>
|
||||
<thead>
|
||||
@@ -223,6 +256,8 @@ export default typedMemo(function DataTable<D extends object>({
|
||||
column.render('Header', {
|
||||
key: column.id,
|
||||
...column.getSortByToggleProps(),
|
||||
onDragStart,
|
||||
onDrop,
|
||||
}),
|
||||
)}
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user