Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add / Remove row(s) in ag-grid

How to add or remove a row in ag-grid,
i try this, but not work

$scope.gridOptions.rowData.push(data);

and with this

scope.gridOptions.api.forEachNode( function(node) {
    var data = node.data;
    updatedNodes.push(vehicle);
});

$scope.gridOptions.api.refreshRows(updatedNodes);

Thanks

like image 260
ⴱⵓⵖⴰⵏⵉ ⵔⴰⴼⵉⵇ Avatar asked Jul 21 '16 13:07

ⴱⵓⵖⴰⵏⵉ ⵔⴰⴼⵉⵇ


People also ask

How do I remove a row from ag-Grid?

Sometimes your users want to remove rows and cells in AG Grid by pressing Backspace or Delete keys. The following example uses api. applyTransaction ({...}) to remove selected rows, when the user presses BACKSPACE or DELETE keys.

How do you add a new row on ag-Grid?

Adding a row to the grid is very simple. Use the gridApi that we get after gridReady to add a row. This process is called applying transaction. Create a button that takes a onClick funtion, when we click the button add a callBack funtion that does the following operation.

How do I make rows disabled on ag-Grid?

There is no option in the library to make a single row disabled(both visually and keyboard event based), the only way we could make a single row disabled is by using a customCellRenderer for both header and subsequent cell checkboxes, this allows full control over the checkbox.


2 Answers

This is what worked for me with the ag-grid community version 22.1.1:

add new row

const row = //someNewRowDataHere

this.gridOptions.rowData.push(row)
this.gridApi.setRowData(this.gridOptions.rowData)

remove

const selectedRow = this.gridApi.getFocusedCell()
const id = this.gridOptions.rowData[selectedRow.rowIndex].i

this.gridOptions.rowData.splice(selectedRow.rowIndex, 1)
this.gridApi.setRowData(this.gridOptions.rowData)
like image 72
jogarcia Avatar answered Sep 21 '22 21:09

jogarcia


I personally like this approach

Add row

$( '#newRow' ).on( 'click', function() {
    gridOptions.api.applyTransaction({ add: gridOptions.rowData });
} );

Remove row (ex. clicking on a bin-icon)

Works with rowDragManaged enabled

// remove row
$( document ).on( 'click', '#myGridFees .fa-trash-alt', function(e) {
    $.fn.agGridRemoveRowByBinClick(gridFeeOptions, $(this));
});


/**
 *
 * @param agGridOption
 * @param $this
 */
$.fn.agGridRemoveRowByBinClick = function(agGridOption, $this) {
    let id = $this.closest('.ag-row').attr( 'row-id' );
    agGridOption.api.applyTransaction({ remove: [ agGridOption.api.getRowNode(id).data ] });
}
like image 34
Unicco Avatar answered Sep 19 '22 21:09

Unicco