3

I'm trying to use ng2-dragula in my app to add the option to reorder a list of instructions that were added to a list. For example, think of it as if you were making a recipe and you put in an instruction but then wanted to move it somewhere else in the list without deleting other ones, etc. I'm going for something similar.

Here's the HTML:

<div [dragula]="'recipeInstructionBag'" [dragulaModel]="recipe.instructions">
    <div class="input-group" *ngFor="let instruction of recipe?.instructions; let i = index; trackBy:trackInstructions">
        <span class="input-group-addon reorder-trigger"><i class="fa fa-reorder"></i></span>
        <input class="form-control" type="text" [ngModel]="instruction" (ngModelChange)="recipe.instructions[i] = $event" [name]="'recipeInstruction' + i">
    </div>
</div>

So I've got the directives in the right spot, I've got the *ngFor working and able to edit instructions in place, etc. Everything that I want to happen is working, except when dragging to reorder these items. I've looked at the docs, specifically the Angular specific examples at the bottom of the page, and that's where I got the info for the [dragulaModel] directive. The problem is when I try and drag and drop elements, they don't reorder properly. I'll try and explain:

With two items in the list, dragging to reorder doesn't reorder them. I drop the second item so that it should be the first, and it gets placed back in its original spot.

Now watch this GIF with 3 items:

3 items gif

As you can see I try and move item 3 into the second spot. It keeps getting dropped to the bottom of the list. Then I take it to the top spot, and it reorders the list to be 2, 3, 1. Similar things happen when there are 4 or more items in the list.

Also, when adding a new item after trying to reorder, it gets placed in the array in unexpected places:

enter image description here

I'm not sure what the deal is or how to fix it. If anyone has experience with this and can help, I'd greatly appreciate it!

pjlamb12
  • 2,300
  • 2
  • 32
  • 64
  • I never faced this problem. create plunker. – CharanRoot Apr 28 '17 at 15:16
  • 1
    it could be due to your trackBy in the *ngFor... if its using the array index in any way to track the instructions and dragula reorders the array (changing indexes) then DOM nonsense could occur – cboston Jun 12 '17 at 22:06

0 Answers0