minop1205 / react-dnd-treeview

A draggable / droppable React-based treeview component. You can use render props to create each node freely.
MIT License
519 stars 69 forks source link

wrong `destinationIndex` calculation when dropping to the root #129

Closed 1998MaxChykalo closed 1 year ago

1998MaxChykalo commented 2 years ago

if I drag an element and drop it at the root level then destinationIndex is calculated as if every element is the direct child of the root element. Example:

image

the destinationIndex is 6. But we have only 5 elements in the root, so the destinationIndex in this case should've been 4:

image

If we drop an element to some nested item, then destinationIndex is calculated relatively to the item it was dropped on. link to sandbox: https://codesandbox.io/s/minimum-configuration-js-forked-n3yor6?file=/src/App.jsx

Expected result: destinationIndex is calculated based on the level of nesting where it was dropped

Surjit4048 commented 2 years ago

Any update on this? I am facing same issue.

Surjit4048 commented 2 years ago

nvm, I can access parent and previous node with setup easy. All good for me.

hightgha commented 1 year ago

@minop1205 any update? same issue

a-utkarsh commented 1 year ago

Is there any update or any workaround for this, I need to know the index of an item where it inserted with respect to its parent in case of sortable false. @minop1205

Please update ASAP.

a-utkarsh commented 1 year ago

is there anyway to add id to the li element? @minop1205

minop1205 commented 1 year ago

Sorry everyone for misleading you with the README explanation. First, let us discuss destinationIndex.

If the sort property of the tree is false, the order of the nodes follows the order of the data array.Therefore, reordering the nodes requires reordering the data array.

destinationIndex is not an index value relative to the parent node, but rather an index value indicating where in the data array the drag source data is to be moved.

In fact, the array swapping is done inside the library, and the onDrop callback receives the reordered data array.

There is currently no way to get the index value relative to the parent node. I will make it available in the next update.

Also, I'll correct the description of destinationIndex in the README.

If you don't mind me asking as a reference, what is the use of relative indexes? For example, is this a case of sending a relative index to synchronize the tree sequence with the server?

hightgha commented 1 year ago

Sorry everyone for misleading you with the README explanation. First, let us discuss destinationIndex.

If the sort property of the tree is false, the order of the nodes follows the order of the data array.Therefore, reordering the nodes requires reordering the data array.

destinationIndex is not an index value relative to the parent node, but rather an index value indicating where in the data array the drag source data is to be moved.

In fact, the array swapping is done inside the library, and the onDrop callback receives the reordered data array.

There is currently no way to get the index value relative to the parent node. I will make it available in the next update.

Also, I'll correct the description of destinationIndex in the README.

If you don't mind me asking as a reference, what is the use of relative indexes? For example, is this a case of sending a relative index to synchronize the tree sequence with the server?

@minop1205 in my case, i wanna to update order in database after reordering items. Cuz destinationIndex is wrong, i'm calculating order in current lvl, by checking position in array. Ordering is applied in some lvl For example: 1 Parent1 1 Child1 2 Child2 2 Parent2 3 Parent3 1 Child1 2 Child2

a-utkarsh commented 1 year ago

@minop1205 Yes, I need to synchronize the tree sequence with the server. Otherwise how will we know where the node is inserted when we are reordering within the same parent or different parent. Anyway for those who are still stuck in this issue, I have found a workaround or hack you can say. Here is the code for the same, You already have the drag node id and you can get the parent node of the same Basically need to pass down the node and parent node to the list item and get all the items from the dom. image image

minop1205 commented 1 year ago

@hightgha @a-utkarsh Thank you for your comment!

I have just released v3.3. In this version, an option called relativeIndex is available in addition to destinationIndex.

The relativeIndex indicates the index relative to the parent node. The relativeIndex is actually equivalent to the index of the placeholder.

Below is an image of how it works.

2022-11-02_04h53_42

I will Close this Issue, but reopen it if you have any problems.