AG网格隐藏空列

发布于 2025-01-11 02:30:48 字数 183 浏览 0 评论 0原文

我正在尝试使用简单的行分组来实现 AG 网格,并尝试隐藏空列。有什么办法可以实现这一点吗? 输入图片此处描述

I am trying to implement an AG-grid with simple row grouping and trying to hide the columns that are empty. Is there any way to achieve that?
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛夏已如深秋| 2025-01-18 02:30:48

这可以帮助我在 tool.ts 中编写两个函数
第一个返回每列的值
第二个检查列是否为空然后隐藏它

    /**
   *
   * @param o // node data
   * @param propertyName  // columns name = the filed of the headerName
   * @returns
   * This function infers the type of the object T
   * and casts the property name to the key type K,
   * returning the property of the object using the given key T[K]
   */
  public static getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] {
    return o[propertyName];
  }
  /**
   *
   * @param gridApi
   * @param columnDefs
   * @param columnApi
   * Hide empty columns if all the column is empty
   */
  static hideEmptyColumns(
    gridApi: GridApi,
    columnDefs: any,
    columnApi: ColumnApi
  ) {
    let headers = []; // Headers array to store headersName fields
    if (gridApi) {
      columnDefs.forEach((col) => {
        col.children.forEach((child) => {
          headers.push(child.field);
        });
      });
      const columnsEmptyCheck = [];
      headers.forEach((it) => columnsEmptyCheck.push(true));
      // loop for each node
      gridApi.forEachNode((node) => {
        //loop throw headers
        headers.forEach((header, idx) => {
          const value = this.getProperty(node.data, header);
          if (value?.length === 0 || value === undefined) {
            columnsEmptyCheck[idx] = false;
          }
        });
      });

      /**
       * make the empty columns that has false unvisible
       */

      columnsEmptyCheck.forEach((element, index) => {
        if (element === false) {
          columnApi.setColumnsVisible([headers[index]], false);
        }
      });
    }
  }

This could help i write two function in a tool.ts
the first one returns the value of each column
and the second check if column is empty then hide it

    /**
   *
   * @param o // node data
   * @param propertyName  // columns name = the filed of the headerName
   * @returns
   * This function infers the type of the object T
   * and casts the property name to the key type K,
   * returning the property of the object using the given key T[K]
   */
  public static getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] {
    return o[propertyName];
  }
  /**
   *
   * @param gridApi
   * @param columnDefs
   * @param columnApi
   * Hide empty columns if all the column is empty
   */
  static hideEmptyColumns(
    gridApi: GridApi,
    columnDefs: any,
    columnApi: ColumnApi
  ) {
    let headers = []; // Headers array to store headersName fields
    if (gridApi) {
      columnDefs.forEach((col) => {
        col.children.forEach((child) => {
          headers.push(child.field);
        });
      });
      const columnsEmptyCheck = [];
      headers.forEach((it) => columnsEmptyCheck.push(true));
      // loop for each node
      gridApi.forEachNode((node) => {
        //loop throw headers
        headers.forEach((header, idx) => {
          const value = this.getProperty(node.data, header);
          if (value?.length === 0 || value === undefined) {
            columnsEmptyCheck[idx] = false;
          }
        });
      });

      /**
       * make the empty columns that has false unvisible
       */

      columnsEmptyCheck.forEach((element, index) => {
        if (element === false) {
          columnApi.setColumnsVisible([headers[index]], false);
        }
      });
    }
  }
尘世孤行 2025-01-18 02:30:48

AG-grid提供了一个getValue方法来抽象取值逻辑。
它还提供了一个对于此用例非常方便的 rowDataChanged 挂钩。

您可以在下面找到一个在 rowDataChanged 上运行的基本示例,该示例隐藏了空列。

hideEmptyColumns(params: RowDataChangedEvent) {
  const columns = params.columnApi.getAllColumns();
  const rowsNodes = params.api.getRenderedNodes();
  columns.forEach(column => {
    const isColumnEmpty = !rowsNodes.some(rowNode => {
      const value = params.api.getValue(column, rowNode);
      return typeof value !== 'undefined' && value !== null;
    });
    column.setVisible(!isColumnEmpty);
  });
}

AG-grid provides a getValue method that abstracts the value getting logic.
It also provides a rowDataChanged hook very handy for this use case.

You can find below a basic example to run on rowDataChanged that hides empty columns.

hideEmptyColumns(params: RowDataChangedEvent) {
  const columns = params.columnApi.getAllColumns();
  const rowsNodes = params.api.getRenderedNodes();
  columns.forEach(column => {
    const isColumnEmpty = !rowsNodes.some(rowNode => {
      const value = params.api.getValue(column, rowNode);
      return typeof value !== 'undefined' && value !== null;
    });
    column.setVisible(!isColumnEmpty);
  });
}
合约呢 2025-01-18 02:30:48

您可以通过在该列的列定义上设置 hide=true 来隐藏该列。

如果您想动态实现此目的,则可以利用 columnApi.applyColumnState()

  hideGold() {
    this.gridColumnApi.applyColumnState({
      state: [{ colId: 'gold', hide: true }],
    });
  }

请参阅 以下示例

有关列状态的文档。

You can hide a column by setting hide=true on the Column Definition for that column.

If you want to achieve this dynamically, then you can leverage columnApi.applyColumnState():

  hideGold() {
    this.gridColumnApi.applyColumnState({
      state: [{ colId: 'gold', hide: true }],
    });
  }

See this implemented in the following sample.

Documentation on Column State.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文