Steve
Steve

Reputation: 14922

Angular Dragula and ng-repeat $index

I have an ng-repeat of rows. I set them up so you can reorder them with drag-and-drop using Angular Dragula. This works fine, but the ng-repeat $index remains the initial value for each item after dragging. See screen captures below before and after a drag of the "Recipient" row.

Is there a way to update the index? I also want to re-order the menu with javascript, with that button in each row, but for that to work I need to get the current $index (and decrement/increment it) and that won't work if the index is incorrect like this.

Before Drag

enter image description here

After Drag

enter image description here

Upvotes: 0

Views: 982

Answers (2)

Steve
Steve

Reputation: 14922

For what it's worth, this is the answer:

 <div class="table-cards-body" dragula="'first-bag'" dragula-model="home.quickReceiveFields">

      <div class="table-cards-row drag-item" ng-repeat="field in home.quickReceiveFields track by $index">
        <div class="one">{{field.name}}</div>
        <div class="two">{{field.type}}</div>
        <div class="three">{{field.default}} {{$index}}</div>
        <div class="four"><input type="checkbox" ng-model="field.display"></div>
        <div class="five"><input type="checkbox" ng-model="field.retain"></div>
        <button class="btn btn-default btn-xs" ng-click="home.moveItemUp($index)">^</button>
      </div>

 </div>

Despite what the docs say on the angular-dragula site, you need to put the dragula-model attribute on the container into which you put the repeating items. Not the ng-repeat itself.

Upvotes: 1

Carnaru Valentin
Carnaru Valentin

Reputation: 1885

Try to add in ng-repeat track by $index.

Upvotes: 0

Related Questions