【问题标题】:Ag grid sizeColumnsToFit only when there is space available in the total grid widthAg grid sizeColumnsToFit 仅在总网格宽度中有可用空间时
【发布时间】:2019-08-30 09:48:51
【问题描述】:

是否有一些功能可以调整列宽以适应整个网格(基本上调用以下

api.sizeColumnsToFit()

仅当没有足够的标题来填充总宽度中可用的空白空间时。

【问题讨论】:

    标签: javascript angular ag-grid ag-grid-ng2 ag-grid-angular


    【解决方案1】:

    我知道它有点晚了,但你知道...为时已晚总比没有好;-)

      /**
       * resizes the columns to fit the width of the grid
       * @param allowShrink if false, columns will NOT be resized when there is no "empty" horizontal space
       */
      public resizeColumnsToFit(allowShrink = true) {
        if (this.gridApi) {
          if (allowShrink) {
            this.gridApi.sizeColumnsToFit();
          } else {
            /**
             * this is a "hacK" - there is no way to check if there is "empty" space in the grid using the
             * public grid api - we have to use the internal tools here.
             * it could be that some of this apis will change in future releases
             */
            const panel = this.gridApi["gridPanel"];
            const availableWidth = this.gridApi["gridPanel"].eBodyViewport.clientWidth;
            const columns = this.gridApi["gridPanel"]["columnController"].getAllDisplayedColumns();
            const usedWidth = this.gridApi["gridPanel"]["columnController"].getWidthOfColsInList(columns);
    
            if (usedWidth < availableWidth) {
              this.gridApi.sizeColumnsToFit();
            }
          }
        }
      }
    

    您可以使用此方法有条件地调整网格列的大小。如果您不想在有水平滚动条时调整列大小,请使用resizeColumnsToFit(false)

    信用:https://github.com/ag-grid/ag-grid/issues/1772

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2020-08-23
      • 2019-05-30
      • 2017-07-18
      • 2019-07-16
      • 2017-03-02
      • 2018-03-10
      • 2020-08-19
      • 2020-02-17
      相关资源
      最近更新 更多