$scope.moveUp = function (smItems) { if (smItems != 'undefined' && smItems.length > 0) { smItems.forEach(function (item) { var sortOrder = $scope.assignedItems.indexOf(item); $scope.assignedItems.move_element(sortOrder, -1); }); if (!$scope.keepPristine) $scope.form.$setDirty(); }; }; $scope.moveDown = function (smItems) { if (smItems != 'undefined' && smItems.length > 0) { smItems.forEach(function (item) { var sortOrder = $scope.assignedItems.indexOf(item); $scope.assignedItems.move_element(sortOrder, 1); }); if (!$scope.keepPristine) $scope.form.$setDirty(); }; };The directive itself is taken from somewhere, I think. Here is the method for moving elements:
Array.prototype.move_element = function (index, delta) { // This method moves an element within the array // index = the array item you want to move // delta = the direction and number of spaces to move the item. // // For example: // move_element(myarray, 5, -1); // move up one space // move_element(myarray, 2, 1); // move down one space // // Returns true for success, false for error. var index2, temp_item; // Make sure the index is within the array bounds if (index < 0 || index >= this.length) { return false; } // Make sure the target index is within the array bounds index2 = index + delta; if (index2 < 0 || index2 >= this.length || index2 == index) { return false; } // Move the elements in the array temp_item = this[index2]; this[index2] = this[index]; this[index] = temp_item; return true; }So, my colleagues who are testing our application noticed the following problem (which I can reproduce). We can move items up OK (it seems), but if two items are selected with no item in between them, then we can not move them down. If I select, say, 3rd and 5th item and have more items below, I can move my items down OK (both are moved down).