2

I'm using ng-drag and drop npm module which provides draggable directive. I also have a 2D array with list of items I want to display inside li elements and make each of them draggable. The thing is that I cannot use multiple ngFor's because that is not valid in angular, so I need to find a way to get around this. For that I wrapped my inner array items inside ng-container but this doesn't help because then I can't drag individual li items, it wraps the whole list. The only idea I have came up with is this:

app.component.ts

  vegetables = [[
{name: 'Carrot', type: 'vegetable'},
{name: 'Onion', type: 'vegetable'},
{name: 'Potato', type: 'vegetable'},
{name: 'Capsicum', type: 'vegetable'}],
[
  {name: 'Carrotas', type: 'vegetable'},
  {name: 'Onionas', type: 'vegetable'},
  {name: 'Potatoas', type: 'vegetable'},
  {name: 'Capsicumas', type: 'vegetable'}]]

this.i++;

      this.indexes = this.vegetables[this.i];

app.component.html

<li class="list-group-item list-group-item-action list-group-item-success" [draggable] *ngFor="let item of [this.indexes]" [dragClass]="'active'" [dragTransitClass]="'active'" [dragData]="item" [dragScope]="item.type" [dragEnabled]="dragEnabled">
      {{item.name}}

</li>

but this doesn't print out anything either.

Now I left my code like this:

 <li class="list-group-item list-group-item-action list-group-item-success" [draggable] *ngFor="let item of [vegetables[0]]" [dragClass]="'active'" [dragTransitClass]="'active'" [dragData]="item" [dragScope]="item.type" [dragEnabled]="dragEnabled">
  <ng-container *ngFor="let items of item">
      {{items.name}}
  </ng-container>
</li>

but it puts all items in outside array index in one li row and not separate draggable items as I need and looks like this:

enter image description here

But I want each item to be in separate green rows and each to be draggable.

Limpuls
  • 856
  • 1
  • 19
  • 37

1 Answers1

3

You are close. You need to invert li and ng-container and you will be good to go. Example:

<ng-container *ngFor="let item of [vegetables[0]]">
  <li 
    *ngFor="let items of item"
    class="list-group-item list-group-item-action list-group-item-success"
    [draggable]
    [dragClass]="'active'"
    [dragTransitClass]="'active'"
    [dragData]="item"
    [dragScope]="item.type"
    [dragEnabled]="dragEnabled">
    {{items.name}}
  </li>
</ng-container>
Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51