Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Drag and drop with Antd's Tree table

I am trying to make a tree table reorderable via drag and drop.

Here's the render for easier visualization:

enter image description here

I am also using React-DnD.

Here's a piece of my code:

const moveRow = useCallback(
  (record: StructureElement) => (dragIndex, hoverIndex) => {
    // console.log(record.name, dragIndex, hoverIndex);
    const dragRow = sortableData[dragIndex];
    if (!dragRow) {
      console.log(sortableData, dragIndex);
    } else {
      console.log('drag', dragRow.name, 'hover', sortableData[hoverIndex].name);
    }
    // todo - change the order in array
  },
  [sortableData],
);

<StyledTreeTable
  components={components}
  dataSource={sortableData}
  onRow={(record, index) => {
    return {
      index,
      moveRow: moveRow(record as StructureElement),
    };
  }}
/>

So the problem is, my sortableData looks like this:

[
  { name: 1 },
  { name: 2 },
  {
    name: 3,
    children: [
      { name: 11 },
      { name: 22 },
      { name: 33, children: [{ name: 111 }, { name: 222 }, { name: 333 }] },
    ],
  },
];

So my objects can have more objects nested in children.

But moveRow function doesn't see it this way. For it, ABSTRACT_STATE_4 is of index 4. So when I try to reorder the array, I try to do this: data[4], which results in undefined.

Is there a way I can reorder the items in tree table with drag and drop, that I haven't found yet?

The only way I see of solving it now, is to recreate the array the way hover and drag indexes match. But that's a lot of seemingly unnecessary work.

Is there a way to get the record I am hovering over, like I'm getting hoverIndex?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!