Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
sortablejs changing list first and adding new element make element positions incorrect

I am creating simple sortablejs list that can add new element after the current element and change the position. when I drag and change element to last element and add new element will add new element to the top of the element. but I want to add that below the current element.

import { Component } from '@angular/core';
import { Options } from 'sortablejs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  title = 'my-app';
  items = [1, 2, 3, 4, 5];
  items2 = this.items.slice();

  draggableOptions: Options = {
    onUpdate: (event) => {
      let newArr2 = this.items.slice();
      let newEle = this.items.splice(event.oldIndex, 1)[0];

      this.items.splice(event.newIndex, 0, newEle);
    },
    onStart: (event) => {
      // ExcelStore.dispatch({type: ExcelAction.SECTION_DRAG_START, payload: {dragIndex : event.oldIndex}});
    },
    sort: true,
  };

  addNew(item) {
    let currentIndex = this.items.indexOf(item);
    console.log(item);
    this.items.splice(currentIndex + 1, 0, item + 15);
    //this.items.push(item+15)
  }

  checkIndex(item) {
    console.log(this.items);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<h2>Drag / drop the item</h2>
<div [sortablejs] [sortablejsOptions]="draggableOptions">
  <div *ngFor="let item of items"  style="border: 1px solid" >
    <h4>{{ item }}</h4>
    <button (click)="addNew(item)">Add new</button>
    <button (click)="checkIndex(item)">check index</button>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda