Skip to content

Commit 96cbd78

Browse files
committed
feat(editors): add Column Editor collectionOverride option
- only applies to Editor with collection (autocomplete & single/multiple select)
1 parent b080bcb commit 96cbd78

20 files changed

+96
-25
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"postbuild:gh-demo:with-e2e": "npm run cypress:ci",
2424
"delete:dist": "cross-env rimraf dist",
2525
"cypress": "cd test/cypress && node node_modules/cypress/bin/cypress open",
26-
"cypress:ci": "cd test/cypress && npm run cypress:ci",
26+
"cypress:ci": "cd test/cypress && npm run cypress:ci --record",
2727
"test": "npm run test:jest",
2828
"test:jest": "node_modules/.bin/jest --config test/jest.config.js",
2929
"jest:clear": "node_modules/.bin/jest --clearCache",

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

+5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ export class GridEditorComponent implements OnInit {
254254
value: 0,
255255
operator: OperatorType.notEqual
256256
},
257+
// you could also provide a collection override to filter/sort based on the item dataContext or whatever else
258+
// collectionOverride: (updatedCollection, args) => {
259+
// console.log(args);
260+
// return updatedCollection.filter((col) => args.dataContext.id % 2 ? col.value < 50 : col.value >= 50);
261+
// },
257262
editorOptions: {
258263
maxHeight: 400
259264
}

src/app/modules/angular-slickgrid/editors/__tests__/autoCompleteEditor.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,23 @@ describe('AutoCompleteEditor', () => {
245245
expect(spy).toHaveBeenCalled();
246246
});
247247

