Skip to content

Commit

Permalink
fix: updateColumns() should be public use with column hidden (#1288)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Dec 28, 2023
1 parent 0b0b86c commit 211180b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
28 changes: 26 additions & 2 deletions packages/common/src/core/__tests__/slickGrid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('SlickGrid core file', () => {
});

it('should be able to instantiate SlickGrid and invalidate some rows', () => {
const columns = [{ id: 'firstName', field: 'firstName', name: 'First Name' }] as Column[];
const columns = [{ id: 'firstName', field: 'firstName', name: 'First Name', cellAttrs: { 'cell-attr': 22 }, }] as Column[];
const options = { enableCellNavigation: true, devMode: { ownerNodeIndex: 0 } } as GridOption;
const data = [{ id: 0, firstName: 'John' }, { id: 1, firstName: 'Jane' }];

Expand All @@ -171,8 +171,9 @@ describe('SlickGrid core file', () => {

grid.setData(data);
grid.invalidate();
const cellElms = container.querySelectorAll('.slick-cell.l0.r0');

expect(grid).toBeTruthy();
expect(cellElms[0].getAttribute('cell-attr')).toBe('22');
expect(invalidSpy).toHaveBeenCalled();
expect(updateSpy).toHaveBeenCalled();
expect(renderSpy).toHaveBeenCalled();
Expand Down Expand Up @@ -882,6 +883,29 @@ describe('SlickGrid core file', () => {
expect(result).toBe(80 * 2);
});

it('should return visible columns', () => {
const columns = [
{ id: 'firstName', field: 'firstName', name: 'First Name', hidden: true },
{ id: 'lastName', field: 'lastName', name: 'Last Name' },
{ id: 'age', field: 'age', name: 'age' },
] as Column[];
grid = new SlickGrid<any, Column>(container, [], columns, { ...options, frozenColumn: 1 });
const updateSpy = jest.spyOn(grid.onBeforeUpdateColumns, 'notify');
grid.updateColumns();
expect(grid.getVisibleColumns().length).toBe(2);

const newColumns = [
{ id: 'firstName', field: 'firstName', name: 'First Name', hidden: false },
{ id: 'lastName', field: 'lastName', name: 'Last Name', hidden: true },
{ id: 'age', field: 'age', name: 'age', hidden: true },
] as Column[];
grid.setColumns(newColumns);

expect(updateSpy).toHaveBeenCalled();
expect(grid.getHeader()[0]).toBeInstanceOf(HTMLDivElement);
expect(grid.getVisibleColumns().length).toBe(1);
});

it('should return full grid width when fullWidthRows is enabled even with frozenColumn defined', () => {
const columns = [
{ id: 'firstName', field: 'firstName', name: 'First Name', hidden: true },
Expand Down
39 changes: 19 additions & 20 deletions packages/common/src/core/slickGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,8 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
];

const sheet = this._style.sheet;

/* istanbul ignore else */
if (sheet) {
rules.forEach(rule => {
sheet.insertRule(rule);
Expand All @@ -2356,7 +2358,6 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
sheet.insertRule(`.${this.uid} .l${i} { }`);
sheet.insertRule(`.${this.uid} .r${i} { }`);
}
/* istanbul ignore else */
} else {
// fallback in case the 1st approach doesn't work, let's use our previous way of creating the css rules which is what works in Salesforce :(
this.createCssRulesAlternative(rules);
Expand Down Expand Up @@ -2763,24 +2764,22 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
}

protected applyColumnHeaderWidths() {
if (!this.initialized) {
return;
}

let columnIndex = 0;
const vc = this.getVisibleColumns();
this._headers.forEach((header) => {
for (let i = 0; i < header.children.length; i++, columnIndex++) {
const h = header.children[i] as HTMLElement;
const col = vc[columnIndex] || {};
const width = (col.width || 0) - this.headerColumnWidthDiff;
if (Utils.width(h) !== width) {
Utils.width(h, width);
if (this.initialized) {
let columnIndex = 0;
const vc = this.getVisibleColumns();
this._headers.forEach((header) => {
for (let i = 0; i < header.children.length; i++, columnIndex++) {
const h = header.children[i] as HTMLElement;
const col = vc[columnIndex] || {};
const width = (col.width || 0) - this.headerColumnWidthDiff;
if (Utils.width(h) !== width) {
Utils.width(h, width);
}
}
}
});
});

this.updateColumnCaches();
this.updateColumnCaches();
}
}

protected applyColumnWidths() {
Expand Down Expand Up @@ -2991,7 +2990,8 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
this.updateColumnsInternal();
}

protected updateColumns() {
/** Update columns for when a hidden property has changed but the column list itself has not changed. */
updateColumns() {
this.trigger(this.onBeforeUpdateColumns, { columns: this.columns, grid: this });
this.updateColumnsInternal();
}
Expand All @@ -3003,7 +3003,6 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
if (this.initialized) {
this.setPaneVisibility();
this.setOverflow();

this.invalidateAllRows();
this.createColumnHeaders();
this.createColumnFooter();
Expand Down Expand Up @@ -3342,7 +3341,7 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
(row % 2 === 1 ? ' odd' : ' even');

if (!d) {
rowCss += ' ' + this._options.addNewRowCssClass;
rowCss += ` ${this._options.addNewRowCssClass}`;
}

const metadata = (this.data as CustomDataView<TData>)?.getItemMetadata?.(row);
Expand Down

0 comments on commit 211180b

Please sign in to comment.