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

Commit 4c9039c

Browse files
authored
Merge pull request #1445 from ghiscoding/feat/col-picker-label
feat: add `columnPickerLabel` for custom label, also fix #1442
2 parents bfc2780 + 45b0f7f commit 4c9039c

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

src/app/examples/grid-grouping.component.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,31 @@ export class GridGroupingComponent implements OnInit {
4444
textExportService = new TextExportService();
4545

4646
ngOnInit(): void {
47+
this.initializeGrid();
48+
}
49+
50+
initializeGrid() {
51+
// add a simple button with event listener on 1st column for testing purposes
52+
// a simple button with click event
53+
const nameElementColumn1 = document.createElement('div');
54+
const btn = document.createElement('button');
55+
const btnLabel = document.createElement('span');
56+
btnLabel.className = 'mdi mdi-help-circle no-padding';
57+
btn.dataset.test = 'col1-hello-btn';
58+
btn.className = 'btn btn-outline-secondary btn-xs btn-icon ms-1';
59+
btn.textContent = 'Click me';
60+
btn.title = 'simple column header test with a button click listener';
61+
btn.addEventListener('click', () => alert('Hello World'));
62+
btn.appendChild(btnLabel);
63+
nameElementColumn1.appendChild(document.createTextNode('Id '));
64+
nameElementColumn1.appendChild(btn);
65+
4766
this.columnDefinitions = [
4867
{
49-
id: 'sel', name: '#', field: 'num', width: 40, type: FieldType.number,
68+
id: 'sel', name: nameElementColumn1, field: 'num', type: FieldType.number,
69+
columnPickerLabel: 'Custom Label', // add a custom label for the ColumnPicker/GridMenu when default header value extractor doesn't work for you ()
70+
width: 140, maxWidth: 150,
5071
excludeFromExport: true,
51-
maxWidth: 70,
5272
resizable: true,
5373
filterable: true,
5474
selectable: false,

src/app/modules/angular-slickgrid/global-grid-options.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Column, DelimiterType, EventNamingStyle, FileType, Filters, OperatorType, TreeDataOption } from '@slickgrid-universal/common';
1+
import { type Column, DelimiterType, EventNamingStyle, FileType, Filters, GridOption as GridOptionUniversal, OperatorType, type TreeDataOption } from '@slickgrid-universal/common';
22
import type { GridOption, RowDetailView } from './models/index';
33

44
/** Global Grid Options Defaults */
@@ -250,10 +250,15 @@ export const GlobalGridOptions: Partial<GridOption> = {
250250
* when using Column Header Grouping, we'll prefix the column group title
251251
* else we'll simply return the column name title
252252
*/
253-
function pickerHeaderColumnValueExtractor(column: Column) {
254-
const headerGroup = column && column.columnGroup || '';
253+
function pickerHeaderColumnValueExtractor(column: Column, gridOptions?: GridOptionUniversal) {
254+
let colName = column?.columnPickerLabel ?? column?.name ?? '';
255+
if (colName instanceof HTMLElement || colName instanceof DocumentFragment) {
256+
colName = colName.textContent || '';
257+
}
258+
const headerGroup = column?.columnGroup || '';
259+
const columnGroupSeparator = gridOptions?.columnGroupSeparator ?? ' - ';
255260
if (headerGroup) {
256-
return headerGroup + ' - ' + column.name;
261+
return headerGroup + columnGroupSeparator + colName;
257262
}
258-
return column && column.name || '';
259-
}
263+
return colName;
264+
}

src/app/modules/angular-slickgrid/models/gridOption.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { TranslateService } from '@ngx-translate/core';
22
import type { Column, GridOption as UniversalGridOption } from '@slickgrid-universal/common';
33

4-
import type { RowDetailView } from './index';
4+
import type { RowDetailView } from './rowDetailView.interface';
55

66
export interface GridOption<C extends Column = Column> extends UniversalGridOption<C> {
77
/** ngx-translate i18n translation service instance */

test/cypress/e2e/example14.cy.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('Example 14 - Grouping & Aggregators', () => {
2-
const fullTitles = ['#', 'Title', 'Duration', '% Complete', 'Start', 'Finish', 'Cost', 'Effort Driven'];
2+
const fullTitles = ['Id Click me', 'Title', 'Duration', '% Complete', 'Start', 'Finish', 'Cost', 'Effort Driven'];
33
const GRID_ROW_HEIGHT = 35;
44

55
it('should display Example title', () => {
@@ -182,4 +182,42 @@ describe('Example 14 - Grouping & Aggregators', () => {
182182
cy.get(`[style="top: ${GRID_ROW_HEIGHT * 0}px;"] > .slick-cell:nth(1)`).should('contain', 'Task 311');
183183
});
184184
});
185+
186+
describe('Column Header with HTML Elements', () => {
187+
it('should trigger an alert when clicking on the 1st column button inside its header', () => {
188+
const stub = cy.stub();
189+
cy.on('window:alert', stub);
190+
191+
cy.get('button[data-test=col1-hello-btn]')
192+
.click({ force: true })
193+
.then(() => expect(stub.getCall(0)).to.be.calledWith('Hello World'));
194+
});
195+
196+
it('should open Column Picker and have a "Custom Label" as the 1st column label', () => {
197+
cy.get('#grid14')
198+
.find('.slick-header-column')
199+
.first()
200+
.trigger('mouseover')
201+
.trigger('contextmenu')
202+
.invoke('show');
203+
204+
cy.get('.slick-column-picker')
205+
.find('.slick-column-picker-list li:nth-child(1) .checkbox-label')
206+
.should('have.text', 'Custom Label');
207+
});
208+
209+
it('should open Grid Menu and have a "Custom Label" as the 1st column label', () => {
210+
cy.get('#grid14')
211+
.find('button.slick-grid-menu-button')
212+
.trigger('click')
213+
.click({ force: true });
214+
215+
cy.get(`.slick-grid-menu:visible`)
216+
.find('.slick-column-picker-list li:nth-child(1) .checkbox-label')
217+
.should('have.text', 'Custom Label');
218+
219+
cy.get('[data-dismiss="slick-grid-menu"]')
220+
.click();
221+
});
222+
});
185223
});

0 commit comments

Comments
 (0)