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

361
Visualizações
Angular2 ngFor - change items order without them being recreated

I have quite specific scenario where I have to store state inside of item-components (namely, inside canvas elements) that are being created via ngFor loop.

In my list component I have an array of string ids - and I need to create a canvas element for each id. Afterwards some manipulations can occur with canvas data, so if canvas elements are recreated - pixel data will be lost.

However, sometimes the order of string ids may change and, hence, I would also want to change the order in which ngFor renders those canvas elements. Without actually removing them and creating again.

I know, this scenario might sound weird and there are some other ways to handle what I want but at the moment I'm just interested if this would be possible at all in Angular2/4?.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can manage the object with a @Pipe, you just add the pipe to your code, example:

*ngFor="let field of formFields | keys"

Where keys is a @Pipe.

Check this tutorial for creating a custom @Pipe: https://scotch.io/tutorials/create-a-globally-available-custom-pipe-in-angular-2

As you didn't post any code, I assume you have your "change order of elements within object" functionality ready.

So, you could build your @Pipe like this:

@Pipe({name: 'alterOrder'})
export class AlterOrderPipe implements PipeTransform {
    transform(value, args: string[]): any {
        let myArray = [];
        for (let elem in value) {
            if (!isNullOrUndefined(elem)) {                    
                // Alter order functionality
                myArray.push(alteredOrderObj);  // alteredOrderObj would be the result of your functionality
            }
        }
        return myArray;
    }
}

And in your code, you use it like this:

*ngFor="let elem of myArray | alterOrder"

over 4 years ago · Santiago Trujillo Relatório

0

It sounds like you want to visually re-order the elements but not re-render them correct? I believe if you don't want to re-render the elements but want them to present in a different visual order, then your best bet is CSS. Maybe use angular to calculate the positioning and then use CSS positioning.

over 4 years ago · Santiago Trujillo Relatório
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