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
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.
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.
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.
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)
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 ] });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With