4

Is there any way to get notified of click events on ag-grid's header in Angular?

The <ag-grid-angular>-Component offers multiple events for clicks on cells (e.g. cellDoubleClicked). Those don't get triggered for header clicks and I was unable to find any events specific for header clicks.

This question was already asked by someone else in the past and not answered, so I don't think, there's any built in way.

Why do I need this?

I'm working with another library to supply context menus for the application, but I want to differentiate between different context menus for headers and regular cells. So just listening to the default angular click event (on the whole grid component) doesn't help, because I need the clicked column/cell.

For reference I'm currently using ag-grid 15.0.0, but I also didn't find anything in the current documentation.

3 Answers 3

4

I think this may be possible if you define the header component (see: Header Components). In your template for the header component you can create a div element (or any other container element) and define the (click) event handler. This worked for me:

<div (click)='onHeaderClick($event)'>
  <p>Name</p>
</div>

(Of course you'd probably want to pass the header title in the parameters to the agInit() method, since you wouldn't want to create a separate header component for every header!)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. After some investigation it seems, that when implementing a custom Header Component, there's no real fallback behavior. I'd have to implement and maintain sorting and styling myself and I'm not a fan of that.
@KeTr I agree that it's not ideal, but it doesn't look as daunting as you might think. Take a look at this Plunker ... the header-component appears to implement the features you need: embed.plnkr.co/plunk/1rHK9l
2

You could basically listen to the "sortChanged" event, that ag-grid provides when you click on the default header.

2 Comments

Yes, and when in sortChanged, the event doesn't tell you which column, so call getSortModel() to get it.
And note that getSortModel() will have a null column every three click by default, so set the column sortingOrder to exclude 'no sort'
0

You can use onColumnHeaderClicked. For example:

 <AgGridReact
            ref={gridRef}
            rowData={rowData}
            columnDefs={columnsDef}
            onCellClicked={(event) => {
                console.log(event)
            }}
            onColumnHeaderClicked={(event) => {
                if (event.column.getId() === 'operations')
                    handleAdd()
            }}
            defaultColDef={{
                editable: true,
                sortable: false,
                suppressMovable: true,
                resizable: false
            }}
        />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.