仅当总网格宽度中有可用空间时,Ag网格sizeColumnsToFit

时间:2019-04-09 07:16:02

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

是否有一些功能可以调整列宽以适合整个网格(基本上称为以下内容

  

api.sizeColumnsToFit()

仅当头文件不足以填充总宽度中的可用空白时。enter image description here

1 个答案:

答案 0 :(得分: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