最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ag-grid Angular -- Show column menu of invisible column - Stack Overflow

programmeradmin6浏览0评论

In my application I have an ag-grid set up with some of the columns being always hidden. I need to be able to show the filter/column menu for these columns while they are hidden. (by pressing an arbitrary button elsewhere)

Originally this was achieved using gridApi.showColumnMenuAfterButtonClick(someHtmlElement). This is now deprecated and the intended replacement, gridApi.showColumnMenu(colId) doesn't work because it is unable to locate the header control for the column.

I don't have high hopes that this is still possible, but maybe someone knows something I don't. Thanks in advance.

A minimal example would be.

import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  ClientSideRowModelModule,
  ColDef,
  GridApi,
  GridReadyEvent,
  ModuleRegistry,
  ValidationModule,
} from "ag-grid-community";
ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ValidationModule /* Development Only */,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<div style="height: 100%; box-sizing: border-box">
    <ag-grid-angular
      style="width: 100%; height: 100%;"
      [columnDefs]="columnDefs"
      [rowData]="rowData"
      (gridReady)="onGridReady($event)"
    />
    <button (click)="onClick($event)">Menu</button>
  </div> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "athlete", filter:'agTextColumnFilter', hide:true },
    { field: "sport", filter:'agTextColumnFilter' },
    { field: "age" },
  ];
  rowData!: IOlympicData[];
  api!:GridApi;
  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.api = params.api;
    this.http
      .get<
        IOlympicData[]
      >(".json")
      .subscribe((data) => (this.rowData = data));
  }

  onClick(event){
    //this.api.showColumnMenu('sport');  works
    this.api.showColumnMenu('athlete');  //doesn't work
  }
}

In my application I have an ag-grid set up with some of the columns being always hidden. I need to be able to show the filter/column menu for these columns while they are hidden. (by pressing an arbitrary button elsewhere)

Originally this was achieved using gridApi.showColumnMenuAfterButtonClick(someHtmlElement). This is now deprecated and the intended replacement, gridApi.showColumnMenu(colId) doesn't work because it is unable to locate the header control for the column.

I don't have high hopes that this is still possible, but maybe someone knows something I don't. Thanks in advance.

A minimal example would be.

import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
import { AgGridAngular } from "ag-grid-angular";
import {
  ClientSideRowModelModule,
  ColDef,
  GridApi,
  GridReadyEvent,
  ModuleRegistry,
  ValidationModule,
} from "ag-grid-community";
ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ValidationModule /* Development Only */,
]);
import { IOlympicData } from "./interfaces";

@Component({
  selector: "my-app",
  standalone: true,
  imports: [AgGridAngular],
  template: `<div style="height: 100%; box-sizing: border-box">
    <ag-grid-angular
      style="width: 100%; height: 100%;"
      [columnDefs]="columnDefs"
      [rowData]="rowData"
      (gridReady)="onGridReady($event)"
    />
    <button (click)="onClick($event)">Menu</button>
  </div> `,
})
export class AppComponent {
  columnDefs: ColDef[] = [
    { field: "athlete", filter:'agTextColumnFilter', hide:true },
    { field: "sport", filter:'agTextColumnFilter' },
    { field: "age" },
  ];
  rowData!: IOlympicData[];
  api!:GridApi;
  constructor(private http: HttpClient) {}

  onGridReady(params: GridReadyEvent<IOlympicData>) {
    this.api = params.api;
    this.http
      .get<
        IOlympicData[]
      >("https://www.ag-grid/example-assets/olympic-winners.json")
      .subscribe((data) => (this.rowData = data));
  }

  onClick(event){
    //this.api.showColumnMenu('sport');  works
    this.api.showColumnMenu('athlete');  //doesn't work
  }
}
Share Improve this question edited Mar 26 at 12:24 OKayTee asked Mar 26 at 12:23 OKayTeeOKayTee 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

It would be best to raise a ticket on aggrid github, as far as a workaround, you can just make the column visible, then show the menu, then hide it again.

showColumnMenu(colKey: string) {
  // const foundIndex = this.columnDefs.find((item: any) =>item.field === colKey );
  // this.gridApi.moveColumns([colKey], this.columnDefs.length - 1);
  this.gridApi.setColumnsVisible([colKey], true);
  this.gridApi.showColumnMenu(colKey);
  setTimeout(() => {
  // this.gridApi.moveColumns([colKey], foundIndex);
  this.gridApi.setColumnsVisible([colKey], false);
  })
}

Plunkr Demo

发布评论

评论列表(0)

  1. 暂无评论