47

How to hide the column in AG-Grid and also it should not be displayed in Tool Panel...

var columnDefs = [{ headerName: "Stone_ID", field: "Stone_ID", width: 100, hide: "true" }]
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
nkota
  • 555
  • 1
  • 6
  • 15

10 Answers10

58

You can set the column property of suppressToolPanel to true to achieve that.

var columnDefs = [
    {
       headerName: "Stone_ID",
       field: "Stone_ID",
       width: 100,
       hide: true,
       suppressToolPanel: true
    }
]
Ishwor Timilsina
  • 1,344
  • 12
  • 11
45

If you are looking for dynamically show/hide columns follow below:

You can use either setColumnVisible or setColumnsVisible.

NOTE: those functions expect a colKey(s) which is related to the field you set in columnDefs.

columnDefs = [
    {
       headerName: "Name", 
       field: "name", // => that!
       width: 150
    },
    {
       headerName: "Last Name", 
       field: "last_name", // => that!
       width: 150
    },
    // ...
];

When using setColumnVisible you can show/hide a single column

gridOptions.columnApi.setColumnVisible('name', false) //In that case we hide it

When using setColumnsVisible you can show/hide multiple columns

gridOptions.columnApi.setColumnsVisible(['name', 'last_name'], true) //In that case we show them
Roland
  • 24,554
  • 4
  • 99
  • 97
14

To do this programatically you can use:

Hide columns:

this.agGrid.columnApi.setColumnsVisible(["COL_ID_1", "COL_ID_2"], false);

Show columns:

this.agGrid.columnApi.setColumnsVisible(["COL_ID_1", "COL_ID_2"], true);

To do this for a whole column group you can use:

  const group = this.columnApi.getColumnGroup("MY_GROUP");
  group.children.forEach(child => this.columnApi.setColumnsVisible(child, false));
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66
7

Posting late but It may helpful for others with some extra details:

If you want to hide your column on the grid you can use hide property to hide that column from the grid.

Only hide from grid colDef example:

colMainDef = [
    {
      headerName: 'Approved',
      field: 'status',
      hide: true
    }]

But the same column will still be accessible on the side menu panel if you want to hide it from the side menu you can use one more property suppressColumnsToolPanel by making this true it will hide the column from the side menu.

Hide column from the grid as well as from side panel colDef example:

colMainDef = [
    {
      headerName: 'Approved',
      field: 'status',
      suppressColumnsToolPanel: true,
      hide: true,
    }]

Hope this will helps to hide/show columns as per your requirements.

CodeChanger
  • 7,953
  • 5
  • 49
  • 80
3
var columnDefs = [{ headerName: "Stone_ID", field: "Stone_ID", width: 100, hide: true }]
adiga
  • 34,372
  • 9
  • 61
  • 83
Pathi
  • 31
  • 2
  • 5
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – adiga Nov 21 '17 at 12:05
  • 1
    Can I prevent a column from appearing in the column menu ? – eddy Oct 15 '20 at 15:24
2
{
  ...,
  hide: true,
  suppressColumnsToolPanel: true
}

In the package.json:

"dependencies": {
    "@ag-grid-community/angular": "^24.1.0",
    "@ag-grid-enterprise/all-modules": "^24.1.0",
    ...
} 
Adrian
  • 201
  • 2
  • 8
1

hide: should get the value true, not the string "true" (like width: gets 100, not "100")

ScottG
  • 534
  • 4
  • 9
  • thank you for giving answer..But.. hide: "true" this is working for hide the column from AG grid but this column appears in Toolpanel...My question is if hide the column then it should not display value as well as it should not appear inTool Panel... – nkota Jul 01 '16 at 11:57
1

Just add "hide: true" in your column definition.

Ex:

var columnDefs = [{ headerName: "ABC", field: "XYZ", hide: true }]
Indranil
  • 2,229
  • 2
  • 27
  • 40
0

hide column on load { headerName: 'ROE', field: 'roe', width: 100, hide: true },

based on selection you can show/hide it using example this.gridColumnApi.setColumnVisible('roe',true);

Ramesh
  • 11
0

We can hide column dynamically in the following way using onColumnVisible listen event

const onColumnVisible = useCallback(params => {
    if (params.source === 'toolPanelUi') {
        const colId = params.column ? [ params.column.colId ] : params.columns.map(col => col.colId)
        const value = columns.map(v => {
            if (v.colId === colId.find(c => c === v.colId)) {
                params.visible ? v.hide = false : v.hide = true
            }
            return v
        })
        if (value) {
            setColumns(value)
        }
    }
}, [ gridParams, setColumns ])

<Grid onColumnVisible={onColumnVisible } />
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133