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

109
Visualizações
adding ang removing row

I have a grid array of objects and it has default data from the database , now on the front end the data are being displayed on the table/grid and user can add row and delete row , when I add a row I only want to insert an empty object.

my issue is when I add a row duplicate ids are existing and when I delete only just a single row sometimes multiple rows are being deleted

What seem to be the issue of my implementation ? how do I delete and add rows without compromising the ids ? Thanks.

enter image description here

#grid data - from the database

gridTableData [
    {
        "id": 0,
        "demos": "21 Est. Pop",
    },
    {
        "id": 1,
        "demos": "Households",
    },
    {
        "id": 5,
        "demos": "Avg HH Inc",
    },
]

#add and delete row code

 deleteRow(id: number) {
    const filteredDemographics = this.gridTableData.filter(s => s.id !== id);
 this.gridTableData = [...filteredDemographics];

  }

  addRow() {
    this.gridTableData.push({id:this.gridTableData.length});
    console.log("this.gridTableData" , this.gridTableData)
  }

#html

  <tbody *ngIf="gridTableData.length > 0">
                <tr *ngFor="let row of gridTableData;let i = index" [ngClass]="{'row-is-editing':isRowEditing(i)}">
                    <td>
                        <button *ngIf="checkIfExistsOnDefaultGrid(row) === false" class="material-icons-outlined" mat-icon-button color="error"
                            (click)="deleteRow(row.id)">
                            <mat-icon>delete_outline</mat-icon>
                        </button>
                    </td>
                </tr>
            </tbody>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you are adding with the row length and removing by index you might get some weird cases.

For example, if you add two entries in your snippet, you will have 2 rows with ID 5. Therefore, when you filter/delete, you would remove two entries.

If you want change the order, always use the index to remove or add.

If you want to use ID, make sure the ID will always be unique.

Here is a possible solution making the ID random https://stackblitz.com/edit/angular-pebbqb?file=src/app/app.component.ts

about 4 years ago · Juan Pablo Isaza 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