Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column ordering weird behavior on grouped columns #5577

Open
2 tasks done
nurbek-mirai opened this issue May 23, 2024 · 5 comments
Open
2 tasks done

Column ordering weird behavior on grouped columns #5577

nurbek-mirai opened this issue May 23, 2024 · 5 comments

Comments

@nurbek-mirai
Copy link

nurbek-mirai commented May 23, 2024

TanStack Table version

V8.17.3

Framework/Library version

React ^18

Describe the bug and the steps to reproduce it

When implementing Column ordering DnD I encountered 2 issues:

  1. Initial column ordering is wrong when there is grouped columns (The Names grouped column appears last in the order even though it's second column in columns variable)
  2. When drag and dropping grouped columns cause weird behavior

columnOrder variable shows correct order but on the UI it's wrong.

I think these issues are similar: #4872 #5179

Your Minimal, Reproducible Example - (Sandbox Highly Recommended)

https://stackblitz.com/edit/tanstack-table-gu9mzb?file=src/main.tsx

Screenshots or Videos (Optional)

No response

Do you intend to try to help solve this bug with your own PR?

No, because I do not have time to dig into it

Terms & Code of Conduct

  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  • I agree to follow this project's Code of Conduct
@KevinVandy KevinVandy added the v7 label Jun 3, 2024
@KevinVandy
Copy link
Member

Sorry, closing v7 issues. The version you listed does not match the code sandbox

@nurbek-mirai
Copy link
Author

Am I missing something? It seems like the version is correct. I took it from the official example: https://tanstack.com/table/v8/docs/framework/react/examples/column-dnd
image

@mgryal
Copy link

mgryal commented Jun 13, 2024

I have the same problem with v8, did you solve it?

@nurbek-mirai
Copy link
Author

@mgryal No, I did not.
@KevinVandy Could you please take a look again, the bug is in the v8.

@mgryal
Copy link

mgryal commented Jun 18, 2024

@nurbek-mirai I think I found something. Instead of mapping the column IDs, try this.

function getColumns(column: any) {
    if (column.columns) {
      return column.columns.map(getColumns) as string[];
    }
    return (column.id as string) || (column.accessorKey as string);
  }
function getColumnsId(columns) {
  const columnsArray = [];
  columns?.forEach((column) => {
    const columnsId = getColumns(column);
    if (typeof columnsId == 'string') columnsArray.push(columnsId);
    else columnsArray.push(...columnsId);
  });
  console.log(columnsArray);
  return columnsArray;
}

And replace the columnOrder state

 const [columnOrder, setColumnOrder] = React.useState<string[]>(() =>
    getColumnsId(columns)
  );

It's only for two levels of header grouping, but you can extrapolate the code.

@KevinVandy KevinVandy reopened this Jul 7, 2024
@KevinVandy KevinVandy removed the v7 label Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants