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

Commit 4a05a16

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
Add multi-select plugin & checkbox selector plugin
1 parent e9962cd commit 4a05a16

File tree

11 files changed

+171
-9
lines changed

11 files changed

+171
-9
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ export class GridEditorComponent implements OnInit {
127127
}
128128
}
129129
});
130-
131130
}
132131
dataviewReady(dataview) {
133132
this.dataviewObj = dataview;

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Component, OnInit } from '@angular/core';
2-
import { Formatters } from './../modules/angular-slickgrid';
3-
import { Column, FieldType, Formatter, GridOption } from './../modules/angular-slickgrid/models';
2+
import { Column, FieldType, Formatter, Formatters, GridExtraService, GridOption } from './../modules/angular-slickgrid';
43

54
// create my custom Formatter with the Formatter type
6-
const myCustomCheckboxFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) =>
5+
const myCustomCheckmarkFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) =>
76
value ? `<i class="fa fa-fire" aria-hidden="true"></i>` : '<i class="fa fa-snowflake-o" aria-hidden="true"></i>';
7+
const customCheckboxFormatter: Formatter = (row, cell, value, columnDef, dataContext) => {
8+
return `<span class="fa fa-check selector-checkbox pointer"></span>`;
9+
};
810

911
@Component({
1012
templateUrl: './grid-formatter.component.html'
@@ -19,6 +21,8 @@ export class GridFormatterComponent implements OnInit {
1921
gridOptions: GridOption;
2022
dataset: any[];
2123

24+
constructor(private gridExtraService: GridExtraService) {}
25+
2226
ngOnInit(): void {
2327
this.columnDefinitions = [
2428
{id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string},
@@ -27,14 +31,16 @@ export class GridFormatterComponent implements OnInit {
2731
{id: 'percent2', name: '% Complete', field: 'percentComplete2', formatter: Formatters.progressBar, type: FieldType.number, sortable: true},
2832
{id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.dateIso },
2933
{id: 'finish', name: 'Finish', field: 'finish', formatter: Formatters.dateIso, sortable: true, type: FieldType.date },
30-
{id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: myCustomCheckboxFormatter, type: FieldType.number, sortable: true}
34+
{id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven', formatter: myCustomCheckmarkFormatter, type: FieldType.number, sortable: true}
3135
];
3236
this.gridOptions = {
3337
autoResize: {
3438
containerId: 'demo-container',
3539
sidePadding: 15
3640
},
37-
enableCellNavigation: false
41+
enableCellNavigation: false,
42+
enableCheckboxSelector: true,
43+
enableMultiSelect: false
3844
};
3945

4046
// mock a dataset

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export class AngularSlickgridComponent implements AfterViewInit, OnInit {
9999
this._gridOptions = this.mergeGridOptions();
100100

101101
this._dataView = new Slick.Data.DataView();
102+
this.controlAndPluginService.createPluginBeforeGridCreation(this.columnDefinitions, this._gridOptions);
102103
this.grid = new Slick.Grid(`#${this.gridId}`, this._dataView, this.columnDefinitions, this._gridOptions);
103-
this.grid.setSelectionModel(new Slick.RowSelectionModel());
104104

105105
this.controlAndPluginService.attachDifferentControlOrPlugins(this.grid, this.columnDefinitions, this._gridOptions, this._dataView);
106106
this.attachDifferentHooks(this.grid, this._gridOptions, this._dataView);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export const GlobalGridOptions: GridOption = {
1414
sidePadding: 0
1515
},
1616
cellHighlightCssClass: 'slick-cell-modified',
17+
checkboxSelector: {
18+
cssClass: 'slick-cell-checkboxsel'
19+
},
1720
editable: false,
1821
enableAutoResize: true,
1922
enableCellNavigation: false,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface CheckboxSelector {
2+
columnId?: string;
3+
cssClass?: string;
4+
toolTip?: string;
5+
width?: number;
6+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AutoResizeOption } from './autoResizeOption.interface';
22
import { BackendEventChanged } from './backendEventChanged.interface';
3+
import { CheckboxSelector } from './checkboxSelector.interface';
34
import { GridMenu } from './gridMenu.interface';
45
import { HeaderButton } from './headerButton.interface';
56
import { HeaderMenu } from './headerMenu.interface';
@@ -18,18 +19,21 @@ export interface GridOption {
1819
maxToolTipLength: number;
1920
};
2021
cellHighlightCssClass?: string | null;
22+
checkboxSelector?: CheckboxSelector;
2123
editable?: boolean;
2224
enableAsyncPostRender?: boolean;
2325
enableAutoResize?: boolean;
2426
enableAutoTooltip?: boolean;
2527
enableCellNavigation?: boolean;
28+
enableCheckboxSelector?: boolean;
2629
enableColumnPicker?: boolean;
2730
enableColumnReorder?: boolean;
2831
enableFiltering?: boolean;
2932
enableGridMenu?: boolean;
3033
enableHeaderButton?: boolean;
3134
enableHeaderMenu?: boolean;
3235
enableMouseHoverHighlightRow?: boolean;
36+
enableMultiSelect?: boolean;
3337
enablePagination?: boolean;
3438
enableRowSelection?: boolean;
3539
enableSorting?: boolean;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { BackendEventChanged } from './backendEventChanged.interface';
44
export { BackendServiceOption } from './backendServiceOption.interface';
55
export { CaseType } from './caseType';
66
export { CellArgs } from './cellArgs.interface';
7+
export { CheckboxSelector } from './checkboxSelector.interface';
78
export { Column } from './column.interface';
89
export { ColumnFilter } from './columnFilter.interface';
910
export { ColumnFilters } from './columnFilters.interface';

src/app/modules/angular-slickgrid/services/controlAndPlugin.service.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Injectable } from '@angular/core';
22
import { Router, NavigationEnd, NavigationStart } from '@angular/router';
33
import { FilterService } from './filter.service';
4+
import { GridExtraUtils } from './gridExtraUtils';
5+
import { GridExtraService } from './gridExtra.service';
46
import {
57
CellArgs,
8+
CheckboxSelector,
69
CustomGridMenu,
710
Column,
11+
Formatter,
812
GridOption,
913
HeaderButtonOnCommandArgs,
1014
HeaderMenuOnCommandArgs,
@@ -22,19 +26,51 @@ export class ControlAndPluginService {
2226

2327
// controls & plugins
2428
autoTooltipPlugin;
29+
checkboxSelectorPlugin: any;
2530
columnPickerControl;
2631
headerButtonsPlugin;
2732
headerMenuPlugin;
2833
gridMenuControl;
2934
rowSelectionPlugin;
3035

31-
constructor(private filterService: FilterService, private router: Router) {}
36+
constructor(private filterService: FilterService, private gridExtraService: GridExtraService, private router: Router) {}
3237

38+
/**
39+
* Attach/Create different Controls or Plugins after the Grid is created
40+
* @param {any} grid
41+
* @param {Column[]} columnDefinitions
42+
* @param {GridOptions} options
43+
* @param {any} dataView
44+
*/
3345
attachDifferentControlOrPlugins(grid: any, columnDefinitions: Column[], options: GridOption, dataView: any) {
3446
this._visibleColumns = columnDefinitions;
3547
this._dataView = dataView;
3648
this._grid = grid;
3749

50+
if (options.enableMultiSelect) {
51+
// when enabling the multi-select, we need to also watch onClick events to perform certain actions
52+
// the selector column has to be create BEFORE the grid (else it behaves oddly), but we can only watch grid events AFTER the grid is created
53+
grid.onClick.subscribe((e, args) => {
54+
const column = GridExtraUtils.getColumnDefinitionAndData(args);
55+
console.log('onClick', args, column);
56+
if (column.columnDef.id === 'multiSelectSelector') {
57+
const selectedRows = this.gridExtraService.getSelectedRows();
58+
const selectedRowIndex = selectedRows.findIndex((rowNumber) => rowNumber === args.row);
59+
60+
if (selectedRowIndex >= 0) {
61+
selectedRows.splice(selectedRowIndex, 1);
62+
} else {
63+
selectedRows.push(args.row);
64+
}
65+
this.gridExtraService.setSelectedRows(selectedRows);
66+
}
67+
});
68+
69+
// this also requires the Row Selection Model to be registered as well
70+
this.rowSelectionPlugin = new Slick.RowSelectionModel(options.rowSelectionOptions || {});
71+
grid.setSelectionModel(this.rowSelectionPlugin);
72+
}
73+
3874
if (options.enableColumnPicker) {
3975
this.columnPickerControl = new Slick.Controls.ColumnPicker(columnDefinitions, grid, options);
4076
}
@@ -64,6 +100,16 @@ export class ControlAndPluginService {
64100
this.autoTooltipPlugin = new Slick.AutoTooltips(options.autoTooltipOptions || {});
65101
grid.registerPlugin(this.autoTooltipPlugin);
66102
}
103+
104+
if (options.enableCheckboxSelector) {
105+
// when enabling the Checkbox Selector Plugin, we need to also watch onClick events to perform certain actions
106+
// the selector column has to be create BEFORE the grid (else it behaves oddly), but we can only watch grid events AFTER the grid is created
107+
grid.registerPlugin(this.checkboxSelectorPlugin);
108+
109+
// this also requires the Row Selection Model to be registered as well
110+
this.rowSelectionPlugin = new Slick.RowSelectionModel(options.rowSelectionOptions || {});
111+
grid.setSelectionModel(this.rowSelectionPlugin);
112+
}
67113
if (options.enableRowSelection) {
68114
this.rowSelectionPlugin = new Slick.RowSelectionModel(options.rowSelectionOptions || {});
69115
grid.setSelectionModel(this.rowSelectionPlugin);
@@ -183,4 +229,33 @@ export class ControlAndPluginService {
183229

184230
// options.gridMenu.resizeOnShowHeaderRow = options.showHeaderRow;
185231
}
232+
233+
/**
234+
* Attach/Create different plugins before the Grid creation.
235+
* For example the multi-select have to be added to the column definition before the grid is created to work properly
236+
* @param {Column[]} columnDefinitions
237+
* @param {GridOptions} options
238+
*/
239+
createPluginBeforeGridCreation(columnDefinitions: Column[], options: GridOption) {
240+
if (options.enableCheckboxSelector) {
241+
this.checkboxSelectorPlugin = new Slick.CheckboxSelectColumn(options.checkboxSelector || {});
242+
columnDefinitions.unshift(this.checkboxSelectorPlugin.getColumnDefinition());
243+
}
244+
245+
if (options.enableMultiSelect) {
246+
const customCheckboxFormatter: Formatter = (row, cell, value, columnDef, dataContext) => {
247+
return `<span class="fa fa-check selector-checkbox pointer"></span>`;
248+
};
249+
const columnSelector: Column = {
250+
id: 'multiSelectSelector',
251+
name: '<span id="selector-all-btn" class="fa fa-check selector-checkbox pointer"></span>',
252+
field: '', formatter: customCheckboxFormatter, minWidth: 35, width: 35, maxWidth: 35,
253+
cssClass: 'slick-selector'
254+
};
255+
256+
// const columnSelector;
257+
columnDefinitions.unshift(columnSelector);
258+
}
259+
}
186260
}
261+

src/app/modules/angular-slickgrid/services/gridExtra.service.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export class GridExtraService {
6262
}, fadeDelay + 10);
6363
}
6464

65+
getSelectedRows() {
66+
return this._grid.getSelectedRows();
67+
}
6568
setSelectedRow(rowIndex: number) {
6669
this._grid.setSelectedRows([rowIndex]);
6770
}

src/app/modules/angular-slickgrid/styles/_variables.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
/* SlickGrid Bootstrap variables */
88
/* Used by slick-bootstrap.scss */
99
$border-color: #dddddd !default;
10-
$font-size-base: 14px !default;
10+
$font-size-base-value: 14 !default;
11+
$font-size-base: $font-size-base-value + 0px !default;
1112
$font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif !default;
1213

1314
/* Slickgrid container, including headers but excluding pagination */
@@ -43,6 +44,8 @@ $row-highlight-background-color: rgba(212, 203, 156, 0.774) !default;
4344
$row-highlight-fade-animation: 2s ease-in 1 !default;
4445
$row-mouse-hover-color: rgb(245, 245, 204) !default;
4546
$row-selected-color: #e2e2c5 !default;
47+
$row-checkbox-selector-background: inherit !default;
48+
$row-checkbox-selector-border: none !default;
4649

4750
/* header */
4851
$header-input-height: 27px !default; // height of the filter form element (input, textarea, select)
@@ -122,3 +125,6 @@ $pagination-border-right: 0 none !default;
122125
$pagination-border-bottom: 0 none !default;
123126
$pagination-border-left: 0 none !default;
124127
$pagination-text-color: #808080 !default;
128+
129+
/* selector plugin */
130+
$selector-border-right: 1px solid rgb(196, 196, 196) !default;

0 commit comments

Comments
 (0)