Changing multiple rows' value in AG Grid

Hey there :slight_smile:

I have a dash AG grid table (not enterprise) with the following columns:

columnDefs=[
    {
        "field": "main_number",
    },
    {
        "field": "description",
    },
    {
        "field": "valid_from",
        "editable": True,
    },
   {
        "field": "valid_to",
        "editable": True,
    },
]

The table has multiple rows that can have the same main_number, but different description.
What I want is to make sure that when I edit the valid from/to cells in one row, the other rows that have the same main number to be also changed automatically to the date I edit in one of the rows.

Any idea how this can be done? Thanks!

Hello @kjurukova,

You will need to use a callback to adjust the rowData or use rowTransaction to update the grid based upon the cellValuedChanged data.

You could also potentially use the grids API and iterate through forEachNode and if the main number matches the cellValuedChanged data main number, then use the setDataValue of the adjusted column.

There may be some ways I didn’t mention, but those are your options. :grin:

Thanks for the suggestion @jinnyzor !
I’ve tried something, but not sure if I am on the right path, since I don’t see the results I want :smiley:

@callback(
    Output("table", "rowData", allow_duplicate=True),
    Input("table", "cellValueChanged"),
    State("table", "rowData"),
    prevent_initial_call=True,
)
def update_table(cellValueChanged, data):
    try:
        changed_row = cellValueChanged[0]["value"]
        main_number = changed_row["main_number"]
        valid_from = changed_row["valid_from"]
        valid_to = changed_row["valid_to"]

        for row in data:
            if row["main_number"] == main_number:
                row["valid_from"] = valid_from
                row["valid_to"] = valid_to
        print (data)
        return data 

    except Exception as e:
        print(f"Error updating rows: {e}")
        return data

Am I looking in the right direction?

I think it should be something like this:

@callback(
    Output("table", "rowData", allow_duplicate=True),
    Input("table", "cellValueChanged"),
    State("table", "rowData"),
    prevent_initial_call=True,
)
def update_table(cellValueChanged, data):
    try:
        changed_row = cellValueChanged["value"]
        main_number = cellValueChanged['data']["main_number"]
        valid_from = cellValueChanged['data']["valid_from"]
        valid_to = cellValueChanged['data']["valid_to"]

        for row in data:
            if row["main_number"] == main_number:
                row["valid_from"] = valid_from
                row["valid_to"] = valid_to
        print (data)
        return data 

    except Exception as e:
        print(f"Error updating rows: {e}")
        return data

That works! Thanks so much @jinnyzor :nerd_face:

1 Like

Me again :smiley:

I am trying out the ag grid enterprise features and using rowGroup for that first ‘main_number’ field, but it seems that now the values don’t get changed. Any idea why?

Can you please provide an example of your app?

I’ll check it out.