Skip to content

Commit

Permalink
fix(resize): add a patch to fix autoresize on Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Mar 18, 2020
1 parent d22ee64 commit 02faae4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/global-grid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const GlobalGridOptions: GridOption = {
bottomPadding: 20,
minHeight: 180,
minWidth: 300,
sidePadding: 0
rightPadding: 0
},
cellHighlightCssClass: 'slick-cell-modified',
checkboxSelector: {
Expand Down
8 changes: 4 additions & 4 deletions packages/common/src/interfaces/autoResizeOption.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export interface AutoResizeOption {
/** bottom padding of the grid in pixels */
bottomPadding?: number;

/** container id */
containerId?: string;
/** container selector, for example '.myGrid' or '#myGrid' */
container?: string;

/** maximum height (pixels) of the grid */
maxHeight?: number;
Expand All @@ -20,8 +20,8 @@ export interface AutoResizeOption {
/** minimum width (pixels) of the grid */
minWidth?: number;

/** side (left/right) padding in pixels */
sidePadding?: number;
/** padding on the right side of the grid (pixels) */
rightPadding?: number;

/** defaults to 10ms, delay before triggering the auto-resize (only on 1st page load) */
delay?: number;
Expand Down
4 changes: 2 additions & 2 deletions packages/vanilla-bundle-examples/src/examples/example01.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldType, GroupTotalFormatters, Formatters, OperatorType } from '@slickgrid-universal/common';
import { FieldType, GroupTotalFormatters, Formatters, OperatorType, GridOption } from '@slickgrid-universal/common';
import { Slicker } from '@slickgrid-universal/vanilla-bundle';

const actionFormatter = (row, cell, value, columnDef, dataContext) => {
Expand All @@ -22,7 +22,7 @@ export class Example1 {
gridClass;
gridClassName;
columnDefinitions;
gridOptions;
gridOptions: GridOption;
dataset;
dataviewObj: any;
gridObj: any;
Expand Down
4 changes: 2 additions & 2 deletions packages/vanilla-bundle-examples/src/examples/example02.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Aggregators, FieldType, Sorters, SortDirectionNumber, Grouping, GroupTotalFormatters, Formatters } from '@slickgrid-universal/common';
import { Aggregators, FieldType, Sorters, SortDirectionNumber, Grouping, GroupTotalFormatters, Formatters, GridOption } from '@slickgrid-universal/common';
import { Slicker } from '@slickgrid-universal/vanilla-bundle';

const actionFormatter = (row, cell, value, columnDef, dataContext) => {
Expand All @@ -22,7 +22,7 @@ export class Example2 {
gridClass;
gridClassName;
columnDefinitions;
gridOptions;
gridOptions: GridOption;
dataset;
dataviewObj: any;
gridObj: any;
Expand Down
28 changes: 27 additions & 1 deletion packages/vanilla-bundle/src/vanilla-grid-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import {
CollectionService,
GroupingAndColspanService,
SlickgridConfig,

// utilities
getScrollBarWidth,
} from '@slickgrid-universal/common';

import { TranslateService } from './services/translate.service';
Expand Down Expand Up @@ -187,7 +190,7 @@ export class VanillaGridBundle {
}
}

initialization(gridContainerElm: Element) {
async initialization(gridContainerElm: Element) {
// create the slickgrid container and add it to the user's grid container
this._gridContainerElm = gridContainerElm;
this._gridElm = document.createElement('div');
Expand Down Expand Up @@ -240,6 +243,8 @@ export class VanillaGridBundle {
if (this._gridOptions.enableAutoResize) {
const resizer = new Slick.Plugins.Resizer(this._gridOptions.autoResize);
this.grid.registerPlugin(resizer);
await resizer.resizeGrid();
this.compensateHorizontalScroll(this.grid);
}

// user might want to hide the header row on page load but still have `enableFiltering: true`
Expand Down Expand Up @@ -362,6 +367,27 @@ export class VanillaGridBundle {
}
}

/**
* For some reason this only seems to happen in Chrome and is sometime miscalculated by SlickGrid measureScrollbar() method
* When that happens we will compensate and resize the Grid Viewport to avoid seeing horizontal scrollbar
* Most of the time it happens, it's a tiny offset calculation of usually 3px (enough to show scrollbar)
* GitHub issue reference: https://github.com/6pac/SlickGrid/issues/275
*/
compensateHorizontalScroll(grid: any) {
const scrollbarDimensions = grid && grid.getScrollbarDimensions();
const slickGridScrollbarWidth = scrollbarDimensions && scrollbarDimensions.width;
const calculatedScrollbarWidth = getScrollBarWidth();

// if scrollbar width is different from SlickGrid calculation to our custom calculation
// then resize the grid with the missing pixels to remove scroll (usually only 3px)
const containerNode = grid && grid.getContainerNode && grid.getContainerNode() || '';
if (slickGridScrollbarWidth < calculatedScrollbarWidth && containerNode && containerNode.clientWidth) {
const previousWidth = containerNode && containerNode.clientWidth || 0;
containerNode.style.width = `${previousWidth + (calculatedScrollbarWidth - slickGridScrollbarWidth)}px`;
grid.resizeCanvas();
}
}

/**
* Dynamically change or update the column definitions list.
* We will re-render the grid so that the new header and data shows up correctly.
Expand Down

0 comments on commit 02faae4

Please sign in to comment.