>$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(); > }; > };>
> 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; > }>