Ag grid : Field in JavaScript

Hello,

When I define my column like this:

{
            "headerName": "well_type",
            "field": "well_type",
            "rowGroup": True,
            "hide": True,
            "suppressColumnsToolPanel": True,
        },

I can retrieve the “field” in JavaScript like this:

params.node.field == "well_type"

But when I don’t use the rowGroup, how can I retrieve the field in JS?

            "headerName": "section",
            "field": "section",
            "rowGroup": False,
            "hide": False,
            "suppressColumnsToolPanel": True,

I need to differentiate between my rows “section” and “well_type”. I was using params.node.field, but if I don’t use the rowGroup, it doesn’t work anymore :frowning:

Thanks !

Hello @krowry,

I didnt know that the rowGroup had the field key. Thats cool.

The typical way to determine what column you are in would be like this:

params?.column?.colId == 'well_type'

This will only work if it is not a grouped row. :slight_smile:

Hello @jinnyzor,

Thanks for your answer :slight_smile:

I can’t use the params.column.colId because I call the js function on the same column :

"headerName": sample_size,
"field": "sample_size",
"valueGetter": {"function": "customSampleSizeValueGetter(params, 'sample_size')"},

Then I have all the time the same colId (sample_size).

On my column “sample_size”, I want different data if i’m on the parent row (well_type), or on specific children (“section”, “risk”, etc…).
If i’m on the row well_type, I will use the data “sample_size_well_type”, if i’m on the section row I will use the “sample_size_section” for example, this is the reason why I need to know on what row i am.

Can you please provide an MRE?

This will make it easier to help you.

@jinnyzor , I try to do an MRE :smiley:

import dash
from dash_ag_grid import AgGrid

app = dash.Dash(__name__)

app.layout = AgGrid(
    columnDefs=[
        {
            "headerName": "well_type",
            "field": "well_type",
            "rowGroup": True,
            "hide": True,
        },
        {
            "headerName": "section",
            "field": "section",
        },
        {
            "headerName": "sample_size",
            "field": "sample_size",
            "valueGetter": {"function": "customSampleSizeValueGetter(params, 'sample_size')"},
        },
    ],
    rowData=[
        {"well_type": "Perpignan", "section": "Castillet", "well_type_sample_size": 93, "section_sample_size": 3},
        {"well_type": "Mende", "section": "Cathedrale", "well_type_sample_size": 93, "section_sample_size": 50},
    ],
    dashGridOptions={
        "groupDisplayType": "multipleColumns",
        "groupAllowUnbalanced": True,
        "groupDefaultExpanded": 1,
    },
    defaultColDef={
        "resizable": True,
        "enableRowGroup": True,
        "editable": False,
        "suppressMovable": True,
        "domLayout": "autoHeight",
    },
    style={"height": "1000px"},
    enableEnterpriseModules=True,
    licenseKey="x",
)

if __name__ == "__main__":
    app.run_server(debug=True)

I have this definition of my AGGRID, on the column “sample_size”, for the row of my “well_type” I want put the “well_type_sample_size” value, for the row of my “section” I want put the “section_sample_size”.

I use function js :

dagfuncs.customOffSetListValueGetter = function (params, val) {
  if (params.node) {
    if (params.node.field == "well_type") {
        return params.node.allLeafChildren[params.node.allLeafChildren.length - 1].data["well_type_offset_list"];
    } else if (params.node.field == "section") {
        return params.node.allLeafChildren[params.node.allLeafChildren.length - 1].data["section_offset_list"];
    }
  }
  else {
        return params.data[val];
  }
}

This will work for my well_type because it’s a group but not for my section.

When I look the logs, I find nothing for a not group row :

For conclude, I want this :

Thanks a lot !

First thing:

Your data doesnt have these:
section_offset_list
well_type_offset_list

Your function wasnt lined up either:

dagfuncs.customOffSetListValueGetter = function (params, val) {
  if (params) {
    if (params.node.field == "well_type") {
        return params.node.allLeafChildren[params.node.allLeafChildren.length - 1].data["well_type_offset_list"];
    } else if (params.data) {
        return params?.data["section_offset_list"];
    }
  }
  else {
        return params.data[val];
  }
}

This should work though. :slight_smile:

1 Like

Thanks you @jinnyzor, I will do this :slight_smile: