Размер сетки AgColumnsToFit только при наличии свободного места в общей ширине сетки

Есть ли какая-то функция для регулировки ширины столбца, чтобы она соответствовала всей сетке (в основном, вызовите следующие

api.sizeColumnsToFit ()

только когда заголовков недостаточно для заполнения пустого пространства, доступного по всей ширине. введите описание изображения здесь


person Riya    schedule 09.04.2019    source источник


Ответы (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

person Sebastian    schedule 19.03.2020