1

Is it possible to rearrange the multiple selection values in ng-select using cdk Drag and drop. you can check Stackblitz Implementation is changing the model values but not reflected in the dropdown. what could be the way user can rearrange the multiple selected values from dropdown using cdk Drap and Drop. HTML for ng-select with cdk Drag drop

<label>Drap and drop</label>
<ng-select cdkDropList (cdkDropListDropped)="drop($event)" [cdkDropListData]="selectedLocations"
  [(ngModel)]="selectedLocations" [items]="Locations" [searchable]="true" bindLabel="name"  [multiple]="true"
  placeholder="Select Locations">
  <ng-template ng-label-tmp let-item="item" let-clear="clear">
    <div cdkDrag>
      <span
        class="ng-value-icon right"
        (click)="clear(item)">×
      </span>
      <span>{{item.name}}</span>
    </div>
  </ng-template>
</ng-select>
<p> {{selectedLocations|json}}</p>

Image 1

Image 2

Is it possible to use this functionality with ng-select Or any other suggestions to achieve the same.

Vanu
  • 63
  • 1
  • 12

1 Answers1

1

The only way I can see you achieving this is by "mixing" two "structures"

One will be a cdkDropList, but horizontal orientation, like this https://stackblitz.com/angular/eqqmymbjevo?file=src%2Fapp%2Fcdk-drag-drop-horizontal-sorting-example.ts

And a hidden dropdown list, shown when you click on the V icon of your select. For each item of your dropdown, you attach a (click) event, triggering a method to add it self on the cdkDropList source array, and remove it self from the dropdown source list.

For each item added on the cdkDropList source, attach a method to remove itself and added it back to the dropdown list...

Thats a good option... May take a while, but will do the trick

Rafael de Castro
  • 888
  • 4
  • 16
  • 37
  • I don't want to change the ng-select as i need the implementation little early :( . What if I show the selected values in a table and implement cdk drag drop on the same. It is just a thought – Vanu May 15 '21 at 09:05
  • Cdk drag drop doesn't keep the list order. Will be a little messy, but you could try :D – Rafael de Castro May 15 '21 at 13:43
  • 1
    Thanks Rafael de Castro for your inputs. I am able to rearrange the selection in drop down as well as model. But when i drag and drop the content it displays behind the dropdown and it doesnot feel like user is dragging the element. I have updated the stackBlitz too. Any guidance on the same will be a great help. – Vanu May 15 '21 at 14:31
  • @HV712 its happening because the element taht you're seeing being dragged is actually a "duplicate" the API creates to show the dragging. You can control css properties on this element using the class: .cdk-drag-preview and applying a z-index: 9999 property with !important – Rafael de Castro May 15 '21 at 23:15