I have few columns within AG-Grid are being updated every second using setValueData()
within a client-side callback. I also have another server-side callback for the cellValueChanged
event to handle cell edits made by users on the rest of the columns in the grid. Everything is working well except that cellValueChanged
is being unnecessarily triggered every second due to setValueData()
, in addition to being triggered by user-edited cells. I am looking for a solution to prevent the cellValueChanged
event from being triggered by setValueData()
.
import dash
import dash_ag_grid as dag
from dash import html, dcc, Output, Input, State, no_update
import time
import json
app = dash.Dash(__name__)
columnDefs = [{"field": "Update1", "editable": False}, {"field": "Update2", "editable": False}, {"field": "Edit1", "editable": True}, {"field": "Edit2", "editable": True}]
rowData = [{"Update1": 1, "Update2": 2, "Edit1": 3, "Edit2": 4} for i in range(10)]
grid = dag.AgGrid(
id="grid",
columnDefs=columnDefs,
rowData=rowData,
#enableCellChangeFlash=True,
getRowId="params.data.Update1",
)
app.layout = html.Div([
grid,
dcc.Interval(id="interval", interval=2000, n_intervals=0)
])
@app.callback(
Output("grid", 'rowData',allow_duplicate=True),
Input("grid", 'cellValueChanged'),
State("grid","rowData"),
prevent_initial_call = True,
)
def udpdate_grid(cell_value_changed, row_data):
if ((cell_value_changed[0]['colId'] == 'Edit1') or (cell_value_changed[0]['colId'] == 'Edit2')):
print("Desirable Trigger")
return row_data
else:
print("Undesirable Trigger")
no_update
app.clientside_callback(
"""
async function(n_intervals) {
const gridApi = dash_ag_grid.getApi('grid');
if (gridApi) {
const rowIndex = Math.floor(Math.random() * 8);
const colIndex = Math.floor(Math.random() * 2);
const colId = ['Update1', 'Update2'][colIndex];
const rowNode = gridApi.getDisplayedRowAtIndex(rowIndex + 1);
if (rowNode) {
rowNode.setDataValue(colId, Math.random());
}
}
return window.dash_clientside.no_update;
}
""",
dash.dependencies.Output("grid", "rowData"),
dash.dependencies.Input("interval", "n_intervals"),
prevent_initial_call=True,
)
if __name__ == "__main__":
app.run_server(debug=True)