248+
describe('collectionOverride callback option', () => {
249+
it('should create the editor and expect a different collection outputed when using the override', () => {
250+
mockColumn.internalColumnEditor = {
251+
collection: [{ value: 'other', label: 'Other' }, { value: 'male', label: 'Male' }, { value: 'female', label: 'Female' }],
252+
collectionOverride: (inputCollection) => inputCollection.filter(item => item.value !== 'other')
253+
};
254+
255+
editor = new AutoCompleteEditor(editorArguments);
256+
editor.destroy();
257+
editor.init();
258+
const editorCount = divContainer.querySelectorAll('input.editor-text.editor-gender').length;
259+
260+
expect(editorCount).toBe(1);
261+
expect(editor.elementCollection).toEqual([{ value: 'male', label: 'Male', labelPrefix: '', labelSuffix: '' }, { value: 'female', label: 'Female', labelPrefix: '', labelSuffix: '' }]);
262+
});
263+
});
264+
248265
describe('applyValue method', () => {
249266
it('should apply the value to the gender property when it passes validation', () => {
250267
mockColumn.internalColumnEditor.validator = null;

src/app/modules/angular-slickgrid/editors/__tests__/selectEditor.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,24 @@ describe('SelectEditor', () => {
634634
});
635635
});
636636

637+
describe('collectionOverride callback option', () => {
638+
it('should create the multi-select editor and expect a different collection outputed when using the override', () => {
639+
mockColumn.internalColumnEditor = {
640+
collection: ['other', 'male', 'female'],
641+
collectionOverride: (inputCollection) => inputCollection.filter(item => item !== 'other')
642+
};
643+
644+
editor = new SelectEditor(editorArguments, true);
645+
const editorBtnElm = divContainer.querySelector('.ms-parent.ms-filter.editor-gender button.ms-choice') as HTMLButtonElement;
646+
const editorListElm = divContainer.querySelectorAll<HTMLInputElement>(`[name=editor-gender].ms-drop ul>li input[type=checkbox]`);
647+
editorBtnElm.click();
648+
649+
expect(editorListElm.length).toBe(2);
650+
expect(editorListElm[0].value).toBe('male');
651+
expect(editorListElm[1].value).toBe('female');
652+
});
653+
});
654+
637655
describe('collectionInsideObjectProperty setting', () => {
638656
it('should create the multi-select editor with a value/label pair collection that is inside an object when "collectionInsideObjectProperty" is defined with a dot notation', () => {
639657
mockColumn.internalColumnEditor = {

src/app/modules/angular-slickgrid/editors/autoCompleteEditor.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export class AutoCompleteEditor implements Editor {
7878
}
7979

8080
/** Get Column Definition object */
81-
get columnDef(): Column | undefined {
82-
return this.args && this.args.column;
81+
get columnDef(): Column {
82+
return this.args.column;
8383
}
8484

8585
/** Get Column Editor object */
@@ -345,6 +345,11 @@ export class AutoCompleteEditor implements Editor {
345345
// assign the collection to a temp variable before filtering/sorting the collection
346346
let finalCollection = collection;
347347

348+
// user could also override the collection
349+
if (this.columnEditor && this.columnEditor.collectionOverride) {
350+
finalCollection = this.columnEditor.collectionOverride(finalCollection, { column: this.columnDef, dataContext: this.args.item, grid: this.grid });
351+
}
352+
348353
// user might provide his own custom structure
349354
// jQuery UI autocomplete requires a label/value pair, so we must remap them when user provide different ones
350355
if (Array.isArray(finalCollection)) {

src/app/modules/angular-slickgrid/editors/checkboxEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export class CheckboxEditor implements Editor {
2525
}
2626

2727
/** Get Column Definition object */
28-
get columnDef(): Column | undefined {
29-
return this.args && this.args.column;
28+
get columnDef(): Column {
29+
return this.args.column;
3030
}
3131

3232
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/dateEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ export class DateEditor implements Editor {
6565
}
6666

6767
/** Get Column Definition object */
68-
get columnDef(): Column | undefined {
69-
return this.args && this.args.column;
68+
get columnDef(): Column {
69+
return this.args.column;
7070
}
7171

7272
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/dualInputEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export class DualInputEditor implements Editor {
5454
}
5555

5656
/** Get Column Definition object */
57-
get columnDef(): Column | undefined {
58-
return this.args && this.args.column;
57+
get columnDef(): Column {
58+
return this.args.column;
5959
}
6060

6161
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/floatEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class FloatEditor implements Editor {
2828
}
2929

3030
/** Get Column Definition object */
31-
get columnDef(): Column | undefined {
32-
return this.args && this.args.column;
31+
get columnDef(): Column {
32+
return this.args.column;
3333
}
3434

3535
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/integerEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class IntegerEditor implements Editor {
2626
}
2727

2828
/** Get Column Definition object */
29-
get columnDef(): Column | undefined {
30-
return this.args && this.args.column;
29+
get columnDef(): Column {
30+
return this.args.column;
3131
}
3232

3333
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/longTextEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export class LongTextEditor implements Editor {
5757
}
5858

5959
/** Get Column Definition object */
60-
get columnDef(): Column | undefined {
61-
return this.args && this.args.column;
60+
get columnDef(): Column {
61+
return this.args.column;
6262
}
6363

6464
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/selectEditor.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ export class SelectEditor implements Editor {
157157
}
158158

159159
/** Get Column Definition object */
160-
get columnDef(): Column | undefined {
161-
return this.args && this.args.column;
160+
get columnDef(): Column {
161+
return this.args.column;
162162
}
163163

164164
/** Get Column Editor object */
@@ -551,6 +551,11 @@ export class SelectEditor implements Editor {
551551
newCollection = this.filterCollection(newCollection);
552552
newCollection = this.sortCollection(newCollection);
553553

554+
// user could also override the collection
555+
if (this.columnEditor && this.columnEditor.collectionOverride) {
556+
newCollection = this.columnEditor.collectionOverride(newCollection, { column: this.columnDef, dataContext: this.args.item, grid: this.grid });
557+
}
558+
554559
// step 1, create HTML string template
555560
const editorTemplate = this.buildTemplateHtmlString(newCollection);
556561

src/app/modules/angular-slickgrid/editors/sliderEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export class SliderEditor implements Editor {
3030
}
3131

3232
/** Get Column Definition object */
33-
get columnDef(): Column | undefined {
34-
return this.args && this.args.column;
33+
get columnDef(): Column {
34+
return this.args.column;
3535
}
3636

3737
/** Get Column Editor object */

src/app/modules/angular-slickgrid/editors/textEditor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class TextEditor implements Editor {
2626
}
2727

2828
/** Get Column Definition object */
29-
get columnDef(): Column | undefined {
30-
return this.args && this.args.column;
29+
get columnDef(): Column {
30+
return this.args.column;
3131
}
3232

3333
/** Get Column Editor object */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Column, SlickGrid } from './index';
2+
3+
export interface CollectionOverrideArgs {
4+
/** Column Definition */
5+
column: Column;
6+
7+
/** item data context object */
8+
dataContext: any;
9+
10+
/** Slick Grid object */
11+
grid: SlickGrid;
12+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface Column<T = any> {
4444
defaultSortAsc?: boolean;
4545

4646
/** Any inline editor function that implements Editor for the cell value or ColumnEditor */
47-
editor?: any | ColumnEditor;
47+
editor?: ColumnEditor;
4848

4949
/** Default to false, which leads to exclude the column title from the Column Picker. */
5050
excludeFromColumnPicker?: boolean;

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CollectionCustomStructure,
33
CollectionFilterBy,
44
CollectionOption,
5+
CollectionOverrideArgs,
56
CollectionSortBy,
67
EditorValidator,
78
FieldType,
@@ -33,11 +34,17 @@ export interface ColumnEditor {
3334
/** A collection of items/options that will be loaded asynchronously (commonly used with a Select/Multi-Select Editor) */
3435
collectionAsync?: Promise<any>;
3536

37+
/** We could filter 1 or more items from the collection */
38+
collectionFilterBy?: CollectionFilterBy | CollectionFilterBy[];
39+
3640
/** Options to change the behavior of the "collection" */
3741
collectionOptions?: CollectionOption;
3842

39-
/** We could filter 1 or more items from the collection */
40-
collectionFilterBy?: CollectionFilterBy | CollectionFilterBy[];
43+
/**
44+
* A collection override allows you to manipulate the collection provided to the Column Editor.
45+
* NOTE: if you provide a "customStructure" it will still be applied on the collection, in other words make sure that the collection returned by the override does have the properties defined in the "customStructure".
46+
*/
47+
collectionOverride?: (collectionInput: any[], args: CollectionOverrideArgs) => any[];
4148

4249
/** We could sort the collection by 1 or more properties, or by translated value(s) when enableTranslateLabel is True */
4350
collectionSortBy?: CollectionSortBy | CollectionSortBy[];

src/app/modules/angular-slickgrid/models/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export * from './checkboxSelector.interface';
1414
export * from './collectionCustomStructure.interface';
1515
export * from './collectionFilterBy.interface';
1616
export * from './collectionOption.interface';
17+
export * from './collectionOverrideArgs.interface';
1718
export * from './collectionSortBy.interface';
1819
export * from './column.interface';
1920
export * from './columnEditor.interface';

test/cypress/cypress.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"fixturesFolder": "fixtures",
88
"integrationFolder": "integration",
99
"pluginsFile": "plugins/index.js",
10+
"projectId": "hqnfoi",
1011
"reporter": "mochawesome",
1112
"screenshotsFolder": "screenshots",
1213
"supportFile": "support/index.js",

test/cypress/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "Ghislain B.",
1111
"license": "MIT",
1212
"devDependencies": {
13-
"cypress": "^6.1.0",
13+
"cypress": "^6.2.1",
1414
"mocha": "^8.2.0",
1515
"mochawesome": "^6.1.1"
1616
}

0 commit comments

Comments
 (0)