# Column Menus

> Columns have menus that allow quick actions - the menus can be customized or hidden altogether.

Canonical page: https://infinite-table.com/angular/docs/learn/columns/column-menus

All columns in the Infinite Table have a default menu, which can be customized or hidden altogether.

## Customise the menu items

To customize the column menu (for all columns, or for a specific column), use the [`getColumnMenuItems`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#getColumnMenuItems) prop. This function is called with an array of menu items (which are the default items) and it should the final array of menu items - so you can return the default items as is, or you can adjust the default items to fit your needs.

```tsx
const getColumnMenuItems: InfiniteTablePropGetColumnMenuItems<Developer> = (items, { column }) => {
  if (column.id === 'firstName') {
    // you can adjust the default items for a specific column
    items.splice(0, 0, {
      key: 'firstName',
      label: 'First name menu item',
      onClick: () => {
        console.log('Hey there!');
      },
    });
  }

  // or for all columns
  items.push({
    key: 'hello',
    label: 'Hello World',
    onClick: () => {
      alert('Hello World from column ' + column.id);
    },
  });
  return items;
};
```

[`getColumnMenuItems`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#getColumnMenuItems) can return an empty array, in which case, the column menu will not be shown - however, people will still be able to click the menu icon to trigger the column context menu.

If you want to dynamically decide whether a column should show a menu or not, you can use the [`columns.renderMenuIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderMenuIcon) prop.

In this example, the currency and preferredLanguage columns have a custom icon for triggering the column context menu.

In addition, the `preferredLanguage` column has a custom header that shows a button for triggering the column context menu.

**Example: Custom column menu items and custom menu icon**

```ts
import { Component, TemplateRef, viewChild } from '@angular/core';

import {
  DataSource,
  InfiniteTable,
  renderTemplate,
} from '@infinite-table/infinite-angular';
import type {
  InfiniteTablePropColumns,
  InfiniteTablePropGetColumnMenuItems,
} from '@infinite-table/infinite-angular';

type Developer = {
  id: number;
  firstName: string;
  lastName: string;
  country: string;
  city: string;
  currency: string;
  preferredLanguage: string;
  stack: string;
  canDesign: 'yes' | 'no';
  hobby: string;
  salary: number;
  age: number;
};

const dataSource = () => {
  return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/developers1k')
    .then((r) => r.json())
    .then((data: Developer[]) => data);
};

const getColumnMenuItems: InfiniteTablePropGetColumnMenuItems<Developer> = (
  items,
  { column },
) => {
  if (column.id === 'firstName') {
    // you can adjust the default items for a specific column
    items.splice(0, 0, {
      key: 'firstName',
      label: 'First name menu item',
      onAction: () => {
        console.log('Hey there!');
      },
    });
  }

  items.push(
    {
      key: 'hello',
      label: 'Hello World',
      onAction: () => {
        alert('Hello World from column ' + column.id);
      },
    },
    {
      key: 'translate',
      label: 'Translate',
      menu: {
        items: [
          {
            key: 'translateToEnglish',
            label: 'English',
            onAction: () => {
              console.log('Translate to English');
            },
          },
          {
            key: 'translateToFrench',
            label: 'French',
            onAction: () => {
              console.log('Translate to French');
            },
          },
        ],
      },
    },
  );
  return items;
};

@Component({
  selector: 'demo-root',
  imports: [DataSource, InfiniteTable],
  template: `
    <infinite-data-source primaryKey="id" [data]="dataSource">
      <infinite-table
        debugId="getColumnMenuItems-example"
        [columnHeaderHeight]="70"
        [columns]="columns"
        [getColumnMenuItems]="getColumnMenuItems"
      />
    </infinite-data-source>

    <!-- custom menu icon -->
    <ng-template #globe>
      <div>🌎</div>
    </ng-template>

    
    <ng-template #languageHeader let-columnApi="columnApi">
      Preferred Language
      <!-- stop the propagation of pointerdown, so a click on the button doesn't sort the column, and of mousedown, so the menu is not closed automatically: the button toggles it -->
      <button
        style="border: 1px solid magenta; margin: 2px"
        (pointerdown)="$event.stopPropagation()"
        (mousedown)="
          $event.stopPropagation(); columnApi.toggleContextMenu($event.target)
        "
      >
        Toggle menu
      </button>
    </ng-template>
  `,
})
export default class App {
  protected readonly dataSource = dataSource;
  protected readonly getColumnMenuItems = getColumnMenuItems;

  private readonly globeTemplate =
    viewChild.required<TemplateRef<unknown>>('globe');
  private readonly languageHeaderTemplate =
    viewChild.required<TemplateRef<unknown>>('languageHeader');

  protected readonly columns: InfiniteTablePropColumns<Developer> = {
    currency: {
      field: 'currency',

      // custom menu icon
      renderMenuIcon: () => this.globeTemplate(),
    },

    preferredLanguage: {
      field: 'preferredLanguage',
      defaultWidth: 350,
      header: ({ columnApi, renderLocation }) => {
        // if we're inside the column menu with all columns, return only the col name
        if (renderLocation === 'column-menu') {
          return 'Preferred Language';
        }

        // but for the real column header
        // return this custom content
        return renderTemplate(this.languageHeaderTemplate(), { columnApi });
      },

      // custom menu icon
      renderMenuIcon: () => this.globeTemplate(),
    },
    salary: {
      field: 'salary',
      // hide the menu icon
      renderMenuIcon: false,
    },
    country: {
      field: 'country',
    },
    id: { field: 'id', defaultWidth: 80, renderMenuIcon: false },
    firstName: {
      field: 'firstName',
    },
  };
}
```

As you can see in the demo above, you can use [`getColumnMenuItems`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#getColumnMenuItems) to return the default items (received as the first parameter to the function), or another totally different array. We chose to pass the default items to the function, so you can use them as a starting point and adjust them to your needs.

Each item in the array you return from [`getColumnMenuItems`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#getColumnMenuItems) should have a `key` and a `label` property. Additionally, you can specify an `onAction` function, which will be called when the user clicks the menu item.

It's also possible to create items with submenus. For this, specify a `menu` property in the item, with an `items` array. Each item in the `items` array should have a `key` and a `label` property, as you would expect.

```tsx
const getColumnMenuItems: InfiniteTablePropGetColumnMenuItems<Developer> = (defaultItems, { column }) => {
  const items = [
    {
      key: 'translate',
      label: 'Translate',
      menu: {
        items: [
          {
            key: 'translateToEnglish',
            label: 'English',
            onAction: () => {
              console.log('Translate to English');
            },
          },
          {
            key: 'translateToFrench',
            label: 'French',
            onAction: () => {
              console.log('Translate to French');
            },
          },
        ],
      },
    },
  ];

  return items;
};
```

## Custom menu icon

To customize the menu icon, use the [`columns.renderMenuIcon`](https://infinite-table.com/angular/docs/reference/infinite-table-props.md#columns.renderMenuIcon) prop. This prop can be a boolean or a function that returns text, a template (a `TemplateRef` of your component, read with `viewChild`) or a component (`renderComponent(MyIcon)`).

```ts
@Component({
  selector: 'custom-menu-icon-snippet',
  imports: [InfiniteTable],
  template: `
    <infinite-table [columns]="columns" />

    <ng-template #globe>
      <div>🌎</div>
    </ng-template>
  `,
})
export class CustomMenuIconSnippet {
  private readonly globeTemplate =
    viewChild.required<TemplateRef<unknown>>('globe');

  protected readonly columns: InfiniteTablePropColumns<Developer> = {
    name: {
      field: 'firstName',
      renderMenuIcon: () => this.globeTemplate(),
    },
    salary: {
      field: 'salary',
      renderMenuIcon: false,
    },
  };
}
```

For a custom menu icon 🌠 you don't have to hook up the `mousedown`/`click` in order to show or hide the menu - all this is done for you - just return your custom template or component and you're good to go.
