nestedSortable icon indicating copy to clipboard operation
nestedSortable copied to clipboard

Relocate event doesn't seem to work

Open DrSchizo opened this issue 11 years ago • 13 comments

Hi,

Thank for this great script!

Unfortunately, the "relocate" event doesn't seem to work in the following code:

$("ol.sortable").nestedSortable({ change: function(){ console.log("change"); } relocate: function(){ console.log("relocate"); } });

The change event is triggered, but not the relocate one.

If I look into the code on line 406, it is because pid is equal to this._uiHash().item.parent().parent().attr("id")

I tried it on the example provided (on example.html, by replacing "change" by "relocate" in the option) so it doesn't look like it is a configuration mistake.

Thanks a lot

DrSchizo avatar Aug 22 '14 17:08 DrSchizo

Since you seem to have figured it out, would you mind putting in a pull request for the change needed to fix?

ilikenwf avatar Aug 22 '14 19:08 ilikenwf

I'm sorry, I tried to find where the real problem is but I failed. My OOP JS knowledge is very slight. All I can say is that on the _mouseStop event (line 377), the aswer to the if test on line 406 is always "false".

Indeed, the two following statements are always true: 1/ $(this.domPosition.parent).parent().attr("id") == this._uiHash().item.parent().parent().attr("id") 2/ $(this.domPosition.prev).next().index() == this._uiHash().item.index()

The ids and the indexes returned are always the one of the dragged element BEFORE it is moved.

DrSchizo avatar Aug 25 '14 15:08 DrSchizo

I'll take a look when I get a chance, or maybe someone can before I do - depends on my schedule.

Thanks for trying anyway!

ilikenwf avatar Aug 25 '14 17:08 ilikenwf

@twicejr mind taking a look?

ilikenwf avatar Aug 25 '14 17:08 ilikenwf

Thank you a lot.

There is no hurry for me since I've triggered the move is allowed (!=revert). The event is triggered to often (even when the item hasn't move), but it works anyway ;-)

One nice addition would be to pass as arguments of the relocate function the new and the old position of the item. For instance, my script is saving the new location in real time, and not only after clicking on a "save" button. I managed to do it by exporting the toArray output and comparing it with a saved version, but it could have been not much easier to do, especially since the previous and current index and parent id should be retrieved in the script just before triggering the relocate event (at least, once the bug will be fixed).

DrSchizo avatar Aug 25 '14 18:08 DrSchizo

@DrSchizo , I assume you did NOT forget the comma after the change function, as in your sample code? Will look into it when I have time. I think whatever.parent().parent().attr("id") is not a very elegant solution anyway!

twicejr avatar Aug 25 '14 18:08 twicejr

No, I didn't. The code hasn't been written made by me. The mistake is in the file "jquery.mjs.nestedSortable.js". The two comparisons in the if test are always true and therefore the test always fails.

var pid = $(this.domPosition.parent).parent().attr("id"); var sort = this.domPosition.prev ? $(this.domPosition.prev).next().index() : 0;

if(!(pid == this._uiHash().item.parent().parent().attr("id") && sort == this._uiHash().item.index())) { this._trigger("relocate", event, this._uiHash()); }

You can see the bug by adding this code below: console.log('pid = '+pid+' and '+this._uiHash().item.parent().parent().attr("id")); console.log('sort = '+sort+' and '+this._uiHash().item.index());

DrSchizo avatar Aug 25 '14 19:08 DrSchizo

I think we should solve this in a similar way I coded the disableParentChange functionality. //pseudo code: !(oldParent.is(newParent) && oldItem.index() == newItem.index())

twicejr avatar Aug 25 '14 19:08 twicejr

I ran into the same problem. Perhaps the proposal below will solve the problem. (It does seem to work as expected in my tests)

It seems there's just a small flaw in the if... else... logic. (See below). (Can you verify this solution?)

/* // ORIGINAL CODE if (!(pid == this._uiHash().item.parent().parent().attr("id") && sort == this._uiHash().item.index())) { this._trigger("relocate", event, this._uiHash()); } */

// POSSIBLE SOLUTION var isRelocated = false; if (pid !== this._uiHash().item.parent().parent().attr("id")) { isRelocated = true; } else if( sort !== this._uiHash().item.index() ) { isRelocated = true; } if (isRelocated) { this._trigger("relocate", event, this._uiHash()); }

tfddev avatar Sep 20 '14 23:09 tfddev

See: https://github.com/tfddev/nestedSortable

tfddev avatar Sep 21 '14 02:09 tfddev

If you solved it, please put in a pull request.

On Sat, Sep 20, 2014 at 9:19 PM, TFD - DevDept [email protected] wrote:

See: https://github.com/tfddev/nestedSortable

— Reply to this email directly or view it on GitHub https://github.com/ilikenwf/nestedSortable/issues/4#issuecomment-56286501 .

ilikenwf avatar Sep 21 '14 03:09 ilikenwf

I'm afraid tfddev's proposal does not solve the problem. The underlying problem still is as DrSchizo mentioned: "The ids and the indexes returned are always the one of the dragged element BEFORE it is moved."

GlobetechAG avatar Jan 12 '15 13:01 GlobetechAG

It is fixed and tested in the latest version.

twicejr avatar Jan 14 '15 14:01 twicejr