Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 5b56617

Browse files
authored
Merge pull request #1543 from ghiscoding/bugfix/remap-all-rowspan
chore: add rowspan demo with show/hide column with full remapping
2 parents 284a074 + 698b736 commit 5b56617

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

src/app/examples/grid43.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ <h2>
7070
<span class="mdi mdi-chevron-down mdi-rotate-270"></span>
7171
Navigate to Right Cell
7272
</button>
73+
<button
74+
class="ms-2 btn btn-outline-secondary btn-sm btn-icon mx-1"
75+
data-test="toggle-employee-id"
76+
(click)="toggleEmployeeIdVisibility()"
77+
>
78+
Show/Hide EmployeeID
79+
</button>
7380
<button class="ms-1 btn btn-outline-secondary btn-sm btn-icon mx-1" (click)="toggleEditing()" data-test="toggle-editing">
7481
<span class="mdi mdi-pencil-outline"></span>
7582
<span

src/app/examples/grid43.component.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class Grid43Component implements OnInit {
1414
gridOptions!: GridOption;
1515
dataset: any[] = [];
1616
isEditable = false;
17+
showEmployeeId = true;
1718
showSubTitle = true;
1819
metadata: ItemMetadata | Record<number, ItemMetadata> = {
1920
// 10001: Davolio
@@ -152,6 +153,7 @@ export class Grid43Component implements OnInit {
152153
enableCellNavigation: true,
153154
enableColumnReorder: true,
154155
enableCellRowSpan: true,
156+
enableHeaderMenu: false,
155157
enableExcelExport: true,
156158
externalResources: [this.excelExportService],
157159
enableExcelCopyBuffer: true,
@@ -412,6 +414,38 @@ export class Grid43Component implements OnInit {
412414
];
413415
}
414416

417+
// when a side effect happens (e.g. show/hide EmployeeID),
418+
// you have to recalculate the metadata by yourself
419+
// if column index(es) aren't changing then "invalidateRows()" or "invalidate()" might be sufficient
420+
// however, when column index(es) changed then you will have to call "remapAllColumnsRowSpan()" to clear & reevaluate the rowspan cache
421+
toggleEmployeeIdVisibility() {
422+
const newMetadata: any = {};
423+
this.showEmployeeId = !this.showEmployeeId;
424+
425+
// direction to calculate new column indexes (-1 or +1 on the column index)
426+
// e.g. metadata = `{0:{columns:{1:{rowspan: 2}}}}` if we hide then new result is `{0:{columns:{0:{rowspan: 2}}}}`
427+
const dir = this.showEmployeeId ? 1 : -1;
428+
for (const row of Object.keys(this.metadata)) {
429+
newMetadata[row] = { columns: {} };
430+
for (const col of Object.keys((this.metadata as any)[row].columns)) {
431+
newMetadata[row].columns[Number(col) + dir] = (this.metadata as any)[row].columns[col];
432+
}
433+
}
434+
435+
// update column definitions
436+
if (this.showEmployeeId) {
437+
this.columnDefinitions.unshift({ id: 'employeeID', name: 'Employee ID', field: 'employeeID', width: 100 });
438+
} else {
439+
this.columnDefinitions.splice(0, 1);
440+
}
441+
this.angularGrid.slickGrid.setColumns(this.columnDefinitions);
442+
443+
// update & remap rowspans
444+
this.metadata = newMetadata;
445+
this.angularGrid.slickGrid.remapAllColumnsRowSpan();
446+
this.angularGrid.slickGrid.invalidate();
447+
}
448+
415449
toggleSubTitle() {
416450
this.showSubTitle = !this.showSubTitle;
417451
const action = this.showSubTitle ? 'remove' : 'add';

src/app/examples/grid44.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export class Grid44Component implements OnInit {
274274
enableCellNavigation: true,
275275
enableColumnReorder: true,
276276
enableCellRowSpan: true,
277+
enableHeaderMenu: false,
277278
gridHeight: 600,
278279
gridWidth: 900,
279280
rowHeight: 30,

test/cypress/e2e/example43.cy.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,80 @@ describe('Example 43 - colspan/rowspan - Employees Timesheets', { retries: 0 },
403403
cy.get('[data-row=6] > .slick-cell.l4.r4.active.editable .editor-text').should('not.exist');
404404
});
405405
});
406+
407+
describe('Rowspan Remapping', () => {
408+
describe('hide EmployeeID', () => {
409+
it('should hide EmployeeID', () => {
410+
cy.get('[data-test="toggle-employee-id"]').click();
411+
});
412+
413+
it('should expect EmployeeID to follow columns at index 0 column index', () => {
414+
cy.get(`[data-row=0] > .slick-cell.l0.r0.rowspan`).should('contain', 'Davolio');
415+
cy.get(`[data-row=0] > .slick-cell.l0.r0.rowspan`).should(($el) =>
416+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
417+
);
418+
419+
cy.get(`[data-row=2] > .slick-cell.l1.r3.rowspan`).should('contain', 'Check Mail');
420+
cy.get(`[data-row=2] > .slick-cell.l1.r3.rowspan`).should(($el) =>
421+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
422+
);
423+
424+
cy.get(`[data-row=8] > .slick-cell.l6.r8.rowspan`).should('contain', 'Development');
425+
cy.get(`[data-row=8] > .slick-cell.l6.r8.rowspan`).should(($el) =>
426+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
427+
);
428+
});
429+
430+
it('should expect "Lunch Break" to be moved to the left by 1 index less', () => {
431+
cy.get(`[data-row=0] > .slick-cell.l9.r11.rowspan`).should('contain', 'Lunch Break');
432+
cy.get(`[data-row=0] > .slick-cell.l9.r11.rowspan`).should(($el) =>
433+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 10)
434+
);
435+
});
436+
437+
it('should expect "Development" to be moved to the left by 1 index less', () => {
438+
cy.get(`[data-row=1] > .slick-cell.l12.r13.rowspan`).should('contain', 'Development');
439+
cy.get(`[data-row=1] > .slick-cell.l12.r13.rowspan`).should(($el) =>
440+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 5)
441+
);
442+
});
443+
});
444+
445+
describe('show EmployeeID', () => {
446+
it('should show EmployeeID', () => {
447+
cy.get('[data-test="toggle-employee-id"]').click();
448+
});
449+
450+
it('should expect EmployeeID to follow columns at index 1 column index', () => {
451+
cy.get(`[data-row=0] > .slick-cell.l1.r1.rowspan`).should('contain', 'Davolio');
452+
cy.get(`[data-row=0] > .slick-cell.l1.r1.rowspan`).should(($el) =>
453+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
454+
);
455+
456+
cy.get(`[data-row=2] > .slick-cell.l2.r4.rowspan`).should('contain', 'Check Mail');
457+
cy.get(`[data-row=2] > .slick-cell.l2.r4.rowspan`).should(($el) =>
458+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
459+
);
460+
461+
cy.get(`[data-row=8] > .slick-cell.l7.r9.rowspan`).should('contain', 'Development');
462+
cy.get(`[data-row=8] > .slick-cell.l7.r9.rowspan`).should(($el) =>
463+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 2)
464+
);
465+
});
466+
467+
it('should expect "Lunch Break" to be moved to the right by 1 index less', () => {
468+
cy.get(`[data-row=0] > .slick-cell.l10.r12.rowspan`).should('contain', 'Lunch Break');
469+
cy.get(`[data-row=0] > .slick-cell.l10.r12.rowspan`).should(($el) =>
470+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 10)
471+
);
472+
});
473+
474+
it('should expect "Development" to be moved to the right by 1 index less and a large "Development" section that spans over multiple columns & rows in the afternoon', () => {
475+
cy.get(`[data-row=1] > .slick-cell.l13.r14.rowspan`).should('contain', 'Development');
476+
cy.get(`[data-row=1] > .slick-cell.l13.r14.rowspan`).should(($el) =>
477+
expect(parseInt(`${$el.outerHeight()}`, 10)).to.eq(GRID_ROW_HEIGHT * 5)
478+
);
479+
});
480+
});
481+
});
406482
});

0 commit comments

Comments
 (0)