1

I have a dashboard with fix size of cards(4).Whenever i shift any one card to another position, Duplicate of that same card got created in dashboard. Now I have total 5 cards.If I do this same, again it will create duplicates.My aim is just to move a card to another position without duplication.

In html file

  <div class="card" 
        ng-repeat="card in dashCtrl.cards track by $index"
         dnd-draggable="card" 
         dnd-type="'card'" 
         dnd-effect-allowed="move"
         dnd-moved="dashCtrl.updateCardPosition($index)"
         dnd-dynamic-placeholder-size="true" ng-include="'/partials/dashboard/'+card.template+'.html'">
  </div>

Here dashCtrl == dashboard_controller

in dashboard controller.

function updateCardPosition($index) {
            // if(vm.cards.length!=4)validEntry();
            // vm.cards.splice($index, 1);
            console.log('dsdsdsdsds',$index);

            vm.cards = _.uniqBy(vm.cards,'template');
            vm.cards.forEach((ele)=>{
                console.log('xxxxxxxxxxxxxx',ele);  
            });
            // console.log("cards",vm.cards.length);
            // console.log('index',$index);
            Store.set('cards', JSON.stringify(vm.cards));
        }

Here vm is object(this) of controller and it has many method. So vm.cards is an array of cards.I used Store(for local storage) to store modified array(in terms of position).

structure of vm.cards

vm.cards = [
            {template: 'agenda-card'},
            {template: 'budget-card'},
            {template: 'collaborate-card'},
            {template: 'todo-card'}
        ];

I checked everything from google but I couldn't find the solution.I guess duplication problem might arise from dnd-draggable.I am just guessing. So how to solve that duplication problem??

Dhruv Koshiyar
  • 167
  • 1
  • 10

1 Answers1

0

remove the track by $index from ng-repeat="card in dashCtrl.cards track by $index"

Al-un
  • 3,102
  • 2
  • 21
  • 40
Anilkumar
  • 11
  • 3
  • 1
    Please explain why removing `track by` is a solution. According to [this post](https://stackoverflow.com/a/22761548/4906586), if you have duplicate values when using `track by`, it means that your source list has duplicate values somewhere so removing `track by` won't remove the root cause of the issue – Al-un Mar 20 '19 at 15:13