1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

How to insert row at index into sorted ag-grid

Discussão em 'Angular' iniciado por nikoh, Julho 28, 2025.

  1. nikoh

    nikoh Guest

    I have a grid setup with sorting enabled. Each row has a duplicate button. When duplicating a row, I would like to insert the new row just below the copied row. This works with a default sort but if you sort it on a column like for example status, it randomly inserts the row into the grid, making it hard to find.

    I have noticed that the grid does a sort sometime during save, yet before it gets a response to assign a new id.

    I have tried adding the row using updateRowData(transaction) with an addIndex but it either doesn't insert anything or ignores the index.

    I do have access to any component variables from the postSort but I only get a response with an ID after the postSort.

    cloneRow(item: any): void {
    let newRow = JSON.parse(JSON.stringify(item.data)) as Object;
    newRow.gridItemId = null;

    this.index = this.api.getFocusedCell().rowIndex;

    this.selectRow(newRow, ++index);
    this.saveRow(newRow);
    }

    selectRow(newRow: Object, index: number) {
    this.selectedObject = [];
    this.gridItemList.splice(index, 0, newRow);

    this.api.setFocusedCell(index, "ColumnName");
    this.api.startEditingCell({
    rowIndex: index,
    colKey: "ColumnName"
    });
    }

    saveRow(newRow: Object): void {
    let objectsToSave = new Array<Object>();
    objectsToSave.push(newRow);

    this.CustomService.saveRow(objectsToSave)
    .subscribe(
    (response) => {
    if (!newRow.rowId) {
    newRow.rowId = response[0].rowId;
    }
    this.gridItemList.filter(g => g.gridItemId === response[0].gridItemId)[0] = response[0];
    this.api.refreshCells();
    }

    postSort = function(rowNodes) {
    console.log("Post Sort");
    }


    Expected: The copied row gets inserted just below the original row. Actual: The copied row get inserted somewhere in the grid.

    Continue reading...

Compartilhe esta Página