Move elements clicked from one array to another

0

What I want to do is move an element from one array to another, but only those that the user selects and without JQuery, I am in Angular 4, I leave my code. Bold text

    <div class="create-cont-top-left">
          <h5 class="clrGrayBlack">Lista de campus:</h5>
          <div class="cont-text">
            <p *ngFor="let campus of campuses">{{campus}}</p>
          </div>
        </div>
      <div (click)="addItem()" class="font">
            <i class="fa fa-plus" style="color: green"></i>
            <a>Agregar</a>
          </div>

  addItem(){
    let _ = this;

    for (let i = 0; i < _.campuses.length; i++){
      console.log(_.campuses[i]);
      _.campusesAggregates.push(_.campuses[i]);
      delete _.campuses[i];
      console.log(_.campusesAggregates[i]);
    }

    // console.log(_.campusesAggregates[i]);
    return;
  }
    
asked by Eduardo García 26.09.2018 в 17:50
source

2 answers

1

You must use the ngFor and pass the index to quickly obtain the element.

First you travel in a ngFor

<p (click)="addItem(i,campus)" *ngFor="let campus of campuses; let i=index">
  {{campus}}
  <a class="cursor-pointer green">+</a>
</p>

Then you get a show.

addItem(index:number,item:string){
  this.selected.unshift(item);
  this.campuses.splice(index,1);
}

Here is an example of how to move elements from one vector to another.

Example

    
answered by 26.09.2018 в 23:12
0

Pass the campus to move directly by the method:

addItem(toMove: Campus): void {
  const index = this.campuses.findIndex((c: Campus) => c.key === toMove.key);
  this.campuses.splice(index, 1); // eliminamos el campus del array original
  this.campusesAggregates.push(toMove); // lo agregamos al array origen
}

And in the HTML:

<div (click)="addItem(campus)" class="font">
  <i class="fa fa-plus" style="color: green"></i>
  <a>Agregar</a>
</div>
    
answered by 26.09.2018 в 19:35