Skip to content

Instantly share code, notes, and snippets.

@kjantzer
Created September 19, 2012 17:00
Show Gist options
  • Save kjantzer/3750782 to your computer and use it in GitHub Desktop.
Save kjantzer/3750782 to your computer and use it in GitHub Desktop.
jQuery sortableWithIndexWatch - modified jquery ui sortable widget to store before and after index of moved item
// custom Sortable widget - gets before and after index of moved item
// https://gist.github.com/3750782
$.widget("ui.sortableWithIndexWatch", $.extend({}, $.ui.sortable.prototype, {
_trigger: function() {
switch(arguments[0]){
case 'start':
this.currentItem.indexStart = this.currentItem.index();
break;
case 'update':
this.currentItem.indexEnd = this.currentItem.index();
break;
}
return $.ui.sortable.prototype._trigger.apply(this, arguments);
}
}));
@gabn88
Copy link

gabn88 commented Jul 18, 2015

I have a question about this... I am in my sortable receiver function like this:

$(".sortable-container").sortable({
receive: function(ev, ui){
}

Here I have $(this).data() which is giving me:

Object {ui-sortable: e.w...t.e.(...).(anonymous function)}

in this object I have ui-sortable, which has the currentItem attribute which is what I need...

How do I get to the currentItem attribute?

I have tried: $(this).data().ui-sortable and $(this).data().uiSortable but both give undefined... Thank you so much for you help!

@yugaego
Copy link

yugaego commented Mar 12, 2017

How do I get to the currentItem attribute?

I have tried: $(this).data().ui-sortable and $(this).data().uiSortable but both give undefined

Just for a reference: use $(this).data()['ui-sortable'].currentItem in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment