Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Add oldindex property to sortupdate event data #27

Closed
wants to merge 1 commit into from

Conversation

flying-sheep
Copy link

knowing the old index is a useful feature for using this in an MVVC application, since the underlying array can be spliced instead of having to be rewritten.

AngularJS example:

<div data-ng-controller="SortableCTRL">
    <ul id="sortable">
        <li data-ng-repeat="item in sortableArray" data-ng-bind="item"></li>
    </ul>

    <pre data-ng-bind="sortableArray|json"></pre>
</div>
function SortableCTRL($scope) {
    $scope.sortableArray = ['One', 'Two', 'Three'];

    $scope.sortupdate = function(e, data) {
        var start = data.oldindex,
            end   = data.item.index();

        $scope.sortableArray.splice(end, 0,
            $scope.sortableArray.splice(start, 1)[0]);

        $scope.$apply();
    };

    setTimeout(function() {//applies this after the items are generated is rendered
        $('#sortable')
            .sortable()
            .on('sortupdate', $scope.sortupdate);
    });
}

knowing the old index is a useful feature for using this in an MVVC application, since the underlying array can be spliced instead of having to be rewritten.

AngularJS example:

```javascript
function SortableCTRL($scope) {
	$scope.sortableArray = ['One', 'Two', 'Three'];
	
	$scope.sortupdate = function(e, data) {
		var start = data.oldindex,
			end   = data.item.index();
		
		$scope.sortableArray.splice(end, 0,
			$scope.sortableArray.splice(start, 1)[0]);
		
		$scope.$apply();
	};
	
	setTimeout(function() {
		$('#sortable')
			.sortable()
			.on('sortupdate', $scope.sortupdate);
	});
}```
@lewang
Copy link

lewang commented Oct 23, 2012

👍 Not just for MVC, old index is generally useful.

@jcurtis
Copy link

jcurtis commented Dec 17, 2012

Why not add newindex while we're at it?

@flying-sheep
Copy link
Author

sortupdate is afaik triggered after the list has been resorted, so the current index is the new index.

else we wouldn’t need the oldindex (as it would be the – still – current index)

@jcurtis
Copy link

jcurtis commented Dec 18, 2012

Right. To make things easier for myself, something like this would be viable?

dragging.parent().trigger('sortupdate', {
  item: dragging, 
  oldindex: index, 
  newindex: dragging.index()
});

@flying-sheep
Copy link
Author

sure, just:

i do that in my example code:

$('#sortable').sortable().on('sortupdate', function(e, data) {
    var start = data.oldindex,
        end   = data.item.index();
});

why would data.item.index() be less easy than data.newindex?

@jcurtis
Copy link

jcurtis commented Dec 18, 2012

Ah. I lept before I looked. Was looking for exactly that bit of code. Thanks.

@voidberg
Copy link
Collaborator

This project has been discontinued by its author and the new mantained version is here.

Since the projects have diverged a while ago I can't migrate issues and pull requests. Please try the new version and if the issue or pull request still makes sense, please resubmit it.

Sorry for the inconvenience.

@voidberg voidberg closed this Nov 21, 2014
@flying-sheep
Copy link
Author

@flying-sheep flying-sheep deleted the patch-1 branch November 21, 2014 15:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants