Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Logic problem
Message
De
19/07/2017 03:53:49
 
 
Information générale
Forum:
Javascript
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
01652713
Message ID:
01652732
Vues:
66
This message has been marked as the solution to the initial question of the thread.
>Hi everybody,
>
>Here is a new puzzle. We have a mover type of the control that also allows to re-sort items (e.g. move up or down). Here is the code for up/down movement:
>
>
>$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).
>
>Do you see what is a bug in the logic here?
>
>Thanks in advance.

Assume the array is : A B C D E F and you want to move B and C down 1 position.
After the first move_element you will have A C B D E F
Now when moving C you will be back to having : A B C D E F

For it to work you would need to iterate the array in reverse order when moving down.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform