For the minimal example here:
import dash
from dash import html, dcc
import dash_ag_grid as dag
import pandas as pd
app = dash.Dash(__name__)
df = pd.read_csv(
"testcsv.csv"
)
columnDefs = [
# Row group by country and by year is enabled.
{
"field": "country",
"rowGroup": True,
"hide": True,
"suppressColumnsToolPanel": True,
},
{
"field": "sport",
"rowGroup": False,
"hide": True,
"suppressColumnsToolPanel": True,
},
{"field": "gold", "sortable": True, "filter": True, "aggFunc": {"function": "customAvgAggFunc(params)"}},
]
app.layout = html.Div(
[
dcc.Markdown(
"This is an example of how to calculate a ratio using values from multiple columns."
),
dag.AgGrid(
enableEnterpriseModules=True,
columnDefs=columnDefs,
rowData=df.to_dict("records"),
defaultColDef={"resizable":True},
dashGridOptions={
"groupDisplayType": "singleColumn",
"autoGroupColumnDef": {
"headerName": "Country",
"flex": 10,
"checkboxSelection": True,
},
"rowSelection": "multiple",
"suppressAggFuncInHeader": True,
"groupSelectsChildren": True,
"suppressRowClickSelection": True,
}
),
]
)
if __name__ == "__main__":
app.run_server(debug=False)
with customagg function:
dagfuncs.customAvgAggFunc = function(params) {
let sum = 0;
let count = 0;
params.values.forEach((value) => {
const groupNode =
value !== null && value !== undefined && typeof value === 'object';
if (groupNode) {
sum += value.avg * value.count;
count += value.count;
} else if (typeof value === 'number') {
sum += value;
count++;
}
});
return (count !== 0) ? sum / count : null;
};
I get the following console reading:
rowNode: e
|---> childrenAfterGroup: Array(x)
|---> x: e
|---> childrenAfterGroup: Array(x)
|---> x: e
|---> selected: true
Thus, I tried to access childrenAfterGroup[1] like:
dagfuncs.customAvgAggFunc = function(params) {
let sum = 0;
let count = 0;
params.values.forEach((value) => {
const groupNode =
value !== null && value !== undefined && typeof value === 'object';
if (groupNode) {
const secondChild = value.childrenAfterGroup[1];
// Check if the secondChild exists and is selected
if (secondChild && secondChild.selected) {
sum += value.avg * value.count;
count += value.count;
}
} else if (typeof value === 'number') {
sum += value;
count++;
}
});
return (count !== 0) ? sum / count : null;
};
but with this I cannot access the selected parameter and return average only for selected rows. Could it even be done this way supposed my code would actually get the status of selected?